Skip to main content

Implicit vs Explicit Waits

In order to make our Selenium tests resilient, we need to make them wait for certain elements to load. Elements that we want to interact with. This is especially true with JavaScript heavy pages.

Intermediatetroubleshooting5 tools

Implicit vs Explicit Waits

Intro​

In order to make our Selenium tests resilient, we need to make them wait for certain elements to load. Elements that we want to interact with. This is especially true with JavaScript heavy pages.

But should we be using static sleeps, implicit waits, explicit waits?

Static sleeps are something to be avoided, because they force your tests to wait a hard-coded amount of time to perform an action -- this is not recommended. But what about implicit waits, explicit waits... and what about using them together?

A Solution​

The short answer? Use explicit waits.

Adding an implicit wait to your Selenium tests is simple enough and may seem intuitive. But there are cases where it simply doesn't help you like you think it will. And while you can combine explicit and implicit waits together (e.g., override an implicit wait with an explicit wait to make a Selenium action wait longer) -- you shouldn't. I'll discuss more on that later.

Let's continue with some examples.

Example​

Let's step through some examples that deal with dynamically loaded content available on the-internet. Both examples have a Start button that, when clicked, shows a loading bar for 5 seconds and then displays Hello World!. In the first example the finish element is already on the page, just hidden. In the second one it gets rendered after the loading bar.

An implicit wait tells Selenium to retry each find element action for up to a set amount of time (3 seconds in our examples). If it can find the element in that amount of time, it proceeds. If it can't, it throws a NoSuchElement exception. Our first two tests show where that falls short:

  • In the second example, finding the finish element fails. The implicit wait gives up after 3 seconds, but the element is rendered after 5. We could increase the implicit wait, but that would impact all the find element actions in our tests, making every failure slow to report.
  • In the first example, finding the finish element succeeds right away, since it's already on the page. But it's still hidden. An implicit wait only waits for elements to be present, not for them to be displayed.

Our last test uses an explicit wait instead, in both examples. We tell Selenium what we're waiting for (the finish element to be displayed) and for how long (up to 10 seconds), only where we need it. Then we can assert its text.

On Not Mixing Explicit and Implicit Waits​

If your test suite uses both explicit and implicit waits, then you're in for some pain (e.g., unpredictable wait times and transient failures as you scale your test suite). For more details about this, check out this StackOverflow answer from Jim Evans (a member of the Selenium core team). That's why the test with the explicit wait doesn't set an implicit wait.

Expected Behavior​

  • Open the browser
  • Visit the page
  • Click the Start button
  • With an implicit wait: fail to find the element (second example), or find it while it's still hidden (first example)
  • With an explicit wait: wait for the loading bar to disappear and the finish text to appear, and assert it
  • Close the browser

Summary​

While an implicit wait can be useful, providing you an initial blanket of cover, it's not ideal for every circumstance. Instead, explicit waits are a better tool for the job since they provide more resilient and predictable results.

Regardless of the approach you choose, be sure never to mix implicit and explicit waits together.

Happy Testing!

Authors​

Last updated on by Diego Molina
Next tipHow to Turn a Selenium Test into a Load TestRead next