0
0
Selenium Javatesting~10 mins

FluentWait with polling in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses FluentWait with polling to wait for a button to become clickable on a web page. It verifies that the button can be clicked successfully.

Test Code - Selenium
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 java.time.Duration;
import java.util.NoSuchElementException;

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

            FluentWait<WebDriver> wait = new FluentWait<>(driver)
                    .withTimeout(Duration.ofSeconds(15))
                    .pollingEvery(Duration.ofSeconds(2))
                    .ignoring(NoSuchElementException.class);

            WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("dynamicButton")));

            button.click();

            WebElement message = driver.findElement(By.id("message"));
            assert message.getText().equals("Button clicked!") : "Message text did not match expected value.";

            System.out.println("Test Passed: Button clicked and message verified.");
        } finally {
            driver.quit();
        }
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open and ready-PASS
2Navigates to https://example.com/dynamic_buttonPage with dynamic button is loaded-PASS
3Creates FluentWait with 15 seconds timeout and 2 seconds polling interval, ignoring NoSuchElementExceptionWait object configured-PASS
4Waits until button with id 'dynamicButton' is clickableButton becomes clickable after some delayButton is clickablePASS
5Clicks the buttonButton clicked, page updates message-PASS
6Finds element with id 'message' and verifies its text equals 'Button clicked!'Message displayed on pageMessage text equals 'Button clicked!'PASS
7Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: Button does not become clickable within 15 seconds timeout
Execution Trace Quiz - 3 Questions
Test your understanding
What does the FluentWait pollingEvery(Duration.ofSeconds(2)) do in this test?
AIt clicks the button every 2 seconds
BIt waits 2 seconds after the button is found
CIt checks for the button every 2 seconds until timeout
DIt ignores the button for 2 seconds
Key Result
Using FluentWait with polling allows tests to repeatedly check for a condition at intervals, handling dynamic page elements more reliably than fixed waits.