Make Selenium Easy

How to Fluent With FluentWait in Selenium WebDriver? – Part 2

We are exploring FluentWait of Selenium WebDriver in depth so that we can be fluent in FluentWait. If you have not read Part 1 yet, read it here.

FluentWait<T> implements a generic functional interface Wait<T>. A functional interface consists of only one abstract method. Wait interface consists of only method “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.

We have already seen a non-selenium related condition evaluating as Boolean. In this post, we will see again a non-selenium related condition evaluating a not null value.

Problem Statement 2 :-

There is a List of Integer values. I would like to pick randomly data from List and asserting with desired one. If it is a match, return otherwise keep checking till timeout.

Solution :-

We can use any loop to do the same but here we will use FluentWait to demonstrate the capability of FluentWaut.

Output:-

Note here that second type parameter ( String in above example ) matters most because that decides the return type of apply method. First type parameter may or may not be used inside apply method.

But there is a twist. Let’s see that.

From previous post, we are using StringBuiler to store data and we perfrom deletion operation on word. Let’s change the data type from StringBuilder to String.

Updated Code :-

Output:-

Note above that length of string is not reduced like it was happening for StringBuilder. Reason is very simple. Reason is that String is immutable and StringBuilder is mutable. If you use any Wrapper class for primitive type, you will notice the same. If you use ArrayList, modification on List will impact actual reference. So, the parameter to apply method should be used carefully. When we define FluentWait for an instance , that is passed to apply method as parameter. Modification to this parameter may or may not change actual reference based on whether reference is mutable or immutable.

I hope you get a good idea about FluentWait now. Let’s see some Selenium related scenario where we can use FluentWait.

Problem Statement 3 :-

There is a link text that appears after sometime. So I need to wait till link text is displayed.

HTML Code :-

Selenium Code :-

Output :-

Let’s twist the above scenario. Above we have located WebElement and waited for the desired link text. So we used type parameters as WebElement and Boolean. This time we will try to locate element with link text and will ignore NoSuchElementException till element is located or timeout reaches. So type parameters will be WebDriver and WebElement respectively.

Output:-

You can use methods of ExpectedConditions class as well with FluentWait if you create FluentWait class instance with WebDriver type. Example is as below :-

You can clone the above example from my repo.

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.

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

2 thoughts on “How to Fluent With FluentWait in Selenium WebDriver? – Part 2

  1. Unfortunately you have mentioned String as Mutable and StringBuffer as Immutable… Can you please make it correct. I know it would be a typo.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.