设置selenium webdriver的默认执行速度

我正在用webdriver运行一些GUI测试。 我直接从selenium IDE导出了一些测试。 在这个测试中,由于加载了下拉,我不得不降低IDE的运行速度。 如何在Selenium webdriver中减慢测试速度? 我已经把

driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); 

它一直在快速运行。 我知道睡眠选项,但这不是我想要的,我想改变webdriver的默认执行速度。 这是我的代码:

 import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.NoAlertPresentException; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class ProfileCheck { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private final StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { driver = new FirefoxDriver(); baseUrl = "http://localhost:8080"; driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); } @Test public void testProfileCheck() throws Exception { System.out.println("Test if profiles have access to the screen"); // Administrateur (has access) driver.get(baseUrl + "/Dashboard/index.jsp?lang=en"); driver.findElement(By.cssSelector("input[name=combo_profile]")).click(); driver.findElement( By.cssSelector(".x-boundlist-item:contains('Administrateur')")) .click(); driver.get(baseUrl + "/Params/ClientCutOff/index.jsp?lang=en"); try { assertTrue(driver.getTitle().matches("^[\\s\\S]*ClientCutOff$")); } catch (Error e) { verificationErrors.append(e.toString()); } // Habilitation (no access) driver.get(baseUrl + "/Dashboard/index.jsp?lang=en"); driver.findElement(By.cssSelector("input[name=combo_profile]")).click(); driver.findElement( By.cssSelector(".x-boundlist-item:contains('Habilitation')")) .click(); driver.get(baseUrl + "/Params/ClientCutOff/index.jsp?lang=en"); try { assertFalse(driver.getTitle().matches("^[\\s\\S]*ClientCutOff$")); } catch (Error e) { verificationErrors.append(e.toString()); } } @After public void tearDown() throws Exception { try { Thread.sleep(5000); driver.quit(); } catch (Exception e) { } String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } private boolean isAlertPresent() { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; } } private String closeAlertAndGetItsText() { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else { alert.dismiss(); } return alertText; } finally { acceptNextAlert = true; } } } 

阅读一些答案,没有帮助

降低Selenium Webdriver的速度

https://sqa.stackexchange.com/questions/8451/how-can-i-reduce-the-execution-speed-in-webdriver-so-that-i-can-view-properly-wh

Selenium IDE – 将默认速度设置为慢速

不要sleep !!!

 public static WebElement findElement(WebDriver driver, By selector, long timeOutInSeconds) { WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds); wait.until(ExpectedConditions.presenceOfElementLocated(selector)); return findElement(driver, selector); } 

一旦有了Selenium WebDriver的Java绑定的“setSpeed()”方法。 但它被弃用了,因为它对浏览器自动化毫无意义。

相反,如果驱动程序比加载元素或类似驱动程序“更快”,则应始终智能地使用等待来处理这些情况。

总结一下, 目前没有选择故意减慢 WebDriver本身的执行速度 。 除了实现Thread.sleep()之外,目前没有其他方法可以明确地减慢你的步骤

但是您可以探索一些其他选项:

例如,如果要减慢元素的点击速度,可以编写自己的方法来单击元素:

 public void clickElement(WebElement element) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } element.click(); } 

如果你想减慢findElement方法,你可以编写另一个帮助方法:

 public void findElement(By by) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } driver.findElement(by); } 

您甚至可以从其中一个WebDriver类编写自己的扩展类,如下所述:

 public class MyFirefoxDriver extends FirefoxDriver { @Override public WebElement findElement(By by) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return by.findElement((SearchContext) this); } 

您可以为要减速的所有执行编写这些方法。

在正常情况下,当它查找一个元素时,Selenium会一直试图找到一个元素,只要你设置了“隐式等待”的值。 这意味着如果找到的元素早于该元素,它将继续执行测试。 我不是Java的专家,但在你提供的代码中,我无法识别任何正在寻找下拉项的位。 由于下拉菜单可以在加载其中的实际项目之前加载很长时间,我会下注其中存在的问题:它正在查找下拉列表,找到它,停止等待并开始尝试执行其余的测试,因为尚未加载项目而失败。 因此,解决方案是查找您尝试选择的特定项目。 不幸的是,我在Java方面不够精通,无法为您提供实际的代码解决方案。

顺便说一句,根据我从Selenium IDE导出到其他东西的经验,你最终会得到几乎(但不完全)完全不同于你期望的测试。 他们可能正在做你期望他们的事情,但他们通常采取捷径或至少采用不同于你自己编码测试的方法。