Using Explicit and Implicit Wait in Selenium

Explicit Implicit wait selenium is important commands. They are used while running automation scripts created using Selenium Web Driver. Explicit Implicit wait in selenium is primarily used to handle the different load time of elements on the web browser.

Using Explicit Implicit Wait Selenium – A Practical Scenario

Consider a situation in which you are testing the Facebook web application and you want to test a scenario in which you post a comment. When you automate this test scenario, there is a possibility it may fail, especially during low bandwidth and high usage. The reason for the automation failure, in such cases, is because the post button was yet to load. Elements on a web application load at different times.

In order to handle this, Selenium has introduced the usage of “Explicit” and “Implicit” wait. Selenium documentation on Explicit and Implicit wait warns users not to use both together.

Using Implicit Wait in Selenium:

Implicit wait is a quick and easy way to handle the problem of elements loading at different times on a web application. It is applicable at a global level and affects all the elements. It basically tells selenium to wait for a specified time before it throws a “No Such Element” Exception.

Let’s dive into an Example:

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

In the above example, we have instructed selenium to wait for the time frame of 10 seconds if the element is not present.

It means that if the element is not located on the web page within that time frame, it will throw an exception. During this time, the script will keep polling the DOM to find if the element has shown up. The polling interval cannot be changed. If the element is found selenium continues with the execution.

There are some clear drawbacks to this:

  1. Applied globally and hence effects all elements
  2. Can be only used to find elements and not conditions
  3. Polling interval cannot be changed
  4. It will slow down the execution time

Using Explicit Wait in Selenium

Explicit wait is a remedy to some of the problems which occur in implicit wait. However, the flip side to explicit wait is the complexity and the number of lines of code. When we use Explicit wait, we tell Selenium web driver to wait for a condition to occur. This is different from waiting for a specified period of time.

Here is an example:

WebDriverWait wait = new WebDriverWait(<expected condition>,10)

Here selenium will wait for an expected condition or 10 seconds whichever is earlier. The list of expected conditions for java can be found here. Some of these conditions are listed below:

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

A user on Trello explained the differences in using Explicit and Implicit very well.  I am summarizing those differences of Explicit Implicit wait selenium here:

Explicit:

  1. Is in part of selenium which is local
  2. Can handle most of the conditions
  3. Returns either success or timeout error
  4. A success condition can even be absence of an element
  5. Polling time can be customized
  6. But has long and complicated syntax

Implicit:

  1. Simple usage
  2. Is in the part of selenium which is Global (the part controlling the browser)
  3. Can only work with finding elements
  4. Returns element found or error
  5. Polling time cannot be customized


Author: InApp
We are a custom software development company offering Testing Services, Application Development, Mobility Solutions & more. Customers: Startups - Fortune 500

Leave a Reply

fourteen + fifteen =