Synchronization meaning: when two or more components involved to perform any action, we expect these components to work together with the same pace. The co-ordination between these components to run paralelly is called Synchronization.
Synchronization (Wait) in Selenium has a great significant value.
Now a day, we see most of the web applications are developed by using Javacript and Ajax where it might happen that some of the elements may load at distant time intervals, due to which we may encounter “ElementNotVisibleException” or “NoSuchElementException” exceptions. In such scenarios, we use Synchronization/wait to overcome these kinds of exceptions.
Automation Testing is commonly comprising of following two segments:
- Application Under Test
- Test Automation Tool.
We must write our test scripts in such a way that these two segments move with same desired pace, because both have their own agility. Hence, to avoid Errors which will exhaust time in debugging.
Synchronization can be classified into two categories:
- Unconditional Synchronization
- Conditional Synchronization
Unconditional synchronization indicates timeout value alone and makes tool to continue further only when specified time is elapsed. One such functions is:
This statement is helpful only when we are interacting with third party systems like interfaces because it is not possible to write conditions to check the status. Now, we want tool to wait for certain amount of time and then perform the next actions sequence.
This statement makes the tool to wait for certain time even though the application is ready, thus increasing the chance of unnecessary wait and hence giving the scope to Conditional Synchronization.
Conditional Synchronization: By using conditional synchronization we can specify timeout duration along with some desired conditions to check and then throw an error if nothing happens.
Types of Conditional Synchronization:
- Implicit Wait
- Explicit Wait
Implicit Wait: Implicit Wait tells the WebDriver to Wait until the stated time before throwing the NoSuchElement/ElementNotVisible exception. Waiting time across the test script between each consecutive steps are taken by default. Hence, next testStep will execute only when the specified time is elapsed post executing the previous testStep.
The Implicit wait takes one argument: TimeSpan (timeToWait)
Eg: The wait time specified in Implicit Wait command is say 30seconds. The specified time (30seconds) is default for all the succeeding testSteps thus giving rise to execution time. To overcome this issue, Explicit waits are introduced to apply waits whenever it is required while executing the testSteps.
Explicit Wait: Explicit waits are very good to use when page loads dynamically. Explicit Wait tells the WebDriver to Wait until the specified condition is met or maximum time elapses before throwing NoSuchElement (or) ElementNotVisible Exceptions. Explicit waits are applied for the specified testStep in test script.
To use Explicit waits, we must first create instance for “WebDriverWait” class.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
The reference variable “wait” informs the WebDriver to wait until the Expected condition to occur (or) Wait for the specified time of 30seconds, whichever shows in first place before throwing an exception.
Following are the ExpectedConditions:
The Implicit wait is simple when compared to Explicit wait and can be declared in setup method of the test script in single line of code. However, the Implicit wait has drawbacks despite being easy and simple to apply. Hence rising scope to use Explicit Waits because these waits can be applied for a specified testStep in test script.