Synchronization in Selenium WebDriver
Synchronization in Selenium WebDriver
Synchronization is the process of matching the speed of Selenium script execution with the speed of the application under test (AUT).
In general, Test Automation has two components
- Application Under Test( AUT )
- Test Automation Tool
Both these components will have their own speed. You should write our scripts in such a way that both the components should move with same and desired speed, so that we will not encounter “Element Not Found” errors which will consume time again in debugging. For example, web applications may take time to load elements (because of network delays, AJAX calls, or slow servers), Selenium tool needs to wait before interacting with elements.Otherwise, you’ll see errors like NoSuchElementException or ElementNotInteractableException.
Types of Synchronization
Synchronization can be classified into two broad categories. There are:
- Unconditional Synchronization
- Conditional Synchronization
Unconditional Synchronization
In this, you specify timeout value only. You will make the tool to wait until certain amount of time and then proceed further.
Examples: Wait() and Thread.Sleep();
Thread.sleep() (Not recommended)
The main disadvantage for the above statements are, there is a chance of unnecessary waiting time even though the application is ready. The timeout value is hardcoded on the automation script.
- Pauses execution for a fixed amount of time.
- This causes unnecessary delays if the element is already available.
The advantages are like in a situation where we interact for third party systems like interfaces, it is not possible to write a condition or check for a condition. Here in this situations, we have to make the application to wait for certain amount of time by specifying the timeout value.
Conditional Synchronization
We specify a condition along with timeout value, so that tool waits to check for the condition and then come out if nothing happens.
It is very important to set the timeout value in conditional synchronization, because the tool should proceed further instead of making the tool to wait for a particular condition to satisfy.
Types of Synchronization in Selenium
There are mainly three types: