Chapter 3: Handling Waits and Synchronization
By Shivam Singh, founder of TesterRank · Updated September 2026
An explicit wait waits for one specific condition on one specific step, such as "wait until the Login button is clickable". It checks the condition again and again (every 500 ms by default) and continues as soon as it is true. If the condition is still false after the timeout, it throws a TimeoutException with a clear message.
Explicit wait is the industry gold standard for test synchronization. With WebDriverWait, you tell the driver to wait for a specific condition on a specific element before proceeding. It evaluates conditions dynamically, returning the instant the condition is satisfied.
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// 1. Wait until element is VISIBLE (drawn on screen, non-zero height/width)
WebElement welcomeMsg = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("welcome-message"))
);
// 2. Wait until button is CLICKABLE (visible AND enabled)
WebElement submitBtn = wait.until(
ExpectedConditions.elementToBeClickable(By.id("confirm-transfer-btn"))
);
submitBtn.click();
// 3. Wait until dynamic text updates
wait.until(
ExpectedConditions.textToBePresentInElementLocated(
By.id("transaction-status"), "Transfer Completed Successfully"
)
);| Condition Method | Evaluates | Return Type | Production Use Case |
|---|---|---|---|
| elementToBeClickable(locator) | Visible AND enabled in DOM | WebElement | Buttons, links, checkboxes before click() |
| visibilityOfElementLocated(locator) | Visible in DOM (height/width > 0) | WebElement | Reading text, labels, verifying headers |
| invisibilityOfElementLocated(locator) | Hidden or detached from DOM | Boolean | Waiting for loading spinners or progress modals to close |
| presenceOfElementLocated(locator) | Present in DOM (may be hidden) | WebElement | Extracting hidden CSRF tokens or input values |
| textToBePresentInElementLocated(loc, text) | Element contains specific string | Boolean | Waiting for asynchronous currency conversions or totals |
| urlContains(fraction) | Current browser URL contains substring | Boolean | Verifying successful page redirects after login |
ExpectedConditions.and() waits until every condition is true; or() waits until any one is true. This helps when a page can end in more than one state.
// Logical AND — Wait until element is visible AND URL has redirected to dashboard
wait.until(ExpectedConditions.and(
ExpectedConditions.visibilityOfElementLocated(By.id("account-summary")),
ExpectedConditions.urlContains("/banking/dashboard")
));
// Logical OR — Wait for either success banner OR error notification
wait.until(ExpectedConditions.or(
ExpectedConditions.visibilityOfElementLocated(By.id("transfer-success")),
ExpectedConditions.visibilityOfElementLocated(By.id("transfer-failed"))
));
// Logical NOT — Wait until loading bar is NOT visible
wait.until(ExpectedConditions.not(
ExpectedConditions.visibilityOfElementLocated(By.className("loading-bar"))
));| You want to… | Use this condition |
|---|---|
| Click a button | elementToBeClickable |
| Read text from an element | visibilityOfElementLocated |
| Wait for a loader to go away | invisibilityOfElementLocated |
| Wait for a new page | urlContains or titleContains |
| Wait for an alert | alertIsPresent |
Picking the wrong condition is the most common cause of flaky waits. For example, presenceOfElementLocated only checks that the element is in the HTML. A button can be present but still hidden behind a loading overlay, so clicking it fails. Match the condition to the action you are about to do.
Create one WebDriverWait in your base page class and reuse it. Repeating new WebDriverWait(driver, Duration.ofSeconds(10)) on every line makes timeouts hard to change later.
Key Point: WebDriverWait + ExpectedConditions provides precise, targeted synchronization. Use visibilityOfElementLocated for reading and elementToBeClickable for clicking.