использование implictilyWait с Webdriver - PullRequest
1 голос
/ 20 июня 2011

lib- selenium-java2.0rc2.jar и selenium-server-standalone-2.-b3.jar

простой тест:

webDriver = new FirefoxDriver();
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")); 

Утверждение не удалось, и затем я добавил BAD оператор ожидания, непосредственно перед утверждением -

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")); 

Тест пройден успешно. Но потом неявно наткнулся на Wait и использовал его как -

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"));

Утверждение снова терпит неудачу, похоже, что неявно Wait () здесь не оказывает никакого влияния. И я определенно не хочу использовать Thread.sleep (). Какое может быть возможное решение?

1 Ответ

3 голосов
/ 20 июля 2011

Вы можете использовать класс webdriverwait для ожидания выполнения ожидаемого условия.

     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(...);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...