Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests
In Selenium WebDriver, Fluent Wait is used to specify the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition before throwing an exception. It is more flexible than implicit and explicit waits as it allows you to define the wait condition, polling frequency, and even handle exceptions.

1. Dependencies

Ensure you have the necessary dependencies in your ‘pom.xml‘ if you’re using Maven:

				
					<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0</version> <!-- Use the latest version -->
</dependency>

				
			

2. Import Required Packages

Import the necessary classes in your Java file:

				
					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;
import java.util.function.Function;

				
			

3. Setup WebDriver

Initialize your WebDriver. For example, using ChromeDriver:

				
					public class FluentWaitExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        
        driver.get("https://example.com");
    }
}

				
			

4. Implement Fluent Wait

Here is an example of how to set up and use Fluent Wait:

				
					public class FluentWaitExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        
        driver.get("https://example.com");

        // Define the FluentWait
        Wait<WebDriver> wait = new FluentWait<>(driver)
            .withTimeout(Duration.ofSeconds(30))  // Maximum wait time
            .pollingEvery(Duration.ofSeconds(5))  // Check every 5 seconds
            .ignoring(NoSuchElementException.class);  // Ignore NoSuchElementException

        // Usage of FluentWait with a custom function to find an element
        WebElement element = wait.until(new Function<WebDriver, WebElement>() {
            public WebElement apply(WebDriver driver) {
                WebElement element = driver.findElement(By.id("someElementId"));
                if (element.isDisplayed()) {
                    return element;
                } else {
                    return null;
                }
            }
        });

        // Interact with the element
        element.click();

        // Clean up
        driver.quit();
    }
}

				
			

Explanation:

  • withTimeout(Duration.ofSeconds(30)): Specifies the maximum time to wait for the condition to be met (30 seconds in this case).
  • pollingEvery(Duration.ofSeconds(5)): Specifies the frequency to check the condition (every 5 seconds in this case).
  • ignoring(NoSuchElementException.class): Ignores the specified exception(s) while waiting. You can add more exceptions as needed.
  • until() method: Accepts a function that defines the condition to wait for. In this example, the condition is to find a visible element by its ID.

Customizing Fluent Wait

You can customize Fluent Wait further by adding more conditions, logging, or handling different exceptions. For example:

				
					Wait<WebDriver> wait = new FluentWait<>(driver)
    .withTimeout(Duration.ofSeconds(30))
    .pollingEvery(Duration.ofSeconds(5))
    .ignoring(NoSuchElementException.class)
    .ignoring(ElementNotInteractableException.class)
    .withMessage("Custom error message after timeout");  // Custom error message

WebElement element = wait.until(new Function<WebDriver, WebElement>() {
    public WebElement apply(WebDriver driver) {
        WebElement element = driver.findElement(By.id("someElementId"));
        if (element != null && element.isDisplayed()) {
            return element;
        } else {
            System.out.println("Element not found yet, retrying...");
            return null;
        }
    }
});

				
			

Advantages of Fluent Wait

  • Flexible Polling: Unlike implicit waits, Fluent Wait allows you to define the polling frequency.
  • Customizable Exception Handling: You can specify which exceptions to ignore.
  • Custom Conditions: You can define complex conditions using functions.

By understanding and utilizing Fluent Wait in Selenium, you can handle dynamic content and timing issues more effectively, leading to more robust and reliable automated tests.

Scroll to Top