Selenium WebDriver support utilities providing Page Object Model, waiting mechanisms, event handling, and UI utilities for robust test automation
—
The waiting utilities provide robust mechanisms for handling dynamic web content with configurable timeouts, polling intervals, and a comprehensive set of predefined conditions. These tools are essential for reliable test automation in modern web applications with asynchronous behavior.
public interface Wait<F> {
/**
* Wait until the function returns a truthy value or timeout occurs
* @param isTrue Function to evaluate repeatedly until it returns truthy value
* @return The return value of the function when truthy
* @throws TimeoutException if timeout occurs before condition is met
*/
<T> T until(Function<? super F, T> isTrue);
}public interface ExpectedCondition<T> extends Function<WebDriver, T> {
// Marker interface that extends Function<WebDriver, T>
// Used with Wait.until() to wait for specific conditions
}public class WebDriverWait extends FluentWait<WebDriver> {
/**
* Create WebDriverWait with timeout in seconds
* @param driver WebDriver instance
* @param timeOutInSeconds Maximum time to wait
*/
public WebDriverWait(WebDriver driver, long timeOutInSeconds);
/**
* Create WebDriverWait with timeout and polling interval
* @param driver WebDriver instance
* @param timeOutInSeconds Maximum time to wait
* @param sleepInMillis Polling interval in milliseconds
*/
public WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis);
/**
* Create WebDriverWait with custom clock and sleeper
*/
public WebDriverWait(WebDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut);
protected RuntimeException timeoutException(String message, Throwable lastException);
}import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
// Wait for element to be present
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("myElement")));
// Wait for element to be visible and clickable
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn")));
// Wait for title to match
wait.until(ExpectedConditions.titleIs("Expected Page Title"));public class FluentWait<T> implements Wait<T> {
/**
* Create FluentWait with input object
*/
public FluentWait(T input);
/**
* Create FluentWait with custom clock and sleeper
*/
public FluentWait(T input, Clock clock, Sleeper sleeper);
/**
* Set timeout duration (deprecated - use Duration version)
*/
public FluentWait<T> withTimeout(long duration, TimeUnit unit);
/**
* Set timeout duration
*/
public FluentWait<T> withTimeout(Duration timeout);
/**
* Set custom timeout message
*/
public FluentWait<T> withMessage(String message);
/**
* Set timeout message supplier
*/
public FluentWait<T> withMessage(Supplier<String> messageSupplier);
/**
* Set polling interval (deprecated - use Duration version)
*/
public FluentWait<T> pollingEvery(long duration, TimeUnit unit);
/**
* Set polling interval
*/
public FluentWait<T> pollingEvery(Duration interval);
/**
* Ignore collection of exception types during polling
*/
public <K extends Throwable> FluentWait<T> ignoreAll(Collection<Class<? extends K>> types);
/**
* Ignore single exception type during polling
*/
public FluentWait<T> ignoring(Class<? extends Throwable> exceptionType);
/**
* Ignore two exception types during polling
*/
public FluentWait<T> ignoring(Class<? extends Throwable> firstType, Class<? extends Throwable> secondType);
/**
* Wait until condition is true
*/
public <V> V until(Function<? super T, V> isTrue);
}import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.NoSuchElementException;
import java.time.Duration;
// Create custom FluentWait with detailed configuration
FluentWait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofMillis(500))
.ignoring(NoSuchElementException.class)
.ignoring(StaleElementReferenceException.class)
.withMessage("Element was not found within 30 seconds");
// Wait for custom condition
WebElement element = wait.until(driver -> {
WebElement el = driver.findElement(By.id("dynamic-element"));
return el.isDisplayed() && el.isEnabled() ? el : null;
});
// Wait for element attribute to have specific value
wait.until(driver -> {
WebElement el = driver.findElement(By.id("status"));
return "complete".equals(el.getAttribute("data-status"));
});The ExpectedConditions class provides a comprehensive set of predefined conditions for common waiting scenarios.
public class ExpectedConditions {
/**
* Wait for element to be present in DOM
*/
public static ExpectedCondition<WebElement> presenceOfElementLocated(By locator);
/**
* Wait for element to be visible
*/
public static ExpectedCondition<WebElement> visibilityOfElementLocated(By locator);
/**
* Wait for specific element to be visible
*/
public static ExpectedCondition<WebElement> visibilityOf(WebElement element);
/**
* Wait for all elements to be visible
*/
public static ExpectedCondition<List<WebElement>> visibilityOfAllElementsLocatedBy(By locator);
public static ExpectedCondition<List<WebElement>> visibilityOfAllElements(WebElement... elements);
public static ExpectedCondition<List<WebElement>> visibilityOfAllElements(List<WebElement> elements);
/**
* Wait for all elements to be present
*/
public static ExpectedCondition<List<WebElement>> presenceOfAllElementsLocatedBy(By locator);
}public class ExpectedConditions {
/**
* Wait for element to be clickable (visible and enabled)
*/
public static ExpectedCondition<WebElement> elementToBeClickable(By locator);
public static ExpectedCondition<WebElement> elementToBeClickable(WebElement element);
/**
* Wait for element to be selected
*/
public static ExpectedCondition<Boolean> elementToBeSelected(WebElement element);
public static ExpectedCondition<Boolean> elementToBeSelected(By locator);
/**
* Wait for element selection state to match expected value
*/
public static ExpectedCondition<Boolean> elementSelectionStateToBe(WebElement element, boolean selected);
public static ExpectedCondition<Boolean> elementSelectionStateToBe(By locator, boolean selected);
/**
* Wait for element to become stale (no longer attached to DOM)
*/
public static ExpectedCondition<Boolean> stalenessOf(WebElement element);
}public class ExpectedConditions {
/**
* Wait for element to be invisible or not present
*/
public static ExpectedCondition<Boolean> invisibilityOfElementLocated(By locator);
/**
* Wait for element with specific text to be invisible
*/
public static ExpectedCondition<Boolean> invisibilityOfElementWithText(By locator, String text);
/**
* Wait for specific element to be invisible
*/
public static ExpectedCondition<Boolean> invisibilityOf(WebElement element);
/**
* Wait for all elements to be invisible
*/
public static ExpectedCondition<Boolean> invisibilityOfAllElements(WebElement... elements);
public static ExpectedCondition<Boolean> invisibilityOfAllElements(List<WebElement> elements);
}public class ExpectedConditions {
/**
* Wait for element to contain specific text
*/
public static ExpectedCondition<Boolean> textToBePresentInElement(WebElement element, String text);
public static ExpectedCondition<Boolean> textToBePresentInElementLocated(By locator, String text);
/**
* Wait for element value attribute to contain text
*/
public static ExpectedCondition<Boolean> textToBePresentInElementValue(WebElement element, String text);
public static ExpectedCondition<Boolean> textToBePresentInElementValue(By locator, String text);
/**
* Wait for exact text match
*/
public static ExpectedCondition<Boolean> textToBe(By locator, String value);
/**
* Wait for text to match regex pattern
*/
public static ExpectedCondition<Boolean> textMatches(By locator, Pattern pattern);
/**
* Wait for attribute to have specific value
*/
public static ExpectedCondition<Boolean> attributeToBe(By locator, String attribute, String value);
public static ExpectedCondition<Boolean> attributeToBe(WebElement element, String attribute, String value);
/**
* Wait for attribute to contain value
*/
public static ExpectedCondition<Boolean> attributeContains(WebElement element, String attribute, String value);
public static ExpectedCondition<Boolean> attributeContains(By locator, String attribute, String value);
/**
* Wait for attribute to be non-empty
*/
public static ExpectedCondition<Boolean> attributeToBeNotEmpty(WebElement element, String attribute);
}public class ExpectedConditions {
/**
* Wait for page title conditions
*/
public static ExpectedCondition<Boolean> titleIs(String title);
public static ExpectedCondition<Boolean> titleContains(String title);
/**
* Wait for URL conditions
*/
public static ExpectedCondition<Boolean> urlToBe(String url);
public static ExpectedCondition<Boolean> urlContains(String fraction);
public static ExpectedCondition<Boolean> urlMatches(String regex);
/**
* Wait for specific number of windows
*/
public static ExpectedCondition<Boolean> numberOfWindowsToBe(int expectedNumberOfWindows);
/**
* Wait for alert to be present
*/
public static ExpectedCondition<Alert> alertIsPresent();
}public class ExpectedConditions {
/**
* Wait for frame to be available and switch to it
*/
public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(String frameLocator);
public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(By locator);
public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(int frameLocator);
public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(WebElement frameLocator);
}public class ExpectedConditions {
/**
* Wait for specific number of elements
*/
public static ExpectedCondition<List<WebElement>> numberOfElementsToBe(By locator, Integer number);
/**
* Wait for number of elements to be more than specified
*/
public static ExpectedCondition<List<WebElement>> numberOfElementsToBeMoreThan(By locator, Integer number);
/**
* Wait for number of elements to be less than specified
*/
public static ExpectedCondition<List<WebElement>> numberOfElementsToBeLessThan(By locator, Integer number);
}public class ExpectedConditions {
/**
* Wait for nested element visibility
*/
public static ExpectedCondition<List<WebElement>> visibilityOfNestedElementsLocatedBy(By parent, By childLocator);
public static ExpectedCondition<List<WebElement>> visibilityOfNestedElementsLocatedBy(WebElement element, By childLocator);
/**
* Wait for nested element presence
*/
public static ExpectedCondition<WebElement> presenceOfNestedElementLocatedBy(By locator, By childLocator);
public static ExpectedCondition<WebElement> presenceOfNestedElementLocatedBy(WebElement element, By childLocator);
/**
* Wait for nested elements presence
*/
public static ExpectedCondition<List<WebElement>> presenceOfNestedElementsLocatedBy(By parent, By childLocator);
}public class ExpectedConditions {
/**
* Logical NOT of condition
*/
public static ExpectedCondition<Boolean> not(ExpectedCondition<?> condition);
/**
* Logical OR of conditions (any condition can be true)
*/
public static ExpectedCondition<Boolean> or(ExpectedCondition<?>... conditions);
/**
* Logical AND of conditions (all conditions must be true)
*/
public static ExpectedCondition<Boolean> and(ExpectedCondition<?>... conditions);
/**
* Refresh condition on stale element exception
*/
public static <T> ExpectedCondition<T> refreshed(ExpectedCondition<T> condition);
}public class ExpectedConditions {
/**
* Wait for JavaScript to execute without throwing exceptions
*/
public static ExpectedCondition<Boolean> javaScriptThrowsNoExceptions(String javaScript);
/**
* Wait for JavaScript to return a non-null/non-empty value
*/
public static ExpectedCondition<Object> jsReturnsValue(String javaScript);
}public interface Sleeper {
/**
* Sleep for specified duration
*/
void sleep(Duration duration) throws InterruptedException;
/**
* Default system sleeper implementation
*/
Sleeper SYSTEM_SLEEPER = duration -> Thread.sleep(duration.toMillis());
}// Custom condition for element to have specific CSS class
public class CustomConditions {
public static ExpectedCondition<Boolean> elementToHaveClass(WebElement element, String cssClass) {
return driver -> {
String classes = element.getAttribute("class");
return classes != null && classes.contains(cssClass);
};
}
public static ExpectedCondition<Boolean> pageLoadComplete() {
return driver -> {
JavascriptExecutor js = (JavascriptExecutor) driver;
return "complete".equals(js.executeScript("return document.readyState"));
};
}
}
// Usage
wait.until(CustomConditions.elementToHaveClass(element, "active"));
wait.until(CustomConditions.pageLoadComplete());// Wait for multiple conditions
WebDriverWait wait = new WebDriverWait(driver, 20);
// Wait for element to be both visible and contain specific text
wait.until(ExpectedConditions.and(
ExpectedConditions.visibilityOfElementLocated(By.id("message")),
ExpectedConditions.textToBePresentInElementLocated(By.id("message"), "Success")
));
// Wait for any of several possible error messages
wait.until(ExpectedConditions.or(
ExpectedConditions.visibilityOfElementLocated(By.className("error-message")),
ExpectedConditions.visibilityOfElementLocated(By.className("warning-message")),
ExpectedConditions.visibilityOfElementLocated(By.id("alert-box"))
));FluentWait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(1))
.ignoring(NoSuchElementException.class)
.ignoring(StaleElementReferenceException.class)
.withMessage("Custom timeout message");
try {
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("dynamic-element")));
} catch (TimeoutException e) {
// Handle timeout - element never appeared
System.err.println("Element not found within timeout: " + e.getMessage());
}Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-support