【发布时间】:2015-08-18 08:29:23
【问题描述】:
我正在使用 webdriver 运行一些 GUI 测试。我直接从 selenium IDE 导出了一些测试。在这个测试中,由于加载了一个下拉菜单,我不得不降低 IDE 的运行速度。如何减慢 Selenium webdriver 中的测试速度?我已经放了
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
它一直在以极快的速度运行。我知道 sleep 选项,但这不是我想要的,我想更改 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;
}
}
}
阅读了一些答案,但没有任何帮助
【问题讨论】:
-
是的,正如其他人所说,不要依赖定时等待。而是使用等待直到调用 - wait(until.elementLocated(By.css('csss selector')) 或 .wait(until.titleIs('title for the page''), 1000); 这样您还可以测试更多您的代码 - 出现正确的页面了吗?(css 选择器)元素出现了吗?
标签: java user-interface selenium selenium-webdriver selenium-ide