0
0
Selenium Javatesting~10 mins

Custom ExpectedCondition in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test demonstrates how to create and use a custom ExpectedCondition in Selenium WebDriver with Java. It waits for a specific element's text to become "Loaded" before proceeding, verifying the page state dynamically.

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.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

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

            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

            ExpectedCondition<Boolean> textIsLoaded = new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    WebElement element = d.findElement(By.id("status"));
                    return "Loaded".equals(element.getText());
                }

                public String toString() {
                    return "text to be 'Loaded' in element with id 'status'";
                }
            };

            wait.until(textIsLoaded);

            WebElement statusElement = driver.findElement(By.id("status"));
            assert "Loaded".equals(statusElement.getText()) : "Status text is not 'Loaded'";

            System.out.println("Test Passed: Status text is 'Loaded'.");
        } finally {
            driver.quit();
        }
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts and ChromeDriver is initializedBrowser window opens with a blank page-PASS
2Navigates to https://example.com/loadingpagePage loads with element id='status' showing initial text (e.g., 'Loading...')-PASS
3Waits up to 10 seconds for element with id 'status' text to become 'Loaded' using custom ExpectedConditionPage updates element text from 'Loading...' to 'Loaded' dynamicallyChecks if element text equals 'Loaded'PASS
4Finds element with id 'status' and asserts its text is 'Loaded'Element text is 'Loaded'assert "Loaded".equals(statusElement.getText())PASS
5Prints success message and closes browserBrowser closes-PASS
Failure Scenario
Failing Condition: Element with id 'status' text does not become 'Loaded' within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
What does the custom ExpectedCondition in this test check for?
AThe element with id 'status' is visible
BThe page URL contains 'loadingpage'
CThe element with id 'status' has text 'Loaded'
DThe element with id 'status' is clickable
Key Result
Creating custom ExpectedConditions allows precise waiting for specific page states, improving test reliability when built-in conditions are insufficient.