Wait, Do Not Sleep()

Harith Sankalpa
5 min readAug 13, 2019

--

Reference : https://cdn-images-1.medium.com/max/800/1*Q3p1NPEuS2iYIbHbFfjyJA.jpeg

In test automation of web applications using Selenium, sometimes the test script may should be paused until an event occurs such as completion of a web page loading into the browser or until an element becoming clickable. If the test script does not wait for the document and perform action on it or click on the element before it becomes clickable, errors may occur at run time.

Sleep() As a Solution

How to use

The simplest solution for the above problem is to use sleep functions available in programming languages. For example,

Thread.sleep(millis); //millis is the sleep time in milliseconds.

in Java programming language can be used to pause the test script for a specific amount of time which can be the maximum amount of time which is allowed to load the page or element to become clickable. Although the test will fail if the web page had not been loaded completely even after the sleep time or element had not become clickable, it can marked as a fault in the program or network connection as to pass the test the web application must meet the required time requirements.

Problem

But the problem of using sleep functions in test scripts is even if the the expected event occurs before the sleep time passes,the test script will continue to sleep for the full amount of sleep time. For example, let’s imagine a sleep function is used to pause the test script which is used to automate tests in a web application, for 5 seconds which is the maximum time allowable until the web page loads completely. Then the test script will always continue to sleep for full 5 seconds when web pages loads. But if the web page loads completely in 2 seconds for the most times additional 3 seconds will be wasted.

This will significantly increase the test time unnecessarily and will add additional costs to the testing process.

A Better Solution — Selenium Waits

Reference : https://cdn.educba.com/academy/wp-content/uploads/2019/05/selenium-framework.png

A better solution for the original problem is available in Selenium, which is the dynamic waits available in the framework. Each of these types of waits will wait only until an specified event occurs and if the event does not occur within the wait time a variant of Selenium Timeout Exception will be thrown. The three main types of waits which can be used for different requirements are,

  1. Implicit Wait
  2. Explicit Waits
  3. Fluent Waits

Let’s look at each type of waits separately with details of when and how to use with examples from Java language.

1. Implicit wait

Implicit wait is a one time solution for NoSuchElement Exception. Once a certain amount of time is set as Implicit Wait after the driver instance created, the web driver will always wait the specified time amount polling(per around 250 millis) the DOM to get the specified element before throwing an exception saying cannot find an element in the web page. But if the element is found before the total amount of wait time elapses, web driver will not wait for the complete amount of time and continue running the test script. The following code presents how implicit wait can be set when using Java and Chrome as the web browser.

A possible problem that may occur when using a single wait time for all the elements is that you may not want to wait the large amount of time, taken to load the page when a dynamic element loads. Or you may want to wait for an existing element to become clickable or selectable. For those cases we can use Explicit waits and Fluent waits.

2. Explicit Waits

As mentioned before, Explicit wait can be used to specify a wait time for a specific element to become visible or selectable or clickable or any other any other condition to become to be true. The same wait object can be used multiple times in a single thread for different elements and different conditions to become true which helps to save memory. Selenium provides ready made conditions for frequently used conditions such as to wait for visibility, click-ability of an element in ExpectedConditions class so you don’t have to code them yourself. How cool is that?

The following example presents how Explicit Wait can be used to wait for an element to become clickable.

In addition to the timeout, we can specify the sleep time in constructor as the third argument which represents per how much time the web driver should be polling the DOM.

3. Fluent Waits

Fluent waits can be used for the same requirements as same as the ones Explicit Waits used for. And also same conditions available in ExpectedConditions class can be used in Fluent Wait. But in Explicit wait, we must provide timeout in full seconds and sleep time in milliseconds. As a solution we can use Fluent Wait where we can define timeout and sleep time in any time unit. Another advantage that Fluent Waits have over Explicit Wait is that we can define specific Exceptions such as NoSuchElementException to be ignored when waiting for the condition to become true.

The following example demonstrate how to wait for an element to become available and return it once it’s available. You can note that timeout is defined in milliseconds and sleep time (polling interval) is defined in nanoseconds.

Notes :-

  • When working on a real world project, Implicit Wait and Explicit Waits are often preferred than Fluent Waits.
  • Never use Implicit Wait and Explicit Waits together as it may lead to unexpected wait times.

Conclusion

As discussed in this article, although using sleep functions is the simplest solution to pause a test script until an event occurs, it may lead time resource to be underutilized and unnecessary costs to be added to test automation. As a solution we can use three types of dynamic waits Implicit wait,Explicit waits and Fluent waits which do not wait for the specified full wait time but only until the event occurs available in Selenium according to different requirements.

--

--

Harith Sankalpa
Harith Sankalpa

Written by Harith Sankalpa

Software Engineer | Tech Enthusiast | Gamer | Photographer

No responses yet