Synchronization Methods in Selenium

Kavindra Lunuwilage
7 min readMay 1, 2020

--

It’s 2020. If I must tell you what Selenium is, this is probably not for you and feel free to spare your precious few minutes elsewhere. But if you’re that person who is overwhelmed with Test Automation and compelled to get your hands dirty with coding stuff please read on. Let me have the pleasure of giving you some cool tips and explanations in a couple of minutes.

For quite a long time I had this question when explaining Selenium Synchronization Methods, it’s quite surprising the number of people who don’t have enough understanding of basic two WebDriver Waits: Explicit & Implicit. So let’s deep dive into Synchronization Methods in Selenium with detailed explain examples.

Let’s Unpack!!!!

What is Synchronization in Test Automation?

This is a mechanism which involves two or more components working parallel with each other. As an example, these two components will be application under test and the test automation tool.

Why Synchronization is an Essential Factor in Test Automation?

As I mentioned above, both the automation tool and the application under test will have specified speeds to run. When writing test scripts, as automation engineers we should write in a way such that both of these components will work at the same speed.

Nowadays most of the software is being developed with the latest technology stack. Due to this many more page loadings, more interactions, page sliders are available in the user interfaces. When the page is loading these elements may load at different time intervals.

Using Waits, these elements can get identified and resolve the ElementNotVisibleException easily.

The Usefulness of Synchronization?

The main reason is to avoid Element Not Found exception. This is the most common exception we come across when developing test automation scripts.

By understanding synchronization and applying them correctly will save your test running time and test failures very easily.

Synchronization Methods in Selenium WebDriver

There are two main types of synchronization categories. And these categories contain their own subcategories as well. Let’s deep dive and understand.

Selenium Synchronization Mechanism: Image

1. Unconditional Synchronization -

Keep waiting the WebDriver for a specified amount of time without listening to the DOM Elements.

This method is used when we need to interact with third-party applications and interfaces. But this is highly not recommended and as Automation Engineers, we should avoid using Thread.Sleep() method.

Thread.Sleep method is not a part of Selenium and it’s a static wait that will hold the execution for a specified time.

Thread.Sleep() keeps the WebDriver holding the time without having any idea about the DOM elements. This method is not responsible to wait until the element get identified by the WebDriver.

The main drawback of this methods is keeping the WebDriver in a holding for a number of seconds that is specified and start executing the script after waiting. The test execution time gets longer if this is used without proper understanding.

2. Conditional Synchronization -

Keep waiting the WebDriver for a specific time duration until the target element is available or then throw exceptions by listening to the DOM Elements.

The main difference of Conditional Synchronization is synchronization will wait to check the target element for a given time period by listening to the DOM and once the element gets identified, again start running the script or given an exception.

To give a further understanding, it’s like if you give 5 secs to wait in the Conditional Synchronization and the element is getting identified within 2 seconds, the driver will continue to run the script with the identified element without waiting the remaining 3 seconds as in unconditional synchronization.

Types of Conditional Synchronization Methods

Conditional Synchronization Categories

a) Implicit Wait –

Implicit Wait makes the WebDriver to wait for every element until the specified time before throwing an exception.

Implicit Wait is defined as a Global Wait timer. Once Implicit Wait is defined in the script, WebDriver will automatically wait for every element as a specified amount of time.

Implicit Wait can be declared globally in your scripts as below.

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECOND);

Implicit Wait declaration

In the above line of code, I have given the time to wait like 5 seconds and TimeUnit I have selected is Seconds. You can configure the time unit that aligned with your projects.

b) Explicit Wait (WebDriverWait) -

Explicit Wait makes your WebDriver to wait for a specific amount of time for a Target Element before throwing an exception. Using Explicit Wait you can target a specific element. Let’s take an example.

If you come across a scenario where one of your elements takes more than the implicit wait time in your application, Explicit Wait is a good option you can choose.

Explicit Wait can be achieved in two ways WebDriverWait & FluentWait. I will explain this in FluentWait section.

You may think, why can’t we increase the Implicit Timer without defining a new wait! Yes, you can do so, but that is not a good coding practice. If you do so the waiting time for every element get increase and cause a performance issues.

As an example, when you run a sequence of scripts at once, the system you are testing might get slow and take more time to load elements. Also, the test case relevant to loading elements says that every element should load within 5 sec of time. But if you increase the time, you cannot rely on your test case anymore! That will cause big issues when comes to a production environment.

Explicit Wait can be declared as below.

WebDriverWait explicitWait = new WebDriverWait(driver, 5); explicitWait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector(“span.promoInfo”)))

ExplicitWait Declaration

Define WebDriverWait class and inside this class add driver object and number of seconds to wait. To access methods present in WebDriverWait class, first, create an object of it. After that using the object access until a method to give your expected condition with the locator element.

c) Fluent Wait –

As I mentioned above Explicit Wait section, Explicit Wait can be achieved in two ways. They are :

1. WebDriverWait — Wait for the Web Element Repeatedly until timeouts.

2. FluentWait — Wait for the Web Element Repeatedly at a Regular Interval of time until the timeouts.

In FluentWait WebDriver keeps monitoring the DOM Elements in regular intervals of polling. Hereafter every polling duration starts to search for the element. FluentWaits can use to define a maximum amount of time to wait for a condition and also to increase the frequency with which the conditions are checked.

FluentWait can be declared as below.

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class);

FluentWait is one of the class which is implementing the Wait Interface. Then pass the WebDriver class to this implementation. After that add Timeout as duration with TimeUnit. Give the Polling Time Unit as an above code snippet.

Note : Selenium HQ page, FluentWait implementation is now deprecated with Selenium v3.11. I have used the new way of declaring the FluentWait.

Usage of FluentWait in Practical Example -

To explain the usage of FluentWait let’s take a Shopping Cart Checkout scenario as an example. Before checkout, you need to add a promo code then click on apply button. Once promo code is added, a message appears with the validity of the promo code. Also, the promo code text box and promo code validity message have the same Html attributes. In such a situation we can’t use ExplicitWait.

The reason is you can’t identify the promo valid message apart from promo text box due to similar attributes. If you use ExplicitWait here, it will listen to the DOM elements but cannot validate the change of attributes because that already exists. To avoid this type of scenario FluentWait comes into the picture. The polling interval listening can capture elements by intervals easily. (Actually, instead of FluentWait, ExplicitWait can be used in most of the scenarios)

Code Example for FluentWait()

Once fluentWait object is created using FluentWait() class, use that object to access until method as in the above snippet.

Conclusion -

The recommended way of using waits in Selenium is to use Implicit, Explicit and Fluent Waits. Using Thread.Sleep() is not acceptable and not a good coding standard.

The best way of using Selenium Synchronization is by combining Implicit & Explicit Waits in the code. According to my experience, Implicit and Explicit are the most frequently using and make automation smoothly.

Fluent Wait is not frequently used due to its complexity and need to be customized before using it. But it’s actually fun to do customizations and make your hands dirty!!

Happy Coding!!!

Stay Safe!!

--

--