Synchronization in Selenium
Mukta Sharma
LinkedIn Top Voice |3C - Communication, Collaboration, Community || Community TOP VOICE*5 || TOP 100 Women In Tech || TOP 10 LinkedIn Influencers- London || AWS Certified || Scrum Master || Technical Blogger
When two or more components work together to perform any action at the same pace, the process is called synchronization. In Selenium, there should be synchronization between selenium script, web app and the script execution speed.
You might have noticed that sometimes pages are loaded successfully without any time delay whereas in some cases it takes a few seconds to load. In other words, web elements on a webpage are taking time to load. How to resolve such issues? Here, come “Waits” in the picture which has a significant value in Selenium.
Selenium provides synchronization in the form of implicit wait and explicit wait. We insert synchronization points in the script using WebDriverWait class.
Mainly, 2 types of waits are used in selenium. Both the below waits are dynamic in nature.
1. Implicit Wait
2. Explicit Wait
Let’s understand one by one:
Implicit wait:
1. Implicit Wait is global wait.
2. Implicit Wait time is applied to all the web elements in a test script with which the driver is interacting.
3. Implicit wait is given immediately after driver is initialized.
4. Implicit wait tells driver to poll the DOM for a certain amount of time when trying to find an element if the element is not immediately available.
5. The Syntax for implicit wait is simply and just one line:
Syntax:
driver.manage().timeouts().implicitlyWait(TimeOut,TimeUnit.SECONDS);
Example:
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Example elaboration: 10 second is the maximum time, driver will wait for an element to be found. If an element is not located on the web page within that time frame, it will throw an exception. Do you know the name of exception? Comment below and tell me which exception will be thrown?
6. The subsequent step in your script will only execute when 10 seconds have elapsed.
7. Implicit wait is useful when certain elements on the webpage are not immediately available and need some time to load.
8. The Default setting for implicit wait is 0, meaning disabled. Once set, it will be globally applicable to the script.
Code Snippet:
Explicit Wait:
1. Explicit wait is applied to a specific element on a web page.
2. Explicit wait allows your code to halt program execution until the condition is resolved or the expected condition is met.
3. Once you declare explicit wait, we have to use “ExpectedConditions” on the element to be located.
4. When the elements are taking a long time to load and also for verifying the property of the element like (VisitbilityOfElementLocated,elementToBeClickable,elementToBeSelected)
5. Syntax of explicit wait :
New WebDriverWait(driver,timeout).until(ExpectedConditions.elementToBeClickable(element));
Code Snippet:
Note: Thread.sleep() always pause the current thread execution. It interacts with thread scheduler to put the current thread in wait state for a specified period of time. Once the wait time is over, thread state is changed to runnable state and wait for CPU for further execution. Thread. sleep() is a static wait and it is not recommended to use it in our automation script.
I hope, this is useful for you and I could provide some clarity in understanding between these waits. This is one of the very important interview question which is often asked to an automation engineer.
Thank you for reading! Please feel free to contribute. Let’s share, learn and grow.
Sr. QA Engineer at QalbIT
3 年Nice article, keep it up
Senior Project Manager ||Ex Testing Head || Ex Sen Test Manager || Ex MorganStanley || EU National
3 年nicely explained : however in any case we wait for polling of dom elements or whole page : thats base of syncronization : thread.sleep() is however hardcoding waiting for that much time .
Senior QA Associate L2 at Publicis Sapient |Tester|Write blog at makeseleniumeasy.com|YouTuber @ RetargetCommon|Automation Testing|Volunteer @ CSP - Choti Si Pahal|
3 年2. Implicit Wait time is applied to all the web elements in a test script with which the driver is interacting. - Applied while locating element/s (findElement and findElement) only not other actions. 3. Implicit wait is given immediately after driver is initialized. - Not necessarily. It will Be applied fot upcoming findElement() and findElememts() after setup. There is also not necessarily you need to use ExpectedConditions if you use explicit wait.
Quality Assurance Expert in Integration and Device Testing
3 年Thanks,very well explained