?? Stop Flaky Tests! The Right Way to Use Selenium Wait Strategy in Commercial Projects
Przemek (Shemeck) Nojman
Test Automation Expert | Helping in Test Automation implementation | Java, Selenium WebDriver, Rest-Assured, JDBC,Junit, Jmeter
? Are your Selenium tests randomly failing?
? Do your tests break due to slow page loads?
? Are you mixing implicit and explicit waits, which makes you really confused ?
?? If yes, this article will show why explicit waits are the best strategy for stable and reliable test automation.
1?? Why Implicit Waits Cause More Harm Than Good
Many testers default to implicit waits, thinking they will stabilize test execution. However, they often lead to flaky tests, inconsistent execution times, and debugging nightmares.
Imagine you set an implicit wait of 10 seconds and try to click the following button:
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.findElement(By.id("button")).click();
Scenario 1: Hidden Button
<button id="button" style="display: none;">Hidden Button</button>
What Happens?
? Better Practice:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("button"))); button.click();
Scenario 2: Disabled Button
<button id="button" disabled>Disabled Button</button>
What Happens?
? Better Practice:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("button")));
button.click();
Scenario 3: Button Becomes Visible After 11 Seconds
<body onload="setTimeout(() => document.getElementById('button').style.display = 'block', 11000);"> <button id="button" style="display: none;">Hidden but visible after 11s</button> </body>
What Happens?
领英推荐
? Better Practice:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(11)); WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("button"))); button.click();
2?? The Core Issue with Implicit Waits
As you can see, setting one global implicit wait makes your tests unstable:
? waits only check for DOM presence, not visibility or interactability.
? You cannot control wait conditions for different elements.
? If you have thousands of web elements, you don’t know your final wait time.
? Setting a global wait time of 5 minutes to cover all cases would make test execution painfully slow.
3?? Why Explicit Waits Are the Best Choice
To solve all these problems, you should use explicit waits for each web element based on its condition.
? Waits only when needed (no global slowdowns).
? Works with conditions like elementToBeClickable() instead of just presence.
? Provides meaningful error messages (clear reasons for failures).
? Reduces flaky test failures (works reliably across browsers).
4?? Can We Mix Implicit & Explicit Waits?
? The Short Answer: No
?? Example of Mixed Waits Breaking a Test:
Imagine that you want to use explicit wait with a method visibilityOf(WebElement element) and wait 6 seconds for it because your button will be visible after 5 seconds.
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(6)); WebElement button = driver.findElement(By.id("delayedButton")); wait.until(ExpectedConditions.visibilityOf(button)).click();
In this example you will get exception: NoSuchElementException after 3 second. We will never perform explicit wait.
?? Correct Approach: Use correct Explicit Wait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(6));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("delayedButton"))).click();
5?? Summary: Why Explicit Waits Win in Selenium
?? Explicit waits are the best choice for reliable Selenium test automation because:
? They provide fine control over element conditions.
? They prevent flaky test failures.
? They improve test execution speed.
? They are the only scalable approach in commercial projects.