Synchronization has a very vital role to play in automation. Code execution and application need to be in sync to perform the operation. If the application slows down for any reasons like network, heavy load, etc then the code keeps on checking for the particular web element. If the code doesn't able to find that element it fails, by throwing exceptions like NoSuchElement, ElementNotVisible, etc.
Hence, it becomes our responsibility to maintain the synchronization during automation. In Selenium WebDriver,
Synchronization can be divided into 2 types:
This concept of synchronization comes from Java language where we just specify the timeout limit. It pauses the code for particular time specified and then it starts to execute the next line of code.
As a parameter, we have to pass the timeout value. Values are default to milliseconds. For example, if want to wait for 10 seconds , we need to pass value as thread.sleep(10000). Unconditional synchronization is not advisable to use for automation as it just waits for the specified time even if it has found the web element.
In conditional synchronization, conditions will be set along with the timeout limit. Code will wait for the specified time declared until the expected condition gets satisfied, then it executes the next line of code.
It is divided into 2 types:
Implicit Wait - The Implicit Wait in Selenium is used to tell the web driver to wait for a certain amount of time before it throws a “No Such Element Exception”. The default setting is 0. Once we set the time, the web driver will wait for the element for that time before throwing an exception.
Syntax -> driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Once after instantiating the webdriver, implicit wait has to be declared which gets the wait time for its driver's lifetime. If any Web element is not available in DOM, it waits until the specified time to search that element.
Implicit wait will accept 2 parameters, the first parameter will accept the time as an integer value and the second parameter will accept the time measurement in terms of SECONDS, MINUTES, MILISECOND, MICROSECONDS, NANOSECONDS, DAYS, HOURS, etc.
The Explicit Wait in Selenium is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or maximum time exceeded before throwing “ElementNotVisibleException” exception. It is an intelligent kind of wait, but it can be applied only for specified elements. It gives better options than implicit wait as it waits for dynamically loaded Ajax elements. It is recommended to use when the elements are taking long time to load and also for verifying the property of the element like(visibilityOfElementLocated, elementToBeClickable,elementToBeSelected). Explicit wait is used to wait for particular Web element, whereas Implicit wait will be used to wait for whole Web Elements in the driver instance.
In this WebDriver wait example, wait for the amount of time defined in the “WebDriverWait” class or the “ExpectedConditions” to occur whichever occurs first.
The above Java code states that we are waiting for an element for the time frame of 20 seconds as defined in the “WebDriverWait” class on the webpage until the “ExpectedConditions” are met and the condition is “visibilityofElementLocated“.
I hope this blog would have helped in making the concept of synchronization in Selenium WebDriver clear.
Thanks for visiting and keep reading!!!