Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

WebDriverWait & FluentWait in Selenium WebDriver – Let’s Deep Dive

Posted on 11/09/2024 By admin

Wait is a generic functional interface. A functional interface consists of only abstract method. Wait interface consists of only method named “until”. This interface is for waiting until a condition is true or not null. “until” method implementation should wait until the condition evaluates to a value that is neither null nor false. Because of this contract, the return type must not be Void. If the condition does not become true within a certain time (as defined by the implementing class), this method will throw a non-specified Throwable which serves the purpose of implementer.

There are two implemented classes of Wait interface:-

  1. A directly implemented generic class named FluentWait
  2. A indirectly implemented class named WebDriverWait

In fact , class WebDriverWait extends FluentWait and FluentWait implements Wait. So WebDriverWait becomes indirect implementation of Wait interface. WebDriverWait is a specialization of FluentWait that used WebDriver instance.

Let’s see the signature of above types:-

public interface Wait

public class FluentWait implements Wait

FluentWait class declares a default sleep timeout or default polling interval as 500 MS.

public class WebDriverWait extends FluentWait

As per Selenium 4, there are three constructors in WebDriverWait class. Those are as below:-

  1. public WebDriverWait(WebDriver driver, Duration timeout)
  2. public WebDriverWait(WebDriver driver, Duration timeout, Duration sleep)
  3. public WebDriverWait(WebDriver driver, Duration timeout, Duration sleep, Clock clock, Sleeper sleeper)

Mostly used WebDriverWait constructor which is given below is deprecated in Selenium 4.

public WebDriverWait(WebDriver driver, long timeoutInSeconds)

Instead of using timeout in long, we need to use a final class named Duration which allows you to pass timeout in multiple units.

Let me explain you the third constructor of WebDriverWait class i.e.

public WebDriverWait(WebDriver driver, Duration timeout, Duration sleep, Clock clock, Sleeper sleeper)

  • driver the WebDriver instance to pass to the expected conditions
  • clock used when measuring the timeout
  • sleeper used to make the current thread go to sleep
  • timeout the timeout when an expectation is called
  • sleep the timeout used whilst sleeping

Sleeper is an interface that allows you to implement your own way how to make your code sleep. The default way is to call Thread.sleep(duration).

public interface Sleeper {

  Sleeper SYSTEM_SLEEPER = duration -> Thread.sleep(duration.toMillis());

  /**
   * Sleeps for the specified duration of time.
   *
   * @param duration How long to sleep.
   * @throws InterruptedException If the thread is interrupted while sleeping.
   */
  void sleep(Duration duration) throws InterruptedException;
}

If we use first or second constructor of WebDriverWait, it calls third constructor with default values for missing parameters. For example:-

If we use public WebDriverWait(WebDriver driver, Duration timeout) constructor, it will use default interval i.e. 500 MS, System default zone and default sleeper.

   */
  public WebDriverWait(WebDriver driver, Duration timeout) {
    this(
        driver,
        timeout,
        Duration.ofMillis(DEFAULT_SLEEP_TIMEOUT),
        Clock.systemDefaultZone(),
        Sleeper.SYSTEM_SLEEPER);
  }

Similarly, if we pass interval time as well i.e we use second constructor, it will override default time interval.

public WebDriverWait(
WebDriver driver, Duration timeout, Duration sleep) {     this(driver, timeout, sleep, Clock.systemDefaultZone(), Sleeper.SYSTEM_SLEEPER);   
} 

First two constructors calls third constructor of WebDriverWait internally i.e. constructor chaining and third constructor of WebDriverWait calls Super class constructor i.e. FluentClass and define timeout, polling interval and ignoring exceptions using FluentWait methods.

 
 public WebDriverWait(
      WebDriver driver, Duration timeout, Duration sleep, Clock clock, Sleeper sleeper) {
    super(driver, clock, sleeper);
    withTimeout(timeout);
    pollingEvery(sleep);
    ignoring(NotFoundException.class);
    this.driver = driver;
  }

You can see how WebDriverWait class constructor helps you to set timeout, polling interval. By default it ignores NotFoundException which is a WebDriverException. NotFoundException class in super class for below classes:-

  1. NoAlertPresentException
  2. NoSuchElementException
  3. NoSuchFrameException
  4. NoSuchWindowException

Let’s see some important methods of FluentWait class.

  1. public FluentWait withTimeout(Duration timeout)

Sets how long to wait for the evaluated condition to be true. The default timeout is 500 MS. The return type of this method is self which allows you to use builder pattern to configure FluentWait.

2. public FluentWait withMessage(final String message)

Sets the message to be displayed when time expires.

3. public FluentWait pollingEvery(Duration interval)

Sets how often the condition should be evaluated. Default polling interval is 500 MS. Note here that default timeout and default polling interval are same. In reality, the interval may be greater as the cost of actually evaluating a condition function is not factored in.

4. ignoreAll() and ignoring() methods:-

Method signatures are as below:-

public  FluentWait ignoreAll(Collection> types)
public FluentWait ignoring(Class
 exceptionType)

Configures this instance to ignore specific types of exceptions while waiting for a condition. Any exceptions not white listed will be allowed to propagate, terminating the wait.

So, you create a FluentWait or WebDriverWait reference, you can do similar stuffs like setting polling interval or ignoring exceptions. These waits can be categorized as Explicit Waits in Selenium WebDriver.

So there are in fact only two types of waits in Selenium WebDriver:-

  1. Implicit Wait – You achieve it using an inner interface named “Timeout” of WebDriver interface and it specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
  2. Explicit Wait – You achieve it using WebDriverWait or FluentWait. This can be used for custom waiting. You can use predefined expected conditions as well as you can write your own wait condition. You can go extra miles with FluentWait.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 43: Difference Between “Parameters” and “parameter” in TestNG
Next Post: amod mahajan

Related Posts

Interview Experience at Publicis Sapient Bangalore for Selenium & API Testing Profile – Jan– 2020 Uncategorized
Postman Tutorial Part 42 – Create Parameterized Request by Reading Data From CSV in Postman Uncategorized
December 2017 – Make Selenium Easy Uncategorized
Data Structure For Testers Uncategorized
TestNG Tutorials 56: DataProvider in TestNG – Parameterizing DataProvider Method to Provide Data to Multiple Test Methods Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark