Understanding the Differences Between FluentWait and WebDriverWait

Understanding the Differences Between FluentWait and WebDriverWait

3 July 2024 Stephan Petzl Leave a comment QA

When working with Selenium WebDriver, managing waits is crucial to ensure your tests run smoothly and reliably. Two common types of waits are FluentWait and WebDriverWait. This article will delve into the differences between these two waits and provide guidance on when to use each.

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. Once set, the implicit wait is applied globally to the WebDriver instance.


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: Implicit waits are generally not recommended due to their global nature and less flexibility.

Explicit Wait

Explicit waits are used to wait for a certain condition to occur before proceeding further in the code. WebDriverWait is a commonly used explicit wait that calls the ExpectedCondition every 500 milliseconds 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: Use explicit waits when an element takes a long time to load or to check specific properties of an element, such as presence or clickability.

FluentWait

FluentWait is a more flexible type of explicit wait where you can specify:

  • Frequency with which FluentWait checks the conditions defined.
  • Specific types of exceptions to ignore while waiting.
  • Maximum amount of time to wait for a condition.

// Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
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 FluentWait: Use FluentWait when you need more control over the waiting process, such as specifying custom polling intervals and ignoring specific exceptions.

Key Differences

  • Setting Type: WebDriverWait always sets the type to WebDriver, whereas FluentWait can be set to any class, including WebDriver.
  • Default Exception Handling: WebDriverWait automatically ignores NotFoundException by default, whereas FluentWait does not ignore any exceptions unless specified.
  • Flexibility: FluentWait offers more customization options, such as polling intervals and exception handling, making it suitable for more complex scenarios.

Summary

In summary, if you are primarily dealing with WebDriver-related operations, WebDriverWait is usually sufficient. However, if you need to handle more complex waiting scenarios, such as repeatedly calling an API until a valid response is received, FluentWait offers the flexibility you need.

For more detailed guides on using Selenium WebDriver, check out our documentation section.

When it comes to automated testing, having the right tools can make a significant difference. Repeato is a no-code test automation tool for iOS and Android, designed to help you create, run, and maintain automated tests for your apps efficiently. With its computer vision and AI capabilities, Repeato offers a quick and easy setup, making it an excellent choice for quality assurance.

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