?? Stop Flaky Tests! The Right Way to Use Selenium Wait Strategy in Commercial Projects

?? Stop Flaky Tests! The Right Way to Use Selenium Wait Strategy in Commercial Projects

? 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?

  • Selenium finds the button but throws ElementNotInteractableException when trying to click it.
  • The real issue (button not visible) is not clear from the error.
  • Instead of implicit waits, explicit waits ensure the button is visible before clicking.


? 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?

  • Selenium does not throw an error but clicking has no effect because the button is disabled.
  • There is no benefit in clicking a disabled button so we should control this situation.


? 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?

  • Selenium finds the element immediately but can't interact with it.
  • Setting a high implicit wait time (11s) is not a scalable solution when automating thousands of similar elements.


? 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.


要查看或添加评论,请登录

Przemek (Shemeck) Nojman的更多文章

其他会员也浏览了