Selenium WebDriver Waits
Now a day, the web applications are using AJAX and Java Script techniques for development purpose. At the time of loading particular page on web browser, elements within the page may require different time intervals to be loaded
This will make difficult to locate element and if element not located WebDriver throw “ElementNotVisibleException” exception.
Using waits, we are telling WebDriver to wait for a certain amount of time before going to next step. So we can solve this problem.
Selenium WebDriver software Testing tool provides four types of waits.
- Implicit wait
- Explicit wait
- Fluent wait
Thread.sleep() method can be used to pause the execution of current thread for specified time in milliseconds. The argument value for milliseconds can’t be negative, else it throws IllegalArgumentException.
Thread.sleep() method is used to pause the for defined time. Time is defined in milliseconds for this method.
This will helpful to pause execution for 2 sec.
But Theard.sleep() introduces a definite wait(that is fixed periods of inactivity) no matter what happens, which will slow down our test, and if you are using CI(Continuous integration) generally result take much time if we use it all over the place.
Secondly most people who use it end up doing so because they don’t understand a problem so it’s quick and easy to just throw in an arbitrary wait rather than trying to understand the problem and fix it.
For Example: – If we do an AJAX action and then have to wait for an element to appear, the correct thing to do would be to scan for the element until it appears, the correct thing to do would be to scan for the element until it appears and then carry on, but this is harder to code than to just throw in a 30 second wait so people use a Thread.seelp() instead.
Now the problem is that this means you don’t really know why the test has failed if the element is not picked up after your 30 second time out, is it because the element didn’t appear at all, or is it because the AJAX call took longer than normal to return?
If you had some code that scanned for the element you could stick in a much longer timeout secure in the knowledge that you are not slowing your test down, and also secure in the knowledge that when it fails you can be sure that it is because the element didn’t return.
I’m sure people will argue that it is easier to use Thread.sleep() and the difference in results as described above is not that great, but when you have 200 scripts getting run in CI and each one has about 5 Thread.sleeep() calls you’ll see how much pain just throwing in a Thread.sleep() can cause.
Due to these limitation selenium introduce implicit and explicit waits.
The idea of implicit wait is borrowed by Selenium WebDiver from Watir.
The implicit wait will tell to the web driver to wait for certain amount of time before it throws a “No Such Element Exception”. The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception.
If we have declared an implicit wait with the time frame of 10 seconds. It means that if the element is not located on the web page within that time frame, it will throw an exception.
To declare implicit wait:
The Explicit wait will tell the WebDriver to wait until certain/expected condition will occur or the maximum time exceed before throwing “ElementNotVisibleException” exception.
It is an intelligent kind of wait, but it can be applied only for specified elements.
Explicit wait gives better options than that of an implicit wait as it will wait for dynamically loaded Ajax elements.
WebDriver wait = new WebDriverWait(WebDriverRefrence,TimeOut);
The following are the Expected Conditions that can be used in Explicit Wait
Fluent wait is the mixture of both implicit and explicit wait. It is used to tell the WebDriver to wait until certain/expected condition will occur, as well as the time in which we want to check the condition before throwing an “ElementNotVisibleException” exception.
Wait wait = new FluentWait(driver)withTimeout(“TIME”, SECONDS).pollingEvery(“TIME”, SECONDS).ignoring(NoSuchElementException.class);
Wait wait = new FluentWait(driver)withTimeout(30, SECONDS).pollingEvery(5, SECONDS).ignoring(NoSuchElementException.class);