In This Course
In this course, you will:
1. Learn the problems that can occur as a result of improper synchronization
2. Learn how to use Selenium Webdriver explicit waits
3. Master Selenium Webdriver implicit wait
4. Understand what a DefaultWait is
5. Comprehend how to use all of the different types of waits during appropriate situations
In this Lecture
This lecture is an introduction of explicit waits and it’s purpose and use. This can help fix issues on implicit waits. You’ll also see examples on what this wait is about.
Selenium Tutorial – Introduction to Explicit Waits
To fix all of our issues that we have been have with implicit waits, there is an option in Selenium WebDriver known as an explicit wait. An explicit wait is the code that you define to be able to sit and wait for different kinds of elements for something to occur. That’s an explicit wait. As opposed to using the implicit wait method of Selenium WebDriver, you actually define the code that allows you to check for a certain condition.
Let’s quickly go through this test and I’ll show you guys different examples of explicit waits so that you can better understand these test classes. It’s pretty much the same as the last one for the implicit waits. However, what I did here is just clean it up a little bit.
The URI I also updated just for variety instead of going to the first page, I’m taking you to another dynamic loading screen. This one the only difference is that all the actions are the same but here if you look at the html that the div that we were looking for it doesn’t exist with the “Hello World!” It doesn’t exist right now. It gets rendered only after we click the start button. Take a look.
See that. Now it exists in the HTML. It wasn’t rendered before that. That’s the main difference. Also, what I did was I put our element into a class variable here so that we can use it all throughout and it can be consistent in all of our tests. I am also going to be using a stopwatch class to show you guys how long a test takes to run.
In my set-up method, I do the basic initialize that we have been doing, creating a FireFoxDriver and then I’m also going to our URL and finding the button element right away. From there, I’m going to take other actions on our tests.
The very first example that I want you to show you of explicit waits is this Thread.Sleep. It is the worst example by far over an explicit wait because you should never use it. This is exactly the same thing as an implicit wait except you are literally hard coding how long a test is going to wait for an element to appear.
If we run this test, then we can come here and see the output. You can see that the time for element identification was ten seconds. That makes complete sense, right? Because we started the stopwatch here, right before our sleeping of ten seconds and then we obviously the element was already loaded and so this took less than a second and then we stopped our stopwatch and then the element was found.
The length to find an element using Thread.Sleep is obviously going to be as long as this Thread.Sleep. It doesn’t matter that the element was returned within five seconds, it’s still going to wait for ten seconds. Our problems with implicit waits are not solved here but it’s exactly the same problem. Then you can progress to a better solution to be able to have your tests dynamically updating their run times.