ExpectedConditions class in Selenium Java - Build an Automation Script
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.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; public class ExpectedConditionsTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/startpage"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement submitButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn"))); submitButton.click(); wait.until(ExpectedConditions.urlToBe("https://example.com/nextpage")); String currentUrl = driver.getCurrentUrl(); if (!currentUrl.equals("https://example.com/nextpage")) { throw new AssertionError("URL after click is not as expected. Found: " + currentUrl); } System.out.println("Test Passed: Button clicked after becoming clickable and URL verified."); } catch (Exception e) { e.printStackTrace(); } finally { driver.quit(); } } }
This code uses Selenium WebDriver with Java to automate the test case.
First, it opens the browser and navigates to the start page.
Then, it creates a WebDriverWait object with a 10-second timeout.
It waits explicitly for the button with id submitBtn to become clickable using ExpectedConditions.elementToBeClickable.
Once clickable, it clicks the button.
After clicking, it waits until the URL changes to the expected next page URL using ExpectedConditions.urlToBe.
It then asserts that the current URL matches the expected URL.
If all steps pass, it prints a success message.
The try-finally block ensures the browser closes even if an error occurs.
Now add data-driven testing to wait for and click buttons with ids 'submitBtn1', 'submitBtn2', and 'submitBtn3' on the same page, verifying navigation after each click.
Practice
ExpectedConditions class in Selenium WebDriver?Solution
Step 1: Understand the role of ExpectedConditions
The ExpectedConditions class provides predefined conditions to wait for, such as visibility or clickability of elements.Step 2: Compare with other Selenium functions
Locating elements, navigation, and JavaScript execution are handled by other classes or methods, not ExpectedConditions.Final Answer:
To wait for certain conditions or states of web elements before proceeding -> Option CQuick Check:
ExpectedConditions = wait for element states [OK]
- Confusing ExpectedConditions with element locators
- Thinking it performs actions like clicking
- Assuming it runs JavaScript
By.id("submit") is clickable using ExpectedConditions and WebDriverWait?Solution
Step 1: Identify correct WebDriverWait constructor and method
The correct way is to create a new WebDriverWait object and call theuntilmethod.Step 2: Check ExpectedConditions method name
The method iselementToBeClickable, notelementIsClickable, and the locator is passed correctly.Final Answer:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("submit"))); -> Option BQuick Check:
Use new WebDriverWait + until + elementToBeClickable [OK]
- Using wait() instead of until()
- Wrong method name like elementIsClickable
- Not creating new WebDriverWait instance
WebDriverWait wait = new WebDriverWait(driver, 5);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".login-button")));
element.click();What happens if the element with class
login-button is not visible within 5 seconds?Solution
Step 1: Understand behavior of WebDriverWait with ExpectedConditions
If the condition is not met within the timeout, WebDriverWait throws a TimeoutException.Step 2: Analyze the code flow
Since the element is not visible within 5 seconds,wait.untilthrows an exception, soelement.click()is not executed.Final Answer:
A TimeoutException is thrown and the test fails -> Option DQuick Check:
TimeoutException if condition unmet in wait [OK]
- Assuming click happens even if element not visible
- Thinking wait ignores failure silently
- Believing test passes without clicking
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated("#username"));Solution
Step 1: Check the argument type for presenceOfElementLocated
This method requires aBylocator, not a plain string.Step 2: Verify the locator usage
Instead of passing "#username", it should beBy.cssSelector("#username")or another By method.Final Answer:
The locator should be a By object, not a String -> Option AQuick Check:
presenceOfElementLocated needs By locator [OK]
- Passing string selector instead of By object
- Confusing timeout units (seconds vs milliseconds)
- Thinking ExpectedConditions methods don't exist
loading disappears or a maximum of 15 seconds passes before clicking a button with id continue. Which code snippet correctly uses ExpectedConditions to achieve this?Solution
Step 1: Identify the condition to wait for
The requirement is to wait until the element with id "loading" disappears, which matchesinvisibilityOfElementLocated.Step 2: Confirm the action after wait
After waiting, the code clicks the button with id "continue" as required.Final Answer:
new WebDriverWait(driver, 15).until(ExpectedConditions.invisibilityOfElementLocated(By.id("loading"))); driver.findElement(By.id("continue")).click(); -> Option AQuick Check:
Wait for invisibility, then click continue [OK]
- Waiting for visibility instead of invisibility
- Clicking wrong element after wait
- Using presenceOfElementLocated for disappearance
Ready to prove it?
Pass the test and this pattern moves unknown
