What is Fluent wait in selenium

What is Fluent wait?

What is Fluent wait?What is Fluent wait?What is Fluent wait?

What is Fluent wait?

Fluent wait is a wait in Selenium in which we can give maximum amount of time to wait for specific condition with a frequency time in which we can validate the element in regular intervals before throwing “ElementNotVisibleException”.

When we can use Fluent Wait in Selenium?

We can use this wait command when a web element is visible in few seconds and sometimes it is taking longer time to visible. Mainly it happens in Ajay Applications. We can give polling time to check the web element in regular intervals. We can also configure the wait to ignore any exception during polling period.

Syntax:

Wait wait = new FluentWait(Webdriver_Reference)
.withTimout(timeOut, SECONDS)
.pollingEvery(timeOut, SECONDS)
.Ignoring(Exception class)

Example:

FluentWait wait = new FluentWait(driver);
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);

WebElement element = wait.until(new Function<WebDriver, WebElement) {    
    public WebElement apply(WebDriver driver) {    
        return driver.findElement(By.id("element"));    
    }
});

 

Note:

[wait.until selenium]

  • FluentWait is a class which implements Wait functional Interface. A functional interface is an interface which consist only one abstract method. Wait interface contains only one abstract method – ‘until’.
  • FluentWait class mainly accept two parameters mainly – timeout value and polling frequency. In above syntax we have given 30 seconds timeouts and 5 seconds polling time. So it will check the condition every 5 seconds till 30 seconds. But first time it will check the web element at 0th seconds. Here total number of polling is 6 times.
  • Function – It is a generic function which exists in com.google.common.base.Function.
  • It asks to you implement apply() method.
  • The above apply method takes Webdriver as an argument and return either true or false.

Fluent wait in Selenium with Example

In below scenario we will search ‘Selenium’ text on google and click on google search button. After that we will move into 2nd page and click on 2nd link text which is ‘Selenium – Health Professional’.

Script:

import java.util.concurrent.TimeUnit;

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 com.google.common.base.Function;

public class FluentWaitExp {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        driver.findElement(By.name("q")).sendKeys("Selenium");
        Thread.sleep(2000);
        driver.findElement(By.name("btnK")).click();

        @SuppressWarnings({ "deprecation" })
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS)
                .pollingEvery(2, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);

        WebElement element = (WebElement) wait.until(new Function<WebDriver, WebElement>() {
            @Override
            public WebElement apply(WebDriver arg0) {
                WebElement linkelement = driver.findElement(By.partialLinkText("Selenium - Health Professional"));

                if (linkelement.isEnabled()) {

                    System.out.println("Element is Found");
                }

                return linkelement;
            }
        });

        element.click();

    }

}

Screenshot:

Fluent Wait in Selenium

 

Output:

Fluent wait in Selenium Output


Frequently Asked Questions on Fluent Wait in Selenium Interviews:

Q1. Which wait is better in selenium?

Best practice in automation is to try always reducing the automation time. For this, we should always use a command which never waste the time after found the web element. We should use implicit wait, which is a global wait applied on all web element on the page. The default value of implicit wait is 0.

For any web element which have some conditions we can use explicit wait.

Q2. What are the different types of Wait commands in Selenium?

There are different types of waits available in selenium.

  1. Static wait
  2. Implicit wait
  3. Explicit wait
  4. Fluent wait

Q3. What is difference between implicit wait explicit wait and fluent wait?

Implicit Wait

Implicit wait time is applied on all elements.

No need to specify any conditions for wait.

Explicit Wait

Explicit wait is applied on only one element which is given by us.

We need to specify a condition for that web element before proceeding further in the code.

Fluent Wait

We need to provide polling time in this. Polling time is the frequency time on which it will check that particular element.

We can specify exception for ignoring it.

Q4. What is Explicit wait in selenium?

In this wait command, Webdriver wait for certain condition before proceeding ahead.

In web application some element takes longer time to load. In that case we should use explicit wait.

Syntax:

WebDriverWait wait = new WabDriverWait(driver, 30).
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“Locator”));

Q5. What is custom wait in Selenium?

A Custom ExpectedCondition is a class that contains of a constructor with the parameters of the expected condition. It implements the ExpectedCondition interface, overrides the apply method.

Q6. What is Dynamic wait in Selenium?

We can use explicit wait as a dynamic wait because it waits dynamically for specific conditions.

Apart from that we can use Fluent wait, Implicit wait also.


Watch this video for Selenium FluentWait:-


That’s all about Fluent Wait. To more about Selenium Questions, take a look on this link Selenium Interview Question

I hope you have enjoyed this post. Please follow this post ‘Fluent Wait in Selenium’ for your future reference.

Happy Learning 🙂

 

Leave a Comment