Understanding FluentWait and WebDriverWait in Selenium

Understanding FluentWait and WebDriverWait in Selenium

16 July 2024 Stephan Petzl Leave a comment QA

When working with Selenium WebDriver, managing the timing of actions is crucial for ensuring that your tests run smoothly. Two primary methods for handling waits in Selenium are FluentWait and WebDriverWait. This guide will help you understand the differences between these two approaches and when to use each one.

Implicit Wait

An implicit wait tells WebDriver to poll the DOM for a specified amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0, meaning no wait.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

When to use: Generally not recommended as it applies to all elements globally.

Explicit Wait

An explicit wait allows you to define a specific condition to wait for before proceeding further in the code. WebDriverWait, a type of explicit wait, calls the ExpectedCondition every 500 milliseconds by default until it returns successfully.

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

When to use: Useful when an element takes a long time to load or when verifying specific properties of an element, such as presence or clickability.

FluentWait

FluentWait offers more flexibility compared to WebDriverWait. For each FluentWait instance, you can specify:

  • The frequency with which FluentWait checks the conditions.
  • Exceptions to ignore while searching for an element.
  • The maximum amount of time to wait for a condition.
Wait wait = new FluentWait(driver)
    .withTimeout(30, SECONDS)
    .pollingEvery(5, SECONDS)
    .ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function() {
  public WebElement apply(WebDriver driver) {
  return driver.findElement(By.id("foo"));
}
});

When to use: Ideal for scenarios where an element may appear intermittently, and you need to customize the polling frequency and ignored exceptions.

Key Differences

  • Setting Type: WebDriverWait always sets the type to WebDriver, while FluentWait can set the type to any class, including WebDriver.
  • Default Exception Handling: WebDriverWait ignores NotFoundException by default, whereas FluentWait does not ignore any exceptions unless specified.
  • Polling Frequency: Both WebDriverWait and FluentWait have a default polling interval of 500 milliseconds, but this can be configured.

Conclusion

In summary, if you are primarily performing WebDriver-related operations, WebDriverWait should suffice. However, if you need more control over the wait conditions, such as polling frequency and exception handling, FluentWait is the better option.

Enhancing Your Test Automation

For those looking to further enhance their test automation processes, consider using Repeato, a no-code test automation tool for iOS and Android. Repeato allows you to create, run, and maintain automated tests for your apps quickly and efficiently. By leveraging computer vision and AI, Repeato simplifies the setup and execution of tests, making it an excellent choice for quality assurance teams.

To learn more about setting up and using Repeato, visit our Getting Started page.

Like this article? there’s more where that came from!