Selenium Webdriver Tutorials

Selenium WebDriver Wait commands | Different types of wait in Selenium Webdriver

Today most of the web applications are developed using Ajax and Javascript, i.e. the elements within the page might load at different time intervals. What if you write a Selenium WebDriver script just to encounter “ElementNotVisibleException” or “NoSuchElementException”? Yeah! It can be frustrating. This happens when WebDriver tries to identify an element that is still not loaded (or visible). We call it as “Sync issue” in automation. How to avoid the rework? Simple, wait for ‘some’ time during execution until elements are properly loaded or are visible in the webpage. It is NOT recommended to use Thread.Sleep() while testing or building the framework. So how do you wait in Selenium Webdriver? What are the Selenium WebDriver Wait commands? Let’s understand the different types of wait in Selenium Webdriver – Implicit, Explicit and Fluent Wait!

Selenium WebDriver Wait commands

Selenium WebDriver Wait commands

Hope you are already familiar with the Selenium WebDriver basics? If not, please go through our articles to help begin your journey with Selenium Automation!

Implicit Wait

Simply stated – “For every element identification, tell Selenium to wait for a certain amount of time before throwing a “No Such Element Exception”. Please note that once implicit wait is set, it will be applicable until the WebDriver instance is destroyed, i.e. for every element identification.

Implicit wait accepts 2 parameters,

  • Time as an integer value
  • Time measurement in terms of SECONDS, MINUTES, MILISECOND, MICROSECONDS, NANOSECONDS, DAYS, HOURS, etc.

WebDriver tries to locate the element >> say it does not find it >> it doesn’t try to identify the element again, instead it waits till the specified time (10 secs here) >> Before the end of 10 secs, it checks once again if element is present >> If not it throws the exception.

As you might have guessed, implicit wait increases the test script execution time as for each element it will wait for the specified time before resuming the execution. Instead let’s look at the other Selenium WebDriver Wait commands. In order to use Implicit wait, we need to include below package,

Explicit Wait

The explicit wait is an intelligent wait, i.e. used to tell the Web Driver to wait for certain conditions (Expected Conditions like element to be visible, clickable, etc.) or the maximum time before throwing an “ElementNotVisibleException” exception. Explicit wait gives better options than that of an implicit wait. WebDriver introduces classes like WebDriverWait and ExpectedConditions to enforce Explicit waits into the test scripts.

Once we declare explicit wait we can use “ExpectedCondtions” or we can configure how frequently we want to check the condition using Fluent Wait. Both Selenium WebDriver Wait commands – WebDriverWait and FluentWait are classes and implements the Wait interface.

WebDriverWait

WebDriverWait is applied on certain element with defined expected condition and time out. This wait is only applied to the specified element. It will throw an exception when element is not found within the specified time out. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully.

Here we wait for the TimeOut (10 seconds) or the ExpectedCondition, whichever occurs first before throwing an exception. Therefore, as soon as the expected condition occurs, the program control would move to the next execution step instead of forcefully waiting for the entire 10 seconds.

Selenium WebDriver Wait commands – 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()

Say a particular element takes more time (say 1 minute) to load compared to others. We should not use implicit wait for 1 minute since it will slow down the script drastically, i.e. wait for same amount of time for every element. Instead, we should use explicit wait (WebDriverWait) with the expected condition (ExpectedCondition). Unlike Implicit wait, Explicit wait is applied for a particular instance only.

FluentWait

An implementation of the Wait interface that may have its timeout and polling interval configured on the fly. Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

  • Timeout: The maximum time to wait for.
  • Polling Frequency: Setting up a repeat cycle with the time frame to verify the condition at regular interval of time.
  • Ignoring: Type of exceptions that needs to be ignored during element search.

FluentWait can prove pretty handy for Ajax because of the flexibility you have for ignoring exceptions and easily tweaking the wait and polling times. Hope this article helped you understand the different Selenium WebDriver Wait commands. If Yes, don’t forget to share it with your friends…

Leave a Reply

Your email address will not be published. Required fields are marked *