Appium and WebDriverIO - Implicit and Explicit Waits
Exit course

Implicit and Explicit Waits

A large part of writing tests is to make sure you wait for specific actions to be performed, before moving on to the next (test) logic. There are two different types of waits that you can use with Appium and WebDriverIO. Below we'll highlight the differences between the two techniques and describe when you should use one or the other.

Use of Waits

Implicit Waits

Implicit waits are global waits applied by default to all elements on a page or screen. These instruct underlying automation service to wait for a specified amount of time for an element to appear. This wait is set once and is automatically applied to every interaction in the test case. If the duration of the waits is exceeded without a result, an exception will be thrown.

Example code using WebDriverIO:

Copy code
// Here we are setting an implicit wait of 5 seconds
browser.setTimeout({ 'implicit': 5000 })

// Locating an element and performing an action
const myElement = $('.my-element')
myElement.click()

Explicit Waits

Unlike implicit waits, explicit waits are applied selectively to specific elements or conditions in your test case. This type of wait allows you to define the conditions under which the test should proceed. This means greater control and flexibility for your test logic. This will poll the condition you specify in the waitUntil method, until it returns true or until the timeout exceeds.

Copy code
const { until } = require('webdriverio')

// Locate an element and wait for it to be clickable
const myElement = $('.my-element')
await browser.waitUntil(
  async () => await myElement.isClickable(),
  {
    timeout: 2000,
    timeoutMsg: 'Expected element to be clickable within 2 seconds'
  }
)