Fluent Wait is the one type of the three types of command to implement wait in Test Automation. Fluent Wait in Selenium is a feature that allows automation testers to wait for a certain condition to occur on a web page before proceeding further tests.

The Fluent Wait in Selenium is used to define maximum time for the web driver to wait for a condition, also the frequency with which we want to check the condition before throwing an “ElementNotVisibleException” exception. It checks for the web element at regular intervals until the object is found or timeout happens.

Frequency: Setting up a repeat cycle with the time frame to check the condition at the regular interval of time

Fluent Wait Syntax:

Wait wait = new FluentWait(WebDriver reference)
.withTimeout(Duration.ofSeconds(SECONDS))
.pollingEvery(Duration.ofSeconds(SECONDS))
.ignoring(Exception.class);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//someElementLocator")));

Wait:

This will create a new wait object that waits for a webdriver instance.

New FluentWait(WebDriver reference):

This will create a new fluent wait object.

With Timeout(Duration.ofSeconds(SECONDS)):

This will set the maximum time to wait for a certain condition to be met.

PollingEvery(Duration.ofSeconds(SECONDS)):

This sets the polling interval or the time interval to wait between each check to see if the condition has been met.

ignoring(Exception.class):

This specifies any exceptions to ignore during the wait.

Example of Fluent wait using Java with Selenium:

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 org.openqa.selenium.support.ui.Wait;
import java. time. Duration;
import java .util.NoSuchElementException;

public class FluentWaitExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.example.com");
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                .withTimeout(Duration.ofSeconds(30))
                .pollingEvery(Duration.ofSeconds(5))
                .ignoring(NoSuchElementException.class);

        WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myElement")));

        element.click();

        driver.quit();
    }
}

 

Support On Demand!

QA Automation