Fluent Wait- Synchronization in Selenium Part 3

We discussed the core concept of waiting in Selenium, Implicit wait, and Explicit wait. Today we are going to discuss “Mother of waits in Selenium”. You got it right. We will discuss Fluent wait which is the mother of all the waits because it has all the features which you find in Implicit wait as well as Explicit wait. Fluent wait easily handles all your synchronization problems; you just need to be specific to your scenarios.

Today’s topic is quite important for those who are about to give interviews on Selenium and handling real-time problems in your Selenium test automation as well. So I would suggest you be sincere while understanding the Fluent wait. Once you finish this topic then you will have choices among Implicit wait, Explicit wait and Fluent wait to solve the synchronization problem in Selenium.

You notice, seldom the Implicit wait may solve your problem or even Explicit wait may solve your problem, instead of using Fluent wait, but you should be wise in picking up the wait command among the available three. Because your goal should not be only covering your test scenarios, but also your goal should be to make your test script very stable so that it should not fail in an ad-hoc headless run. Hope you got my point.

Let’s understand the Fluent wait to the core.

What is the Fluent Wait in Selenium WebDriver?

The fluent wait inculcates WebDriver to wait for a certain condition over specific time; it checks the given condition in a frequency before throwing the Exception.

Example: There is an element which appears in 5 seconds and in next moment it appears in 25 seconds, so in that case Fluent wait checks the appearance of the element in a given frequency, let’s say the frequency of 5 seconds. Hence, if element appears in first polling time then fine, otherwise polling will undergo till the entire defined time. Thus, at the 5th polling element will appear.

So simply we use Fluent wait, where there is no fixed time length for the appearance of the web element.

Technical specifications of the Fluent Wait

  • Fluent wait is a part of the package – org.openqa.selenium.support.ui.FluentWait
  • It implements Wait interface
  • Polling frequency over the defined time for certain custom conditions is the best feature
  • It allows us to ignore the occurrences of any exceptions during polling period
  • It has taken the feature of both, Implicit wait and Explicit wait

Click here to read complete documentation uploaded on GitHub.

Fluent wait

Syntax of the Fluent Wait in Selenium WebDriver using Java programming

 Wait<WebDriver> fWait = new FluentWait<WebDriver>(driver)
				.withTimeout(30, TimeUnit.SECONDS)
				.pollingEvery(5, TimeUnit.SECONDS)
				.ignoring(NoSuchElementException.class);

WebElement clicableElement = fWait.until(new Function<WebDriver, WebElement>(){

public WebElement apply(WebDriver driver){

return driver.findElement(By.locator(locator_expression));

}

});

 

Explanation of the syntax of the Fluent Wait

I can feel your difficulty in understanding the above syntax, so let me help you.

Wait<WebDriver> fWait = new FluentWait<WebDriver>(driver)

In this line, it is simply creating the instance from Wait interface. It is similar to creating the instance for ChromeDriver() from WebDriver interface.

.withTimeout(30, TimeUnit.SECONDS)

This line set the limit of the full-time duration of 30 seconds. You can put any time here as per the requirement.

.pollingEvery(5, TimeUnit.SECONDS)

This states that it will check the appearance of the web element at the frequency of 5 seconds.

ignoring(NoSuchElementException.class);

According to this line, it will ignore the given exception during polling time.

WebElement clicableElement = fWait.until(new Function<WebDriver, WebElement>(){

public WebElement apply(WebDriver driver){

return driver.findElement(By.locator(locator_expression));

 

This syntax is all about creating a custom condition, like existing pre-defined condition in Explicit wait.

Hope you got the points. If you still have any confusion then leave your questions in the comment below.

Complete program

package SeleniumTester;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.testng.annotations.Test;

public class LocatorDemo {
  @Test
  public void locatorDemo() throws Exception {
	  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  
	  WebDriver driver = new ChromeDriver();
	  
	  //Implicit Wait
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
	  driver.get("https://www.inviul.com");
	  
	  Wait<WebDriver> fWait = new FluentWait<WebDriver>(driver)
				.withTimeout(60, TimeUnit.SECONDS)
				.pollingEvery(5, TimeUnit.SECONDS)
				.ignoring(NoSuchElementException.class);
		WebElement clicableElement = fWait.until(new Function<WebDriver, WebElement>(){
		public WebElement apply(WebDriver driver){
		return driver.findElement(By.xpath("//*[@name='submit']"));
		}
		});
		clicableElement.click();
	  
	  driver.close();
	  
	  driver.quit();
  
  }
}

Relation between Implicit wait, Explicit wait & Fluent wait

Let’s draw a common line between these three waits in Selenium. It will help us to remember them easily. So in Implicit wait, we have seen only time length was there and it was checking element in DOM. Next when we understood Explicit wait then we found there are some pre-defined conditions which it checks for the defined time duration, after the time expiry, it throws an exception. Thus, finally after understanding Fluent wait we understood that it has the combination of all the features of Implicit as well as Explicit wait. Like, Time duration as well as custom conditions, instead of pre-defined conditions. Hence, we have lots of flexibility in using Fluent wait for the synchronization of web elements in Selenium WebDriver.

 

One Response

Leave a Reply