Man*_*ria 3 java user-interface selenium selenium-ide selenium-webdriver
我正在用webdriver运行一些GUI测试.我直接从selenium IDE导出了一些测试.在这个测试中,我不得不降低IDE的运行速度,因为加载了下拉.如何在Selenium webdriver中减慢测试速度?我已经把
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
它一直在快速运行.我知道睡眠选项,但这不是我想要的,我想改变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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
阅读一些答案,没有帮助
不要用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);
}
Run Code Online (Sandbox Code Playgroud)
曾经有一个用于Selenium WebDriver的Java绑定的“ setSpeed()”方法。但它已被弃用,因为它对浏览器自动化没有意义。
相反,如果驱动程序比元素的加载“更快”或类似的加载,则应始终明智地使用等待来处理这些情况。
综上所述,当前没有选择故意降低 WebDriver本身的执行速度。除了实现Thread.sleep()之外,目前没有其他方法可以显着减慢您的步骤
例如,如果要减慢元素的单击速度,可以编写自己的方法来单击元素:
public void clickElement(WebElement element) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
element.click();
}
Run Code Online (Sandbox Code Playgroud)
如果您想减慢findElement方法,可以编写另一个帮助程序方法:
public void findElement(By by) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElement(by);
}
Run Code Online (Sandbox Code Playgroud)
你甚至可以从如描述的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);
}
Run Code Online (Sandbox Code Playgroud)
您可以为要降低速度的所有执行编写这些方法。
| 归档时间: |
|
| 查看次数: |
15909 次 |
| 最近记录: |