0
0
Selenium Javatesting~15 mins

FluentWait with polling in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Wait for a dynamic element to be visible using FluentWait with polling
Preconditions (3)
Step 1: Navigate to the test page URL
Step 2: Click the button with id 'loadButton'
Step 3: Use FluentWait to wait for the element with id 'dynamicElement' to be visible
Step 4: Set polling interval to 500 milliseconds and timeout to 10 seconds
Step 5: Verify that the element with id 'dynamicElement' is displayed
✅ Expected Result: The dynamic element with id 'dynamicElement' becomes visible within 10 seconds after clicking 'loadButton'
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Assert that the dynamic element is displayed after waiting
Best Practices:
Use FluentWait with polling interval and timeout
Use ExpectedConditions for visibility check
Avoid Thread.sleep; use explicit waits
Use By.id locator for element identification
Automated Solution
Selenium Java
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 java.time.Duration;
import java.util.NoSuchElementException;

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

            // Click the load button
            driver.findElement(By.id("loadButton")).click();

            // Create FluentWait instance
            Wait<WebDriver> wait = new FluentWait<>(driver)
                    .withTimeout(Duration.ofSeconds(10))
                    .pollingEvery(Duration.ofMillis(500))
                    .ignoring(NoSuchElementException.class);

            // Wait for the dynamic element to be visible
            WebElement dynamicElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicElement")));

            // Assertion: element is displayed
            if (dynamicElement.isDisplayed()) {
                System.out.println("Test Passed: Dynamic element is visible.");
            } else {
                System.out.println("Test Failed: Dynamic element is not visible.");
            }
        } finally {
            driver.quit();
        }
    }
}

This code starts by setting up the Chrome WebDriver and opening the test page.

It clicks the button with id loadButton to trigger loading the dynamic element.

Then, it creates a FluentWait object with a timeout of 10 seconds and polling every 500 milliseconds. It ignores NoSuchElementException during waiting.

The wait uses ExpectedConditions.visibilityOfElementLocated to wait until the dynamic element with id dynamicElement is visible.

Finally, it checks if the element is displayed and prints the test result.

The driver.quit() in finally ensures the browser closes even if an error occurs.

Common Mistakes - 3 Pitfalls
Using Thread.sleep instead of FluentWait
Not ignoring NoSuchElementException in FluentWait
Using hardcoded XPath instead of By.id
Bonus Challenge

Now add data-driven testing to click different buttons with ids 'loadButton1', 'loadButton2', 'loadButton3' and verify their respective dynamic elements appear.

Show Hint