Explain Fluent Wait In Selenium Webdriver

Posted By : Mohd Sheemal | 13-Jun-2018

Fluent wait is the seperate class in selenium webdriver and using this class we can wait for a specific condition until it is not satisfied so we have already know explicit wait also having lots of preconditions that are more than sufficient but using fluent wait you can customised specific condition.

 

In fluent wait we can change the default polling period based on our requirement so by defaut explicit wait , implicit wait, the default polling time is 250 mili seconds.

If you want to change the polling period 1 sec, 10 sec, 30 sec you can change by default 250 mili seconds is the polling period.

- Fluent wait is a class and is the part of org.openqa.selenium.support.ui package.

- It is an implementation of wait interface.

- Each familiar hold up occurrence characterizes the greatest measure of time to sit tight for a condition and we can give the recurrence with which to check the condition.

- We can also ignore any exception while polling element such as No such element exception in selenium.

 

Example :

 

import java.util.concurrent.TimeUnit;
 
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.FluentWait;
 
import com.google.common.base.Function;
 
public class FluentWaitDEmo {
 
public static void main(String[] args) throws InterruptedException {
 
           // Start browser
 
          WebDriver driver = new ChromeDriver();
 
           // Maximize browser
 
          driver.manage().window().maximize();
 
     // Start the application 
 
     driver.get("url here");
 
// Click on timer so clock will start
 
          driver.findElement(By.xpath("Write xpath")).click();
 
 
 
// Create object of FluentWait class and pass webdriver as input
 
          FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
 
 
 
           // It should poll webelement after every single second
 
          wait.pollingEvery(1, TimeUnit.SECONDS);
 
 
 
           // Max time for wait- If conditions are not met within this time frame then it will fail the script
 
          wait.withTimeout(10, TimeUnit.SECONDS);
 
 
 
       // we are creating Function here which accept webdriver and output as WebElement-
 
          WebElement element = wait.until(new Function<WebDriver, WebElement>() {
 
 
 
                // apply method- which accept webdriver as input
 
               @Override
 
               
public WebElement apply(WebDriver arg0) 
{
 
      // find the element
 
       WebElement ele = arg0.findElement(By.xpath("Write xpath"));
 
// Will capture the inner Text and will compare will WebDriver
 
// If condition is true then it will return the element and wait will be over
 
                     if (ele.getAttribute("innerHTML").equalsIgnoreCase("WebDriver")) 
                      {
 
                          System.out.println("Value is >>> " + ele.getAttribute("innerHTML"));
 
                          return ele;
 
                     }
 
// If condition is not true then it will return null and it will keep checking until condition is not true
 
else {
        System.out.println("Value is >>> " + ele.getAttribute("innerHTML"));
 
           return null;
 
       }
 
               }
 
          });
 
// If element is found then it will display the status
 
          System.out.println("Final visible status is >>>>> " + element.isDisplayed());
 
     }
 

 

Related Tags

About Author

Author Image
Mohd Sheemal

Mohd Sheemal is a Bright QA Engineer. He has a good knowledge over Selenium.

Request for Proposal

Name is required

Comment is required

Sending message..