Creating Custom Expected Conditions in Selenium WebDriver Java
Janesh Kodikara
Experienced QA Leader | Test Automation | Performance Testing | Training
In Selenium WebDriver, built-in ExpectedConditions provide a great way to synchronize test execution with the state of web elements. However, real-world scenarios often demand more specific waiting conditions beyond the standard ones. This is where custom ExpectedConditions come into play.
Why Create Custom Expected Conditions?
Consider a scenario where a dropdown or expandable section gradually expands upon interaction. The built-in ExpectedConditions like visibilityOfElementLocated may not suffice, as they only ensure the element is visible but not necessarily fully expanded. To handle such cases, a custom ExpectedCondition is required to ensure the element reaches its final expanded state before proceeding.
Steps to Create a Custom Expected Condition
Here’s a version of a custom ExpectedCondition that ensures an element has fully expanded:
package com.pragmatic.selenium.examples.synchronisations;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
public class ElementHasExpandedFully implements ExpectedCondition<Boolean> {
private final By expandingElement;
private int lastHeight = -1;
public ElementHasExpandedFully(By expandingElement) {
this.expandingElement = expandingElement;
}
@Override
public Boolean apply(WebDriver webDriver) {
WebElement element = webDriver.findElement(expandingElement);
int newHeight = element.getSize().height;
if (newHeight > lastHeight) {
lastHeight = newHeight;
return false; // Still expanding
}
return true; // Fully expanded
}
}
Other Useful Custom Expected Conditions
领英推荐
public class AttributeToBe implements ExpectedCondition<Boolean> {
private final By elementLocator;
private final String attribute;
private final String expectedValue;
public AttributeToBe(By elementLocator, String attribute, String expectedValue) {
this.elementLocator = elementLocator;
this.attribute = attribute;
this.expectedValue = expectedValue;
}
@Override
public Boolean apply(WebDriver driver) {
return driver.findElement(elementLocator).getAttribute(attribute).equals(expectedValue);
}
}
Learn More
Explore more examples on our GitHub repository.
?? Excited to Announce Our Upcoming Selenium WebDriver Training Program!
Ready to take your automation testing skills to the next level? Join us for an 8-day advanced training focused on mastering Selenium WebDriver techniques with Java.
?? Dates: 8th March 2025 ? Timing: 1:00 PM – 5:00 PM IST ?? Format: Interactive, instructor-led online training
This paid program is perfect for professionals who want to: ?? Dive into advanced Selenium concepts ?? Tackle real-world automation challenges ?? Build robust frameworks with hands-on learning
? Registration is now open! Sign up here: Register Now ?? Course Content Details: Explore Here ?? Questions? Contact [email protected]
#Pragmatic #Selenium #AutomationTesting #SeleniumWebDriver #TestAutomation