Hers is our latest submission to the series of Selenium Webdriver commands. In this tutorial, we’ve brought you a new perspective on the Webdriver Fluent Wait command. This post will highlight the most used Fluent Wait methods and provide a step by step description of their usage in your projects. In all modern web applications which use HTML5 and AJAX for their user interface, Fluent Wait command is the best way to handle dynamic locators. It is an advanced Webdriver wait method which reduces chances of errors and increases stability.
Another Webdriver command which you can use to manage wait is the Webdriver Wait command. Perhaps you should refer our post which discusses the Webdriver Wait command at length. After reading both these posts, you would get a fair idea of the Wait commands. And you’ll be able to choose the right one from these Selenium Webdriver commands as per the context you are working.
Your choice of using Webdriver Wait command would depend on the nature of the web application. With the introduction of Ajax applications in the Web world, we have web elements which sometimes visible in just a second and sometimes take minutes to appear. In such cases, it’s better to make use of Fluent Wait command as this wait tries to find the web element repeatedly at regular intervals until the timeout or till the object gets found.
How to use Webdriver Fluent Wait Commands.
1- How does the Fluent Wait Command work?
Fluent Wait uses two parameters – timeout value and polling frequency.
First of all, it sets the following values.
1- The maximum amount of time to wait for a condition and
2- The frequency to check the success or failure of a specified condition.
Also, if you want to configure the wait to ignore exceptions such as <NoSuchElementException>, then you can add it to the Fluent Wait command syntax.
Below is a sample code which shows the implementation of Fluent Wait.
1.2- Step by step analysis of the Fluent Wait Command sample code.
Step-1: Fluent Wait starts with capturing the start time to determine delay.
Step-2: Fluent Wait then checks the condition defined in the until() method.
Step-3: If the condition fails, Fluent Wait makes the application to wait as per the value set by the <pollingEvery(5, SECONDS)> method call. Here in this example, it’s 5 seconds.
Step-4: After the wait defined in Step 3 expires, start time is checked against the current time. If the difference of the wait start time (set in step-1) and the current time is less than the time set in <withTimeout(30, SECONDS)> method, then Step-2 will need to repeat.
The above steps will recur until either the timeout expires or the condition becomes true.
In the next section, we’ll see a summary of most used Fluent Wait methods which could be very useful in implementing Fluent Wait.
2- Important Methods of Webdriver Fluent Wait Commands.
2.1- Fluent Wait Command Methods.
2.1.1- Commonly used Fluent Wait methods.
Method Name
Syntax
Description
withTimeout
Sets the wait for the condition to become true. The default timeout is FIVE_HUNDRED_MILLIS.
withMessage
Sets the message which would appear after the timeout.
pollingEvery
Sets the frequency of condition should be evaluated. The default polling interval is FIVE_HUNDRED_MILLIS.
ignoring
Lists the exception that you want to skip when the timeouts.
2.1.2- Advanced Fluent Wait methods.
Method Name
Syntax
Description
until
It’ll apply the instance’s input value to the given predicate until the timeout occurs or the predicate becomes true.
until
It’ll apply the instance’s input value to the given function until one of the following occurs:
The function returns neither null nor false.
The function throws an un-ignorable exception.
The timeout expires.
The current thread gets interrupted.
timeoutException
Throws a timeout exception.
2.2- Summary of Fluent Wait Methods.
From the above table it is seen that the until() method is an overloaded function which can take two types of parameters:
1- A Function()
2- A Predicate()
In the next section, we’ll talk about these entities and give examples of Webdriver Fluent Wait commands. But before you proceed, we recommend reading the below post. It’ll provide you step by step instructions to create a Selenium Webdriver project in Eclipse IDE. After going through this article, it would be easy for you to understand the sample code shown in the next section.
<img src="https://s.w.org/images/core/emoji/72x72/1f4a1.png" alt="