Synchronization is one of the most important operations that every automation engineers have to perform. Synchronization helps to stabilize your entire test automation framework. There are lots of technologies run parallel to any web application, like JavaScript, AJAX, jQuery, Flash players, PHPMyAdmin, etc. So whenever any user query any data on the web application then it takes time in sending and accepting response and finally it displays the results after so much filter to provide us appropriate results. Technologies are getting more advanced with the introduction of Artificial Intelligence, Cloud Computing, and the Internet of Things. Therefore, website loading speed really matters. Just to make everything work properly, Selenium offers us WebDriver Wait class to apply different synchronization to make our Selenium test automation cases stable and robust.
WebDriver Advanced Uses Declaration – Official SeleniumHQ
What is Synchronization in Selenium?
In Selenium, we have two components mainly. First one is, Application Under Test and Second one is, Selenium itself as a Test Automation tool. Hence, Synchronization is all about making both these components work together parallel, without any race, not even rat race.
Hope you got my points. Let me clear the confusion with some real-life example. Suppose we are in Marathon and we have to reach the finishing point as early as possible. So we can only reach the finishing line to the earliest when we will have great Stamina, right? So how does great stamina come? Stamina is all about when heartbeat as well as breath is altogether synchronized with the movement of the leg then we could definitely reach the finishing mark.
Above is the metaphor used to explain, the synchronization in Selenium. Here in Selenium, Stamina is actually WebDriver Wait, so WebDriver wait gives your test case a kind of stamina and potential to achieve the synchronization between Application under test (AUT) and Test automation tool (TAT).
What is WebDriver Wait in Selenium?
WebDriver Wait is a class in Selenium WebDriver, which incorporates different types of conditions to facilitate the synchronization between test application and test automation tool. These conditions will be discussed exclusively when we will discuss Explicit wait in upcoming blogs posts.
We initialize WebDriver Wait class as below:
WebDriverWait wait for new WebDriverWait(driver, timeStamp);
Classification of Synchronization
Synchronization is classified into two types, which are listed as follows:
- Unconditional Synchronization
- Conditional Synchronization
Let’s discuss each of the synchronization technique one by one.
1. Unconditional Synchronization
This type of synchronization is used when we don’t have any condition to choose, so we simply hardcode time in the command and set the system for the run. When web driver reaches to the unconditional synchronization command then selenium stops further execution after defined time duration.
Unconditional synchronization has some Pros and Cons, let’s have a look at it:
Pros:
- Best fit, when we have to inject any third party command or functionality
- Skip failure condition which arises in conditional synchronization
Cons:
- Sometimes it takes more time when application loads early than expected
- Execution time unnecessarily delays
List of various Unconditional Synchronization commands
There are some commands which facilitate unconditional synchronization, which is as follows:
thread.sleep()
It forcibly injects waits in the program. We can’t say this command is good or bad because it has its own importance because I have used this command in many of my projects and it really helped a lot in certain situations for my test automation scenarios.
thread.sleep(time in millisecond);
setScriptTimeOut()
This wait command is used to give time delay in asynchronous scenarios. It throws an error when it fails. It runs to an infinite times duration when we enter negative time length.
driver.manage().timeouts().setScriptTimeout(time value, timeUNIT);
pageLoadTimeOut()
This wait command waits for the page to load, once the time expires then it throws an error.
driver.manage().timeouts().pageLoadTimeout(timeValue, timeUNIT);
2. Conditional Synchronization
This synchronization technique works opposite to the previous one. It gives wait time but with a certain condition. If the condition fails then it throws TimeOutException.
There are mainly three types of conditional synchronization, which are described briefly as follows:
Implicit Wait
This conditional wait is used to wait until “FindElements” is able to find the web element. Here is the command for the implicit wait.
driver.manage().timeouts().implicitlyWait(timeValue, timeUNIT);
Explicit Wait
The explicit wait is used to wait by specifying certain conditions. WebDriver throws timeout exception when it fails after reaching the wait time. Here is the demo command for the explicit wait:
WebDriverWait wait = new WebDriverWait(driver, 20); WebElement ele = wait.until(ExpectedConditions.VisibilityOfAllElements(By.xpath(xpath_value));
Fluent Wait
We call it a smart wait. It also uses certain conditions with a time stamp. Additionally, it has polling time frequency to check for the condition.
Here is the command for the Fluent wait:
Wait<WebDriver> wait = new FluentWait<WebDriver> (driver) .withTimeOut(20, TimeUnit.SECONDS) .pollingEvery(2, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class);
These three conditional waits will be discussed separately in exclusive posts.