Selenium Webdriver - Synchronization

When two or more components have to work parallely with each other, the mechanism enabling this is called as synchronization. When a software test is automated, there are two components involved in it, the application under test and the test automation tool. Both these components have their own speeds and the test scripts should be written in such a way that both these components will work with the same desired speed. If not the same speed the chances for “Element Not Found” errors which will take more time for debugging and repeating the tests.

As you are already aware, Selenium webdriver is most widely used nowadays for automating the tests of web based applications. Here there is a need for Selenium Webdriver synchronization. Synchronization can be classified into two categories, Unconditional Synchronization and Conditional Synchronisation.

Unconditional Synchronization

In this case, only the timeout value will be specified. This will make the tool to wait for the specified period of time and then it will proceed automatically.

Examples: Wait(), Thread.Sleep()

The major disadvantage in this case is that the tool may wait unnecessarily for a longer period even when the application is ready. However, it will be highly useful when we have to interact with third party systems such as interfaces. Here, writing condition or checking for the condition is not possible and the only possibility is to specify a time limit for the tool to wait before proceeding with execution.

Conditional Synchronization

In this case, a condition will be specified along with setting a timeout value so that the system will check if the condition is true and proceeds when it is true. It is necessary to set a time limit also to avoid the system for waiting endlessly to meeting the condition.

In Selenium webdriver, there are two conditional wait statements such as Explicit wait and Implicit wait.

Implicit Wait

In implicit wait Selenium webdriver will be told to poll the DOM for a specific number of times to find an element or elements if they are not readily available. Here ‘0’ is set by default and when the implicit wait is defined, it will set for the life of the Selenium Webdriver object instance.

This mechanism will be written only once and used for the entire session and should be applied immediately after the initiation of webdriver. However, implicit wait will not work for all commands but only for “Find Element” and “Find Elements” commands. When implicit wait is set, it will not throw the “Element Not Found” error at the first instance but will poll for the element until the time limit expires. Then it will proceed further.

Explicit Wait

Here, a condition should be specified, which should be met within a specific period of time. If the webdriver meets this condition within the specific time period, the code will get executed. When we will have to wait for a specific content or attribute to get changed, this wait can be the best option.

Leave a Reply

Your email address will not be published. Required fields are marked *