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 gives you a general view on what a WebDriverWait class is. You’ll learn about it’s two constructors. Furthermore, this lecture give you it’s usage and how it’s run.
Selenium Tutorial – Introduction WebDriverWait Class
To fix all of these problems that we’ve been having, we can use Selenium Webdrivers, WebDriverWait class. If we take a look at the WebDriverWait class, it’s actually very simple. What it does is allow us to define an object that is going to wait for elements to appear.
There are two constructors:
- There’s this WebDriver constructor where you’re passing your driver and then you’re passing the TimeSpan to wait a maximum period for an element to appear. This is just like we’ve been doing with the implicit waits where we sure I’m going to wait a maximum. This is a maximum amount. It doesn’t mean that it’s actually going to wait for that amount.
- The other constructor that we have is you can pass in a clock. You can pass in a driver. You can pass in a TimeSpan again. The maximum amounts that you want your code to wait for and then you can pass in a sleepInterval. If we take a look at the SleepInterval defines how often to check for a condition to be true. This kind of allows you to said the polling interval of the WebDriverWait Class.
The default polling interval of the WebDriverWait Class is 500 msec. If you don’t want to be polling frequently or maybe you want to be polling more frequently, you can change that using this class.
Let’s take a look at its usage, that’s why we’re here in this ExplicitWait2 test and you can see that what I did is I created a new instance of WebDriverWait class. I started my stopwatch and I’m going to search for an element and I’m going to search an element by using the wait.until method.
The wait.until method allows me to poll for an element “Until” it appears, right down the “til” if we have over it, you can see that every turns a web element and if you read the text you can see that it repeatedly applies the instances input value to the given function until one of the following occurs. The function returns neither null nor false. The function throws an exception that its in the list of ignored exception types. The timeout expires.
It’s going to keep something until the element does not found basically and then it’s going to throw an exception. This is awesome because as you saw, it only waits for the maximum amount of time required for an element to appear on the page.
Therefore, that was only 5 seconds. If we needed to do it for 10 seconds because the environment was slower, it would do it for 10 seconds. It doesn’t matter if your test is running slower that day, it would wait for a maximum of 10 seconds. The only caveat here is you obviously have to find the right amount of time to wait for an element to appear.
Usually, it depends on your application but on some of my applications, I’ll usually set this to a like 30 seconds. I should expect that an element right should be rendered on a page within 30 seconds. If it’s not, to me that’s pretty absurd that the page is not getting loaded within 30 seconds and then I’ll fail it, if its slower applications. If it’s some of the faster applications, I might just set it to 10 and keep that constant for all of my elements so that I should be expecting that our web pages are loading within 10 seconds.
Let’s do a quick run just so you guys can see what this code right here does and this is my explicit wait that I defined myself. I’m going to do a debug and its going to run until here and then stop at a breakpoint and I’m quickly going to step into it so that you guys can see what happens.
We’re going to step into it and now it’s going to do this method. The FindElement method and if it’s there, it’s going to return that element into here. If I do that, step through myDynamicElement, you can see that now it exists. Here’s its text, “Hello World!” Then of course, the stopwatch get stopped and then, the trace gets written and so we have our explicit wait.
However, you can see that this is a little bit cumbersome. It’s probably a little bit annoying to keep writing the south every time you need to wait for an element. It’s not a lot of code but to be writing these three lines of code for every time you want to wait for an element. It could get kind of cumbersome. Selenium does an amazing thing and they define a convenience method for us to use. Let’s go checked that out.