Wait Commands in Selenium WebDriver | Implicit | Explicit | Fluent Wait

The wait commands in Selenium Driver are the most important pillars behind creating a robust test script and successful execution of the test suite. Since Selenium WebDriver supports automation of only web-based applications, we do know very well that there are lots of variations in the time lag between loading different pages of the same web application.

You might have also observed that even on the sample page of an application some of the elements get loaded quickly while some of the elements take a bit longer to load. It makes it difficult for Selenium WebDriver in identifying web elements. Some of the most common reasons for automation script failure are as follows:

  • The element not found
  • The element is not visible
  • The element is not clickable
  • The element is not in the expected state

Why Do We Use Wait Commands in Selenium WebDriver?

Wait commands are used in Selenium to avoid synchronization issues and make scripts resilient to failure. Selenium WebDriver does have three wait commands to implement in tests.

  • Implicit Wait
  • Explicit Wait
  • Fluent Wait

Implicit Wait in Selenium WebDriver

The Implicit Wait in Selenium instructs the webdriver to wait for a certain amount of time between each consecutive test step across the entire test script. The default wait setting is 0. We can set the desired wait time in seconds, milliseconds, minutes and etc. Once the time is set, the webdriver will wait for the element on the page for the defined time period and if the element is not found within the specified time it will throw “NoSuchElementException“.

The syntax of using implicit wait is as follows. I have set 10 seconds as the default wait time.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

We will have to import the following package in order to use implicit wait in our test.

import java.util.concurrent.TimeUnit;

Example of Implicit Wait In Selenium

However, the implicit wait has a drawback. It may increase the script execution time as each of the steps in the script would wait for a stipulated amount of time before resuming the test execution.

To overcome the shortcomings of Implicit Wait, WebDriver gives us the option to use Explicit waits wherein we can explicitly apply waits as per the need of the situation rather than waiting for a certain interval of time on each step while executing the test.

Explicit Wait in Selenium WebDriver

The Explicit Wait in Selenium is used to tell the WebDriver to wait for a certain amount of time until an expected condition is met or the maximum time has elapsed. If still, the defined time exceeds then Selenium will throw an exception. Unlike Implicit waits, Explicit waits are applied only for specified elements.

You can use Explicit Wait for those elements that take more time to load as compared to other elements on the page. It is a much better option to use for dynamically loaded Ajax elements.

After declaring explicit wait we have to use “ExpectedConditions“.We can also configure how frequently we want to check whether the desired condition is met using the Fluent Wait that I at later part of this tutorial. In some of my posts, you might have observed that I have used Thread.Sleep(). Having said that I am creating sample tests to give examples but it is not recommended in actual test scripts.

The following is the list of Expected Conditions that can be used in Selenium WebDriver Explicit Wait.

  1. alertIsPresent()
  2. elementSelectionStateToBe()
  3. elementToBeClickable()
  4. elementToBeSelected()
  5. frameToBeAvaliableAndSwitchToIt()
  6. invisibilityOfTheElementLocated()
  7. invisibilityOfElementWithText()
  8. presenceOfAllElementsLocatedBy()
  9. presenceOfElementLocated()
  10. textToBePresentInElement()
  11. textToBePresentInElementLocated()
  12. textToBePresentInElementValue()
  13. titleIs()
  14. titleContains()
  15. visibilityOf()
  16. visibilityOfAllElements()
  17. visibilityOfAllElementsLocatedBy()
  18. visibilityOfElementLocated()

We will have to initialize the object of WebDriverWait to use Explicit Wait. I am giving a maximum wait time of 20 seconds.

Syntax:

WebDriverWait wait = new WebDriverWait(driver,20);

You will also have to import the following two packages to use Explicit Wait.

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

Example of Explicit Wait In Selenium

The following code will wait for the Google search text box to become visible within 20 seconds. If the textbox becomes visible before 20 seconds, the required operation will be performed. if the textbox does not become visible within 20 seconds, Selenium will throw an exception “org.openqa.selenium.TimeoutException: Expected condition failed:

Fluent Wait in Selenium WebDriver

The fluent wait is also called Smart Wait in Selenium as it is much smarter than Explicit Wait.FluentWait is used to define the maximum amount of time to wait for a certain condition to meet, as well as the frequency with which to check the expected condition. In addition to that, the user can also configure the wait to ignore specific types of exceptions whilst waiting, such as “NoSuchElementExceptions” or “ElementNotVisibleException”  and etc when searching for an element on the page.

Fluent Wait is very useful in dealing with web elements that significantly take more time to load. 

The Syntax of using Fluent Wait is as follows:

Note: The above code snippet is deprecated in Selenium v3.11 and above. You need to use the new syntax which is as follows.

Example of New Fluent Wait In Selenium

Code Explanation

The above code will wait for the “Sign up” link on the Instagram login page for a maximum time of 20 seconds and after every 2 seconds, it will check whether the “Sign up” link is available on the page. As soon as the link becomes available (let’s say after 6 seconds) it will click on the “Sign up” link and proceed further rather than waiting for 20 seconds.

Difference between Implicit Wait and Explicit Wait in Selenium

Implicit WaitExplicit Wait
It is applied to all the elements in a Test ScriptIt is applied only to those elements which are intended by user
Once Implicit Wait is defined, there is no need to specify "ExpectedConditions" on the element to be locatedIn Explicit Wait, we must have to specify "ExpectedConditions" on the element to be located
It is can be used when the elements are located within the time frame specified in Selenium Implicit WaitIt is should be used when the elements are taking long time to load. It is also very useful for verifying the property of the element such as visibilityOfElementLocated, textToBePresentInElementValue(),elementToBeClickable and etc

Conclusion

Selenium WebDriver provides us three different types of Wait command to be used in the test script. The wait commands are  Implicit, Explicit, and Fluent Wait. The three wait commands can be used as per the different situations discussed in this tutorial.

Recommended Posts

 

 

Leave a Reply