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 explains Implicit wait. You would know all it’s uses and advantages. Then some of your questions about these waits will be answered in this lecture.
Selenium Tutorial – Implicit Wait
Now that you understand the possible problems with the synchronization issues, I am going to teach you how to fix those synchronization issues so that you can have very stable and solidly robust tests, that don’t break under every single condition. Then you can comfortably report your automated functional tests and their results to your bosses.
Selenium WebDriver provides us a few mechanisms for dealing with synchronization issues. Right now, we are going to cover one of those methods and that is known as implicit wait. We are going to cover these properties of the implicit wait and I urge you strongly to remember these properties of implicit wait. Because it’s going to help you to further understand the difference between implicit wait and other different kinds of waits that Selenium Webdriver provides us, so that you can actually understand the difference.
What is an implicit wait?
An implicit wait, the idea was actually taken from watir. No, I don’t mean the water like H2O, by Watir I mean the automatic testing tool. An implicit wait, what it did in water just like it does in Selenium Webdriver, constantly pulled DOM, to check for the presence of the element. If that element wasn’t present in that certain period of time, then implicit wait method throws an exception, that’s exactly what the Selenium Webdriver methods does. It is a method provided to us by Selenium Webdriver that will constantly pull the DOM until an element is present or it throws an exception.
Now that brings a question.
How long is it going to wait for the element to be present?
Well, that depends on the implementation of the method. The way you implement is easy, because the Selenium WebDriver provides us that method directly.
Right here, we have implemented the implicit wait, which has been directly provided to us by Selenium Webdriver. When you implement an implicit wait, you implement this exact method. That’s how you will know that you have used an implicit wait vs a different kind of wait. This implicit wait method takes a struck, which is a TimeSpan Object, and you can specify many different values here, which I will show you in the code in the future.
The full implementation looks something like this. Here it means that it is going to wait for full five seconds. That is how long implicit wait is going to wait for the element to be present. Therefore, this means that when you use FindElement method on whatever element you want to find its going to wait a maximum period of 5 seconds for the element to appear before throwing an exception.
How often it is going to be checking for the element to be present?
That is the tricky part because that implementation depends on the driver. If you are using Safari driver, ChromeDriver or edge driver each of them are going to have its own implementation. Maybe possibly causing some flakiness issues for you when you are doing something like cross browser testing. Let’s say you have implemented a implicit wait for a maximum of five seconds and you look for a element and that element doesn’t appear.
What exception is going to be thrown?
Well that is the no element exception is going to be thrown when the element is not found.
One important thing to remember about implicit wait is the default value. It is crucial for you to understand the disadvantages that we are going to discuss in the future. The default value of an implicit wait is zero. What this implies is that when you do not set an implicit wait and you use the FindElement method it will poll for zero seconds before throwing in an no such element exception. Meaning that it will immediately check for an element if it’s not there it’s going to throw a NoSuchElementException, unless you specify that implicit wait timeout. The other key factor to remember about the implicit wait is that it lives for the lifetime of a driver. This means is that if you set this implicit wait timeout up here, no matter how many elements you find on a page, they are going to be pulled for a maximum of five seconds.
Let me show you an example, If we take a look at this amazing website here, did you guys see when it loaded, it didn’t load all at once, Let me do that again. There are elements that load up here and elements that load down there, then even if I scroll down there are elements that load on the scroll. The page didn’t load full at once, it loaded pieces at a time. That is common in today’s world wide web to make the user experience better.
But if we have set an implicit wait of five seconds for example, any time we try to find an element here or element there or find an element that is loading much later than the others its going to wait for a maximum period of five seconds before throwing that no such element found exception. That’s what that means for the list of lifetime. You set it once and its going to last for rest of the lifetime. Now those were the properties of an implicit wait.
What is the advantage of the implicit wait?
Well it’s easy to use, If you are having intimacy issues throughout your task, go ahead and set an implicit wait. Put in maximum amount of time you think your application is going to be waiting. Then let your test run. It will surely fix a lot of the problems because now each of FindElement method is going to take a certain amount of time find those certain elements. Hopefully that period is long enough for you to not have any issues.
Now the disadvantages I am going to cover in a few minutes. Because the disadvantages are really important but if you have any other questions regarding what which was covered here I have a blog post on my website, here is the short URL. Please keep in mind that it is case sensitive. You have to type this case in exactly. If you forget, it looks exactly like this here on my website ultimateqa.com, Explicit wait vs Wait in Selenium Webdriver. I have detail description of the waits and their properties just in case you forget. But I am sure you are not going to forget.