简体   繁体   English

WebDriver,Selenium2-简单测试失败

[英]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.

相关问题 Selenium2 WebDriver发行说明 - Selenium2 WebDriver release notes 如何在套件中的测试类中共享一个webdriver实例?我使用Selenium2和Python - How can I share one webdriver instance in my test classes in the suite? I use Selenium2 and Python 使用Selenium2 WebDriver在junit中执行多次测试时发生HttpHostConnectException - HttpHostConnectException occured while executing multiple test in junit using Selenium2 WebDriver Selenium2和webdriver的一个很好的工作示例 - A good working example of Selenium2 and webdriver 使用Java的Selenium WebDriver(aka Selenium2)中的getPageSource() - getPageSource() in Selenium WebDriver(a.k.a Selenium2) using Java 使用Hudson运行Selenium2 / Webdriver测试时浏览器不可见 - Browsers not visible when running Selenium2/Webdriver tests with Hudson 使用JavaScript驱动Selenium2(通过WebDriver)的选项有哪些? - What are the options to drive Selenium2 (via WebDriver) using JavaScript? 在window.onload之后运行Selenium2 WebDriver executeScript吗? - Is Selenium2 WebDriver executeScript run after window.onload? 当浏览器窗口未聚焦时,使用Java的Selenium2(WebDriver)失败 - Selenium2 (WebDriver) using Java fails when browser window is not focused selenium2 webdriver找不到javascript新创建的元素 - selenium2 webdriver can't find the element newly created by javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM