0
0
Selenium Javatesting~15 mins

Custom ExpectedCondition in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Wait for a specific text to appear in an element using a custom ExpectedCondition
Preconditions (2)
Step 1: Locate the element with id 'status-message'
Step 2: Wait up to 10 seconds for the element's text to become exactly 'Completed'
Step 3: Verify that the element's text is 'Completed'
✅ Expected Result: The wait completes successfully when the element's text is 'Completed', and the assertion confirms the text matches
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Assert that the element's text equals 'Completed' after waiting
Best Practices:
Use a custom ExpectedCondition class or lambda for waiting
Use WebDriverWait with explicit waits instead of Thread.sleep
Use By.id locator for element identification
Handle possible timeout exceptions gracefully
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.TimeoutException;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CustomExpectedConditionTest {
    private WebDriver driver;

    public CustomExpectedConditionTest(WebDriver driver) {
        this.driver = driver;
    }

    public void waitForTextToBeCompleted() {
        By statusMessageLocator = By.id("status-message");

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

        // Custom ExpectedCondition using lambda
        ExpectedCondition<Boolean> textIsCompleted = drv -> {
            WebElement element = drv.findElement(statusMessageLocator);
            return "Completed".equals(element.getText());
        };

        try {
            wait.until(textIsCompleted);
            WebElement statusElement = driver.findElement(statusMessageLocator);
            assertEquals("Completed", statusElement.getText(), "Status message text should be 'Completed'");
        } catch (TimeoutException e) {
            throw new AssertionError("Timed out waiting for text to be 'Completed'", e);
        }
    }
}

The code defines a method waitForTextToBeCompleted that waits up to 10 seconds for the element with id status-message to have the exact text Completed.

We use WebDriverWait with a custom ExpectedCondition implemented as a lambda. This lambda checks the element's text and returns true only when it matches Completed.

If the wait succeeds, we assert the text is exactly Completed. If the wait times out, we throw an assertion error with a clear message.

This approach avoids using Thread.sleep, uses explicit waits, and follows best practices for element locating and exception handling.

Common Mistakes - 4 Pitfalls
Using Thread.sleep instead of explicit waits
Hardcoding XPath or CSS selectors that are brittle
Not handling TimeoutException when wait fails
Checking element text without waiting
Bonus Challenge

Now add data-driven testing with 3 different expected texts: 'Completed', 'Failed', 'Pending'

Show Hint