When testing a software there are instances where two or more components will have to work concurrently and parallel with each other. When you automate the test, there are two components such as the software application that is to be tested and the test automation tool that is used for executing the test.
Both these components will have their own speed and the test scripts should be written facilitating both these components to work with the same speed. If they have not worked with the same speed the chances for “Element Not Found” errors are more. In such cases synchronization will help both the components to work with the same speed.
When you consider the synchronization in Selenium Webdriver, there are two different types of synchronizations.
In unconditional synchronization, only timeout value will be specified. Selenium webdriver will wait till the specified time and then start executing the test automatically. Below given are the two examples.
Since only timeout is mentioned, the tool will wait unnecessarily till the time is out even when the conditions are met. However, this type of synchronization will be helpful while interacting with third party system such as an interface. While interacting with third party system writing conditions or checking conditions is not possible. Here the tool can be made to wait for a specified period.
In Conditional Synchronization one or more conditions will be set along with a timeout limit. Here the tool will start working as soon as the condition or the conditions are met. When the condition or the conditions are not met, the tool will wait till the specified time and then proceed to execute the test.
There are two types of conditional statements is selenium webdriver. Explicit wait and implicit wait.
Usually, implicit wait is being used when the element or the elements are not available readily. This wait will work only for commands such as “Find Element” and “Find Elements” and not for any other statements.
Example for Implicit wait
WebDriver driver = new FirefoxDiriver();
Here a condition will be specified for a wait statement along with a specified time limit. The condition should be met within the specified time limit. The testing will start proceeding when the condition is not met within the specified period of time.
Example for Explicit wait
/*Explicit wait for state dropdown field*/
WebDriverWait wait = new WebDriverWait(driver, 10);
This is used when a maximum amount of time to be allowed for a condition to met and also when the frequency of checking conditions is more.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
//Wait for the condition
// which to check for the condition with interval of 5 seconds.
//Which will ignore the NoSuchElementException