Sometimes, built-in wait conditions are not enough. Custom ExpectedCondition lets you wait for exactly what you need in your test.
0
0
Custom ExpectedCondition in Selenium Java
Introduction
When you want to wait for a specific text to appear on a page.
When you need to wait for a complex element state that Selenium does not provide by default.
When you want to wait for multiple conditions combined in a special way.
When you want to create reusable wait logic for your tests.
When you want to improve test reliability by waiting smartly for page changes.
Syntax
Selenium Java
ExpectedCondition<T> condition = new ExpectedCondition<T>() {
public T apply(WebDriver driver) {
// your custom logic here
return result != null ? result : null;
}
};The apply method returns a value when the condition is met, or null if not yet met.
You can use lambda expressions in Java 8+ for shorter syntax.
Examples
Waits until the element with id 'message' contains the text 'Success'.
Selenium Java
ExpectedCondition<Boolean> textToBePresent = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return driver.findElement(By.id("message")).getText().contains("Success");
}
};Waits until the element with id 'myElement' has the class 'active'. Returns the element if true, else null.
Selenium Java
ExpectedCondition<WebElement> elementHasClass = driver -> {
WebElement el = driver.findElement(By.id("myElement"));
String classes = el.getAttribute("class");
return classes != null && classes.contains("active") ? el : null;
};Sample Program
This test opens example.com and waits up to 10 seconds for the page title to contain 'Example Domain' using a custom ExpectedCondition. It prints true if the condition is met.
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; public class CustomExpectedConditionExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com"); ExpectedCondition<Boolean> waitForTitleContainsExample = new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().contains("Example Domain"); } }; WebDriverWait wait = new WebDriverWait(driver, 10); boolean titleIsCorrect = wait.until(waitForTitleContainsExample); System.out.println("Title contains 'Example Domain': " + titleIsCorrect); } finally { driver.quit(); } } }
OutputSuccess
Important Notes
Always return null from apply() if the condition is not met yet.
Use WebDriverWait with your custom ExpectedCondition to wait smartly.
Custom ExpectedConditions improve test stability by waiting for exactly what you need.
Summary
Custom ExpectedCondition lets you create your own wait logic.
Use apply() method to check condition and return result or null.
Combine with WebDriverWait to pause test until condition is true.