Real World Example

Add to Cart on a Busy Sale Day

During a flash sale, Flipkart- or Amazon-style sites are slower, and the cart badge updates through an AJAX call that can take 1 to 4 seconds. The QA team replaced every sleep in the cart tests with explicit waits:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));

driver.get("https://shopease.example.com/products/101");
wait.until(ExpectedConditions.elementToBeClickable(By.id("add-to-cart"))).click();
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".spinner")));
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("cart-count"), "1"));

System.out.println("Cart count: " + driver.findElement(By.id("cart-count")).getText());

Example

Output:

mvn test -Dtest=CartTest

Example output

Cart count: 1
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 21.08 s

The Result

Before (Thread.sleep)After (explicit waits)
Cart suite time3 min 40 s1 min 10 s
Flaky failures per week90-1