Waits In Selenium WebDriver

Share

 

Selenium WebDriver Waits

 

Now a day, the web applications are using AJAX and Java Script techniques for development purpose. At the time of loading particular page on web browser, elements within the page may require different time intervals to be loaded

This will make difficult to locate element and if element not located WebDriver throw “ElementNotVisibleException” exception.

Using waits, we are telling WebDriver to wait for a certain amount of time before going to next step. So we can solve this problem.

Selenium WebDriver software Testing tool provides four types of waits.

  1. sleep()
  2. Implicit wait
  3. Explicit wait
  4. Fluent wait

Thread.sleep()

Thread.sleep() method can be used to pause the execution of current thread for specified time in milliseconds. The argument value for milliseconds can’t be negative, else it throws IllegalArgumentException.

Thread.sleep() method is used to pause the for defined time. Time is defined in milliseconds for this method.

Syntax:

Thread.sleep(2000);

This will helpful to pause execution for 2 sec.

But Theard.sleep() introduces a definite wait(that is fixed periods of inactivity) no matter what happens, which will slow down our test, and if you are using CI(Continuous integration) generally result take much time if we use it all over the place.

Secondly most people who use it end up doing so because they don’t understand a problem so it’s quick and easy to just throw in an arbitrary wait rather than trying to understand the problem and fix it.

For Example: – If we do an AJAX action and then have to wait for an element to appear, the correct thing to do would be to scan for the element until it appears, the correct thing to do would be to scan for the element until it appears and then carry on, but this is harder to code than to just throw in a 30 second wait so people use a Thread.seelp() instead.

Now the problem is that this means you don’t really know why the test has failed if the element is not picked up after your 30 second time out, is it because the element didn’t appear at all, or is it because the AJAX call took longer than normal to return?

If you had some code that scanned for the element you could stick in a much longer timeout secure in the knowledge that you are not slowing your test down, and also secure in the knowledge that when it fails you can be sure that it is because the element didn’t return.

I’m sure people will argue that it is easier to use Thread.sleep() and the difference in results as described above is not that great, but when you have 200 scripts getting run in CI and each one has about 5 Thread.sleeep() calls you’ll see how much pain just throwing in a Thread.sleep() can cause.

Due to these limitation selenium introduce implicit and explicit waits.

Implicit Wait

 

The idea of implicit wait is borrowed by Selenium WebDiver from Watir.

The implicit wait will tell to the web driver to wait for certain amount of time before it throws a “No Such Element Exception”. The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception.

If we have declared an implicit wait with the time frame of 10 seconds. It means that if the element is not located on the web page within that time frame, it will throw an exception.

To declare implicit wait:

Syntax:

Driver.manage().timeouts.implicitlywait(TimeOut, TimeUnit.SECONDS);

 

 

Explicit Wait

 

The Explicit wait will tell the WebDriver to wait until certain/expected condition will occur or the maximum time exceed before throwing “ElementNotVisibleException” exception.

It is an intelligent kind of wait, but it can be applied only for specified elements.

Explicit wait gives better options than that of an implicit wait as it will wait for dynamically loaded Ajax elements.

Syntax:

WebDriver wait = new WebDriverWait(WebDriverRefrence,TimeOut);

The following are the Expected Conditions that can be used in Explicit Wait

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

 

Fluent Wait

 

Fluent wait is the mixture of both implicit and explicit wait. It is used to tell the WebDriver to wait until certain/expected condition will occur, as well as the time in which we want to check the condition before throwing an “ElementNotVisibleException” exception.

Syntax:

Wait wait = new FluentWait(driver)withTimeout(“TIME”, SECONDS).pollingEvery(“TIME”, SECONDS).ignoring(NoSuchElementException.class);

 

Example:

Wait wait = new FluentWait(driver)withTimeout(30, SECONDS).pollingEvery(5, SECONDS).ignoring(NoSuchElementException.class);

 

“Related post”

Software Development life cycle

Software Testing Life Cycle

Test Case Documentation

Alpha and Beta Testing

Regression Testing

Smoke and Sanity Testing

Software Testing

Validation and Verification

V-Model

Entry and Exit Criteria in software testing

Integration Testing 

Automation Testing

Real Time Automation Questions and Answer

 

 

Leave a Reply

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