[英]WebDriver, Selenium2 - simple test failed
I try this test: 我尝试这个测试:
@Test
public void theUserShouldBeAbleToTypeInQueryTerms() {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement queryField = driver.findElement(By.name("q"));
queryField.sendKeys("cats");
queryField.submit();
assertThat(driver.getTitle(), containsString("cats"));
}
but got an error: 但出现错误:
Failure, Expected: a string containing "cats" got: "Google"
失败,预期:包含“ cats”的字符串得到:“ Google”
How can I modify the test to have SUCCESS ? 如何修改测试以取得成功?
You have to use WebDriverWait class. 您必须使用WebDriverWait类。 It will wait until title will be equals "cats" or timeout.
它将等到标题等于“ cats”或超时。 Here's an example of how it works (but you have to adjust the code for your case):
这是一个如何工作的示例(但您必须根据情况调整代码):
public static void waitForTitle(final PageTitle title, WebDriver driver) {
new WebDriverWait(driver, TestDriver.WAIT_TIMEOUT).until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
for (WebElement we : PageHelper.findElements(WebElementLocator.pageHeader, driver)) {
if (we.getText().equals(title.getValue())) {
return we;
}
}
throw new NoSuchElementException("Title not found: \"" + title.getValue() + "\"");
}
});
}
包含搜索结果的页面可能尚未加载-尝试等待几秒钟后再运行该断言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.