From the course: Advanced Selenium: 3 Synchronization Strategies

Why do you need synchronization? - Selenium Tutorial

From the course: Advanced Selenium: 3 Synchronization Strategies

Start my 1-month free trial

Why do you need synchronization?

- [Instructor] If we want to have robust automated execution, then we need to learn how to synchronize effectively our test execution with the application state. Otherwise, when we try to interact with the application, sometimes it might work and sometimes it might fail. Take, as an example, this simple application. Here I have a page with some links and a header, and a div with some text. This div has a link in it. But I can only click on that link if I hover over the div with my mouse. Then the div expands and I can see that there's a link on the page. If I try and click the link before it's fully visible, then the test will fail. This requires synchronization. Now looking at the chord here, in my Why Need Synchronization Test, all the chord in this project uses WebDriver Manager to manage the driver. If you already have ChromeDriver installed and you're going to use this chord, you can simply comment out these lines and you'll be fine. Now, I have a test called Can Navigate To About From Expanding Section Link. So this test visits the page we've just seen with the collapsible div, finds the collapsible div, the section dot condense, clicks on it to expand it, finds the about link, and then clicks on it and then asserts the rule on the correct page. If I run the test here, let's see what happens. Run the test. We should see that we visit the page and the test fails. Why does the test fail? If I scroll down to the exception. It fails on the click, so we came to the page, opened it, clicked on the div, it starts to expand, we can see the link is there so we click on it before the div has fully expanded. I didn't give the test enough time to expand the link. So I need to do that. The easiest way for me to do that is to, after I click the div, add in a delay. So I'll add in a thread.sleep and I'll make this 1,000 milliseconds. Now 'cause it's a thread sleep, I have to handle an exception. Alt enter and I'll just add that to the test, so now we have a waiting of one second in my test. Let me run this again. Now, the test should pass, because we click on the div, wait for one second, give it enough time to expand, and then we click the about link. But, I really don't recommend this approach because on line 31 now, I'm synchronizing on time, not on the application state. I really need to synchronize on application state, not time. Now I could use an in-built function in WebDriver called an implicit wait. And we'll cover this in more detail later on, but I'm just going to give you an example of it. If I change the driver management for the timeouts, and implicitly wait for two seconds, let me run the test. What that's done, is it has changed the internal configuration for timeouts, so when WebDriver does a find element or when someone clicks, it will wait until the click successfully passes, or wait until it successfully finds the element for the timeout time, which is two seconds. But this is a global configuration setting. So all my find element and all my click will have this wait. And it isn't as fine-grained as I need it to be 'cause some places in my application are going to need to synchronize differently than others. Now, I'm showing you both of these approaches very quickly, because you'll see them in conduction chord. But neither of these approaches are recommended. So I'm going to comment this out. In this course we will look at synchronization and depth in various approaches open to you to synchronize on application state effectively and robustly.

Contents