Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Part 4: Waits In Selenium: Fluent Wait

Posted on 02/19/2025 By admin

Hello Folks,

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

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

Consider below scenarios:-

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:

FluentWait wait5 = new FluentWait(driver); wait5.withTimeout(30, TimeUnit.SECONDS);
wait5.pollingEvery(5, TimeUnit.SECONDS);
wait5.ignoring(NoSuchElementException.class); Function flag= new Function() { public Boolean apply(WebDriver driver) { List allEle= driver.findElements(By.name("foo")); List allText= new ArrayList(); for(WebElement e: allEle) { String textEle= e.getText(); if(textEle.equals("SomeData")) allText.add(textEle); if(allText.size()==10) { return true; } } return false; } };
wait5.until(flag);

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.

  • 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 Official documentation:-

 // 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")); } });

Creating an instance of FluentWait can be simplified as:

FluentWait wait5 = new FluentWait(driver);
wait5.withTimeout(30, TimeUnit.SECONDS);
wait5.pollingEvery(5, TimeUnit.SECONDS);
wait5.ignoring(NoSuchElementException.class);

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.

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

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:

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

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.

  • 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.

Uncategorized

Post navigation

Previous Post: REST Assured Tutorial 17 – Setting a Default RequestSpecification in Rest Assured
Next Post: Write and validate XPath Expressions and CSS Selectors in Chrome and Firefox browser

Related Posts

REST Assured Tutorial 6 – Interface in OOPS – Implement As You Wish Uncategorized
August 26, 2018 – Make Selenium Easy Uncategorized
March 2, 2019 – Make Selenium Easy Uncategorized
CombiningedXpath – Make Selenium Easy Uncategorized
ExeInUse – Make Selenium Easy 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