All about WAITS, please don’t SLEEP!

HARSHVARDHAN SINGH CHAUHAN
3 min readOct 9, 2021

--

Waits play a very important role while doing UI automation. It is because there are certain elements which get loaded on the page asynchronously.
This means even after the page gets loaded successfully, some of its elements are still not loaded. This in turn leads to the exceptions like
NoSuchElementException, TimeOutException etc.

This can be handled using multiple kinds of waits which are categorized into two:

  1. Static Wait
  2. Dynamic Wait

STATIC WAIT:

It pauses the execution for a given time, irrespective of whether the element is found or not on the web page. There are sleep methods available in programming languages like the below one which is available in Java. It accepts the time duration in milliseconds.

DYNAMIC WAIT:

There are 4 different types of dynamic waits in Selenium:

  1. PAGELOADTIMEOUT

If the page load takes more time than the webdriver speed, then the TimeOutException is thrown by the WebDriver. This problem can be solved using PageLoadTimeout.

2. IMPLICIT WAIT

Implicit wait allows us to halt the WebDriver for a particular period of time until the WebDriver locates a desired element on the web page. In case it finds the element before the duration specified, it moves on to the next line of code execution. Implicit wait is applied globally, which means we only need to write it one time and it gets applicable for all of the web elements specified in a script throughout the WebDriver instance.

3. EXPLICIT WAIT

It helps us to stop the execution of the script based on a certain condition for a specified amount of time. Few types of expected conditions are:

a. visibilityOfElementLocated()- Verifies if the given element is present or not.
b. alertIsPresent()- Verifies if the alert is present or not.
c. elementToBeClickable()- Verifies if the given element is present/clickable on the screen.
d. textToBePresentInElement() — Verifies the given element have the required text or not.

There are many more which can be referred in the official Selenium page below:

4. FLUENT WAIT

It is more or less similar to explicit wait with a couple of additional features which are:

a. The polling frequency: Here we can change the polling frequency which is otherwise 500 milliseconds in case of Explicit Wait.
b. Ignore Exception: During polling, if we do not find an element, we can ignore any exception like “NoSuchElement” exception.

WHY WE SHOULD NOT MIX DIFFERENT TYPES OF WAITS?

Mixing two different types of waits can cause unpredictable wait times. In fact, use of Implicit Waits is officially discouraged.

Simon Stewart says “The advice of the selenium team has always been to try and minimize the timeout used (preferably to 0)”. This is
also stated as warning in the Selenium 4 documentation.

Selenium Waits helps us to make our scripts less flaky and more reliable.
Happy Testing!

--

--

HARSHVARDHAN SINGH CHAUHAN
HARSHVARDHAN SINGH CHAUHAN

Written by HARSHVARDHAN SINGH CHAUHAN

An avid reader, spoken word artist, dog lover, explorer, full stack qa engineer...

No responses yet