What is Fluentwait? How to Achieve Fluent wait in Selenium?

FluentWait in Selenium WebDriverFluentWait is a customized type of Explicit Wait, here we can define polling period according to our need 2 Secs or 5 Secs, We can also configure the wait to ignore some exceptions like NoSuchElementExceptions.

This wait will check for the elements in the DOM for every 5 Secs until the elements found, if the element found within the period it breaks the search for the element and goes for the further action.

Fluent wait = Explicit Wait + frequency with which control need to be checked on application + exceptions to be ignored like  NoSuchElementExceptions.

Syntax: 
 

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
//Wait for the condition
.withTimeout(30, TimeUnit.SECONDS)
// which to check for the condition with interval of 5 seconds.
.pollingEvery(5, TimeUnit.SECONDS)
//Which will ignore the NoSuchElementException
.ignoring(NoSuchElementException.class);

Features of Fluent Wait:

In Fluent Wait we define a Maximum amount of time to wait for a condition. Also, we will be defining a Polling frequency time while declaring the Fluent Wait.Fluent Wait will be polling the web pages every Polling frequency time to verify the given condition.

Fluent Wait has Inbuilt exception handler, which will handle NoSuchElementException until given Maximum amount of time has breached.

Refer below Example for Fluent Wait –

package demoPackage1;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import javax.lang.model.element.Element;

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

import com.google.common.base.Function;


public class ExplicitWait {

  @Test
  public void FluentWaitTest(){
    
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    
    driver.navigate().to("http://demowebshop.tricentis.com");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
       // Define fluent wait
       Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
           .withTimeout(30, TimeUnit.SECONDS)
           .pollingEvery(1, TimeUnit.SECONDS)
           .ignoring(NoSuchElementException.class);

       WebElement element = wait.until(new Function<WebDriver, WebElement>() 
       {
         public WebElement apply(WebDriver driver) { 
           return driver.findElement(By.xpath("//a[@class='ico-register']"));
         } } );
       element.click();
    
    driver.close();
    driver.quit();
  }
}

FluentWait in Selenium WebDriver

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories