using implictilyWait with Webdriver
lib- selenium-java2.0rc2.jar and selenium-server-standalone-2.-b3.jar
simple test:
webDriver = new FirefoxDriver();
webDriver.get("http://www.google.com");
webDriver.findElement(By.name("q")).sendKeys("Test");
webDriver.findElement(By.name("btnG")).click();
Assert.assertTrue(webD开发者_JAVA百科river.findElement(By.cssSelector("ol#rso>li:nth-child(1) a"))
.getText().equalsIgnoreCase("Test.com Web Based Testing and Certification Software v2.0"));
Assertion fails, and then I added BAD wait statement, just before asserition -
Thread.sleep(3000);
Assert.assertTrue(webDriver.findElement(By.cssSelector("ol#rso>li:nth-child(1) a"))
.getText().equalsIgnoreCase("Test.com Web Based Testing and Certification Software v2.0"));
Test succeeds. But then came across implicitlyWait and used it as -
webDriver = new FirefoxDriver();
webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
webDriver.get("http://www.google.com");
webDriver.findElement(By.name("q")).sendKeys("Test");
webDriver.findElement(By.name("btnG")).click();
Assert.assertTrue(webDriver.findElement(By.cssSelector("ol#rso>li:nth-child(1) a"))
.getText().equalsIgnoreCase("Test.com Web Based Testing and Certification Software v2.0"));
Assertion again fails, looks like implicitlyWait() does not have any impact here. And I definitely don't want to use Thread.sleep(). What could be the possible solution?
You can use webdriverwait class to wait for an expected condition to come true.
ExpectedCondition e = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
//Wait until text changes
return webDriver.findElement(By.cssSelector("ol#rso>li:nth-child(1) a"))
.getText().equalsIgnoreCase("Test.com Web Based Testing and Certification Software v2.0"));
}
};
Wait w = new WebDriverWait(driver, 60);
w.until(e);
//do asserts now
Assert.assertTrue(...);
精彩评论