Explicit Wait- Synchronization in Selenium Part 2

Category: Selenium WebDriver 11 0

Till now we have very well understood the concept and importance of Synchronization in Selenium WebDriver. It keeps the program up and running until the Expected and Actual results are same. We knew how WebDriver Wait is helping to establish the synchronization between Selenium testing tool and Application under test. The real use of WebDriver wait is seen in Explicit wait.

What is an Explicit wait?

An explicit wait is a command which inculcates WebDriver to wait for the specific elements to appear for the certain condition within the defined time frame. Thread.sleep() is the worst case of the Explicit wait, which incorporates the condition to wait for the exactly defined time.

It returns its result in Boolean. Once wait time expires then it throws Exceptions like TimeOutException, ElementNotVisibleException, etc.

Earlier we have seen in Implicit wait that it didn’t have any conditions over wait time, however, we find the conditions over wait time in Explicit wait. Hence, it makes “Explicit wait” a kind of intelligent wait.

It has the best use where the entire application under test is filled with dynamic WebElements as well as Elements which appears after AJAX loading.

Syntax of Explicit wait

Here first we create the instance of WebDriverWait and we pass driver as well as time duration as arguments. After this declaration, we define failure condition beyond the defined time. So here is the generalized command of Explicit wait:


explicit wait

Pre-defined Expected conditions for Explicit wait

In above syntax, we have seen the place to put pre-defined conditions. This is the condition for which WebDriver will wait for the defined time duration. If the condition becomes false then it will throw an exception.

List of pre-defined conditions are:

  • titles()
  • titleContans()
  • visibilityOfElementLocated()
  • visibilityOf()
  • visibilityOfAllElements()
  • visibilityOfAllElementsLocatedBy()
  • elementToBeSelected()
  • elementSelectionStateToBe()
  • elementToBeClickable()
  • textToBePresentInElement()
  • textToBePresentInElementValue()
  • textToBePresentInElementLocated()
  • presenceOfElementLocated()
  • presenceOfAllElementsLocatedBy()
  • alertIsPresent()
  • invisibilityOfElementWithText()
  • invisibilityOfTheElementLocated()

Let me teach you the utilization of above conditions in your program.

Let us consider a scenario, where we are expecting some text in the title of the web page and it takes time for the title to appear. We can use Thread.sleep() or Implicit wait, but that is not a perfect choice because we have a condition available as title text. So let’s frame the command.

This program has set the failure condition as for the title, hence, it will wait till 20 seconds and beyond that, it will invoke an exception.

You can use any of the conditions from above list based on the scenarios or business requirement of placement of dynamic element.

Here is the sample code which uses explicit wait:


Related Articles

Add Comment