Wait, Selenium wait!: A brief comparison between Implicit, Explicit and Fluent Waits in Selenium WebDriver

Selenium WebDriver provides a programming interface to create and execute test cases for almost every web application. For this aim Selenium needs to interact with elements on webpages either in an end-to-end testing scenario or individual functional testing. In both cases, elements within a webpage may load at different time intervals for various reasons.

To better understand this topic, we need to distinguish difference between these scenarios:

An element not being present at all in the DOM.

An element being present in the DOM but not visible.

An element being present in the DOM but not enabled. (i.e. clickable)

 

Selenium provides different kind of “wait” for locating elements or perform an action or operation on them.

 

Implicit Wait

By using implicit wait we tell Selenium to wait for a certain amount of time before throwing NoSuchElementException. We should note that implicit waits will be in place for the entire time the browser is open so any search for elements on the page could take the time the implicit wait is set for.

During Implicit wait if the Web Driver cannot find it immediately because of its availability, it will keep polling (around 0.250 seconds) the DOM to get the element. If the element is not available within the specified Time an NoSuchElementException will be raised. The default setting is zero. Once we set a time, the Web Driver waits for the period of the WebDriver object instance.

 

Explicit Wait

By implementing an Explicit wait, we can set it up to wait for any condition we want. Mostly, we use some of the prebuilt ExpectedConditions to wait for elements to become clickable, visible, or enable.

 

Fluent Wait

When we need to deal with a page that has an element which appears in different times we may want to use a Fluent wait to located it. A fluent wait will try to find the element again and again until it finds it or until the final timer’s end up. Fluent Wait uses two parameters – timeout value and polling frequency.

Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Also, we can set the wait to ignore specific types of exceptions when searching for an element on the page.

 

Sleep:

Sleep method is not a built-in method exclusively for selenium. Each programming language got its own method for this. Sleep method could be a good choice when we want to emulate a real user behavior. Most of times, a user needs some seconds to find an element on page (such as textbox, button, dropdown menu, etc.) to interact with them.

The method time.sleep(n) stop the test for the given (n) seconds. The argument may be a floating point number to indicate an exact sleep time. Using sleep will definitely increase the executing time of the test so you should use it wisely based on your need.

 

Which one these kids is better?!

Choosing between Webdriver Wait command depends on the type of the web application we want to test. For interacting with AJAX web pages it’s better to make use of Fluent Wait command as this wait tries to find the web element repeatedly at regular intervals until the timeout or till the object gets found.

Personally I prefer using explicit and fluent wait for functional testing but for implementing a complete End-to-End scenario I use them with many Sleep time to consider user’s behavior.

 

P.S: Talking about explicit and implicit things, reminded me to mention amazing “The Zen of Python” too which indicates:

 

Beautiful is better than ugly.
>>Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
...

 

Maybe that’s why I am in love in Python and Explicit things in life!

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.