Part 4: Waits In Selenium: Fluent Wait

  • Hello Folks,

We have covered below types of waits present in Selenium WebDriver:

  1. Thread.sleep()
  2. Implicit Wait.
  3. Explicit Wait

Consider below scenario:

You are testing Flipkart home page. Flipkart lists top items at home page which includes electronics, furniture etc. Now scenario is you need to find first 10 mobile displayed at home page. First of all you need to list all displayed item. It will include all displayed items. Then you need to filter out mobile out of all result. Once you find 10th mobile, you need to stop looking for next and return model names of top 10 mobiles.

You can use sleep, Implicit or explicit wait for above scenario but you can just wait till elements are found. But you need to wait till 10th mobile item is listed at home page. Apart from that Filtering, verification of count and returning model number can not be done along wait. But it can be achieved using fluent wait.

Sample code for above scenario:

Above code will make webdriver wait till 10th mobile item is displayed or waiting time expires.

Selenium webdriver provides another type of wait which is called Fluent wait. As per my understanding, Fluent wait is just an upgraded version of Explicit wait. In explicit wait, expected conditions are canned(predefined) but by using fluent wait, you can write your own conditions, you can assert and can do whatever you want.

What is FluentWait?

  • FluentWait is class in selenium which implements Wait interface. WebDriverWait is subclass of FluentWait. Now you remember syntax of defining explicit wait. You will be able to find relationship between explicit wait and fluent wait.
  • It  is an implementation of the Wait interface that may have its timeout and polling interval configured on the fly.
  • Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions  when searching for an element on the page.
  • From the selenium doc:

Sample usage:

  • Creating an instance of FluentWait can be simplified as:
  • Now see apply method. Don’t worry with the way of defining. It is java concept called Anonymous inner class.  “apply” is a method which gives Fluent wait is a new meaning.
  • Working with FluentWait has two parts. First defining and customizing wait and second part is for the condition which has to be validated, asserting and performing whatever action you want to do.
  • Functions is an interface which has an abstract method called “apply”.
  • There is one more important point. We are passing generic type as WebDriver and WebElement for Function.

It is not fixed. This generic type is used for apply method. First generic type will be argument for apply method and another generic type is return type of apply method. For example:

Now you must have got strong point of fluent wait. You can do whatever you want. You can locate web element and assert its properties as well and many more.

Where we should use FluentWait?

  • When you do not find suitable expected wait condition in explicit wait.
  • To handle dynamic web elements.
  • You need to do more then just waiting.
  • When you need to create your own customized wait conditions.

Fluent wait is most confusing topic while implementing. I tried to explain all basic concepts about fluent wait. You will have a better idea when you will start using it. If you have any doubt or you catch any mistake, please comment in comment section.

If you like my posts, please like, comment and share. Do not forget to subscribe for upcoming posts.


Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning