Synchronization in Selenium WebDriver
- Synchronization is mechanism that helps to use time periods (or waits) for web pages, objects to sync with tests and tests executions.
- There are some exceptions, like, “Element Not Found”, “No such element found” , “StaleElementReferenceException” and so.
- And these exceptions are mainly because of the synchronization issues with web pages and objects.
- Let’s discuss about the Synchronization and how to use in Selenium WebDriver.
Synchronization can be classified into two categories:
- Conditional Synchronization
- We will make Selenium WebDriver to wait until certain time and then proceed further.
- Examples: Wait() and Sleep();
- The main disadvantage for the above statements are, there is a chance of unnecessary waiting time even though the application is ready.
- 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.
- In such case, we must make the Selenium WebDriver to wait for certain amount of time by specifying the timeout value.
- We specify a condition along with timeout value, so that Selenium WebDriver 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 Selenium WebDriver should proceed further instead of making the tool to wait for a particular condition to satisfy.
- In Selenium, to do such conditional synchronization, we have implicit Wait and Explicit Wait conditional statements.
- An implicit wait is to indicate Selenium WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.
- The default setting is 0.
- Once, we define the implicit wait, it will set for the life of the WebDriver object instance.
- Implicit wait will not work for all the commands/statements in the application.
- Implicit wait will work only for “FindElement” and “FindElements” statements.
- If we set implicit wait, find element will not throw an exception if the element is not found in first instance, instead it will poll for the element until the timeout and then proceeds further.
- We should always remember to add the below syntax immediately below the WebDriver statement.
Example using implicit timeout:
WebDriver driver = new FirefoxDriver();
- We need to define a wait statement for certain condition to be satisfied until the specified timeout period.
- If the WebDriver finds the element within the timeout period, the code will get executed.
- Explicit wait is mostly used when we need to Wait for a specific object/content/attribute after performing any action.
- Example: Like there are drop-downs Country and State, based on the country value selected, the values in the state drop-down will change, which will take few seconds of time to get the data based on user selection.
/*Explicit wait for state dropdown field*/
WebDriverWait wait = new WebDriverWait(driver, 10);
- The above statement waits up to 10 seconds before throwing Exception (TimeoutException – Timed out after 10 seconds waiting for visibility of element) or if it finds the element, it will return in 0 – 10 seconds.
- There are different waits that can be used based on the needs which we frequently come across when automating web applications.