Selenium is the most widely used open-source tool for automating testing suite for web applications across a range of platforms and browsers.

If you want to become an expert at using Selenium WebDriver, one of the most important skills is the use of the Wait commands. They are very important for executing test scripts and help to identify and solve problems related to time lag in web elements.

This article will offer a detailed description of how testers can use the Wait function in Selenium. Also we will break down Implicit, Explicit and Fluent Wait in order to provide clarity on when to use which function.

What is Wait command in Selenium?

Waits are commands in Selenium that are very important for executing automation test scripts. During automation testing of websites, issues may occur due to variation in time lag for loading web elements. Wait commands help to observe and troubleshoot these issues.

When a page loads on a browser, various web elements on it with may load at different time intervals. Wait commands direct a test script to pause for a certain time before throwing a ElementNotVisibleException.  This enables WebDriver to check if one or more web elements are present/visible/enriched/clickable, etc.

Why do users need Selenium Wait?

Most web applications are developed with Ajax and Javascript. When a page loads on a browser, the various web elements that someone wants to interact with may load at various time intervals.

This obviously makes it difficult to identify any element. On top of that, if an element is not located then scripts will failed. Selenium Wait commands help resolve this issue.

Selenium WebDriver provides three commands to implement wait in tests.

  1. Implicit Wait
  2. Explicit Wait
  3. Fluent Wait

Implicit Wait in Selenium

Implicit Wait directs the Selenium WebDriver to wait for a certain measure of time before throwing an exception. Once this time is set, WebDriver will wait for the element before the exception occurs.

Once the command is in place, Implicit Wait stays in place for the entire duration for which the browser is open. Its default setting is 0, and the specific wait time needs to be set by the following syntax.

Copy to Clipboard

Add the above code into the test script. It sets an implicit wait after the instantiation of WebDriver instance variable.

Copy to Clipboard

Explicit Wait in Selenium

By using Explicit Wait command, the WebDriver is directed to wait until a certain condition occurs before proceeding with executing the code.

Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load. If one sets an implicit wait command, then the browser will wait for the same time frame before loading every web element. This causes an unnecessary delay in executing the test script. Explicit wait is more intelligent, but can only be applied for specified elements. However, it is an improvement on implicit wait since it allows the program to pause for dynamically loaded Ajax elements.

The following Expected Conditions can be used in Explicit Wait.

  • alertIsPresent()
  • elementSelectionStateToBe()
  • elementToBeClickable()
  • elementToBeSelected()
  • frameToBeAvaliableAndSwitchToIt()
  • invisibilityOfTheElementLocated()
  • invisibilityOfElementWithText()
  • presenceOfAllElementsLocatedBy()
  • presenceOfElementLocated()
  • textToBePresentInElement()
  • textToBePresentInElementLocated()
  • textToBePresentInElementValue()
  • titleIs()
  • titleContains()
  • visibilityOf()
  • visibilityOfAllElements()
  • visibilityOfAllElementsLocatedBy()
  • visibilityOfElementLocated()

In the following example, the test script is for wait until website homepage load successfully.

Copy to Clipboard

Fluent Wait in Selenium

The Fluent Wait command defines the maximum amount of time for Selenium WebDriver to wait for a certain condition to appear. It also defines the frequency with which WebDriver will check if the condition appears before throwing the “ElementNotVisibleException”.

To put it simply, Fluent Wait looks for a web element repeatedly at regular intervals until timeout happens or until the object is found. Fluent Wait commands are most useful when interacting with web elements that can sometimes take more time than usual to load. This is largely something that occurs in Ajax applications.

While using Fluent Wait, it is possible to set a default polling period as needed. The user can configure the wait to ignore any exceptions during the polling period.

Copy to Clipboard

Conclusion

Mastering the use of selenium wait command is essential for testers to set up efficient test automation. Effective implementation of waits can greatly simplify the process of automated testing. It saves time, effort and helps to detect anomalies on web pages, thus ensuring that software testing is easier to start, execute and review.

Find us if you interested in automation testing.