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

Wait<T> 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<T>
  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<F>

public class FluentWait<T> implements Wait<T>

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

public class WebDriverWait extends FluentWait<WebDriver>

Constructors in WebDriverWait class:-

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

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Leave a Reply

Your email address will not be published. Required fields are marked *