Synchronizing a test with FluentWait

The FluentWait class is an implementation of Selenium WebDriver's Wait interface. Using the FluentWait class, we can define the maximum amount of time to wait for an element or condition as well as the frequency with which to check for the condition. We can also configure it to ignore specific types of exceptions such as the NoSuchElement exception while searching for an element.

Unlike implicit and explicit wait, FluentWait uses a maximum timeout value and polling frequency. For example, if we set the maximum timeout value as 20 seconds and polling frequency as 2 seconds, WebDriver will check for an element every 2 seconds until the maximum value. In addition to this, we can configure FluentWait to ignore specific types of exceptions while waiting for the condition.

The FluentWait class is helpful in automating AJAX applications or scenarios where element load time varies often.

In this recipe, we will explore some of these conditions with the FluentWait class.

How to do it...

Let's implement a test that uses the FluentWait class to find and wait for an element, as shown in following code example:

@Test
public void testFluentWait() {
  WebDriver driver = new ChromeDriver();
  // Launch the sample Ajax application
  driver.get("http://cookbook.seleniumacademy.com/AjaxDemo.html");

  try {
    driver.findElement(By.linkText("Page 4")).click();

    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(10, TimeUnit.SECONDS)
        .pollingEvery(2, TimeUnit.SECONDS)
        .ignoring(NoSuchElementException.class);

    WebElement message = wait
        .until(new Function<WebDriver, WebElement>() {
          public WebElement apply(WebDriver d) {
            return d.findElement(By.id("page4"));
          }
        });

    assertTrue(message.getText().contains("Nunc nibh tortor"));
  } finally {
    driver.quit();
  }
}

How it works...

We can define a fluent wait by using the FluentWait class. We can configure the maximum timeout for 10 seconds, polling frequency for every 2 seconds, and exceptions to be ignored by setting up methods of the FluentWait class, as shown in following code example:

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
  .withTimeout(10, TimeUnit.SECONDS)
  .pollingEvery(2, TimeUnit.SECONDS)
    .ignoring(NoSuchElementException.class);

When we use this instance further to find an element, WebDriver will wait for 10 seconds, polling for the given element every 2 seconds in the DOM:

  WebElement message = wait
      .until(new Function<WebDriver, WebElement>(){
        public WebElement apply(WebDriver d) {
          return d.findElement(By.id("page4"));
        }});

In this example, we are finding an element with FluentWait, and returning the found element back to the test.

There's more...

Unlike the previous example where we returned the element, we can also wait for given conditions. The FluentWait class can be configured in multiple ways to handle wait conditions, as shown in the following code example:

@Test
public void testFluentWaitWithPredicate() {

  final WebDriver driver = new ChromeDriver();
  // Launch the sample Ajax application
  driver.get("http://cookbook.seleniumacademy.com/AjaxDemo.html");

  try {
    FluentWait<By> wait = new FluentWait<By>(By.linkText("Page 4"))
        .withTimeout(1000, TimeUnit.MILLISECONDS)
        .pollingEvery(200, TimeUnit.MILLISECONDS)
        .ignoring(NoSuchElementException.class);

    wait.until(new Predicate<By>() {
      public boolean apply(By by) {
        try {
          return driver.findElement(by).isDisplayed();
        } catch (NoSuchElementException ex) {
          return false;
        }
      }
    });
    driver.findElement(By.linkText("Page 4")).click();
  } finally {
    driver.quit();
  }
}

See also

  • The Synchronizing a test with an implicit wait recipe
  • The Synchronizing a test with an explicit wait recipe
  • The Synchronizing a test with custom-expected conditions recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.37.254