WebDriver Waits Command

Wait commands are essential for executing testcases. It pauses the execution for certain time before moving to next step based on certain condition or defined time.

Why wait commands are needed:

When a page loads on the browser, the elements on the page loads at different time interval as they are developed using Ajax and JavaScript. During automated testing, if the element is not located or partially loaded due to variation in time tag for loading elements, then webdriver will throw an “ElementNotVisibleException” or “ElementNotClickableException”. Waits commands thus helps in resolving these issues. It will pause the execution for certain time before throwing these exceptions.

Types of wait commands

  1. Implicit wait
  2. Explicit wait
  3. Fluent wait

 

Implicit wait:

This command instructs webdriver to wait for the certain amount of time before throwing “ElementNotVisibleException”. The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception. Implicit waits will be in place for the entire time the browser is open. It is a single line of a code that can be declared in the setup method of the test script.

Imports: It can be imported from import java.util.concurrent.TimeUnit package.

Syntax: driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.<TimeMeasurementScale>);

The implicit wait mandates to pass two values as parameters. The first argument defines the time in the numeric digits that webdriver needs to wait. The second argument indicates the time measurement scale.

Example1:

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

Here in above code webdriver will pause execution for 10 seconds.

Example2:

driver.manage().timeouts().implicitlyWait(30, TimeUnit.MILLISECONDS);

Here in above code webdriver will pause execution for 30 milliseconds.

 

Explicit wait:

The explicit wait tells the Web Driver to wait for certain conditions (Expected Conditions) before proceeding with execution of the code. There are certain elements in the webpage that naturally take more time to load, in those cases setting an Explicit Wait is important. If you provide implicit wait, then browser will wait for the same time before loading every element which will cause an unnecessary delay in execution. Explicit wait is more intelligent but can be applied only to some specific elements.

WebDriver introduces two classes “WebDriverWait” and “ExpectedConditions” to impose Explicit waits into test scripts. Some of the most commonly used methods exposes by “ExpectedConditions” that can be used in Explicit wait are below:

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

Imports: It can be imported from import org.openqa.selenium.support.ui.ExpectedConditions and import org.openqa.selenium.support.ui.WebDriverWait

Next step is object Instantiation for WebDriverWait class

WebDriverWait wait = new WebDriverWait(driver, <TimeOutInSeconds>);

Define Expected Condition

Syntax: wait.until(ExpectedConditions.<ExpectedConditionMethod>(<WebElement>)));

Example:

WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));

Here webdriver will pause the execution until the element is visible

 

Fluent Wait:

This defines the maximum amount of time for WebDriver to wait for a certain condition. It also defines the frequency with which WebDriver will check for the condition before throwing “ElementNotVisibleException”. Tt is possible to set a default polling period while using Fluent Wait and user can configure the wait to ignore any exceptions during the polling period.

 

Syntax: Wait wait = new FluentWait(WebDriver reference).withTimeout(timeout, SECONDS).pollingEvery(timeout, SECONDS).ignoring(Exception.class);

WebElement foo=wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id(“foo”));
}
});

Example:

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

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {

public WebElement apply(WebDriver driver) {
return element;
}
});

Here webdriver will check for the element after every 5secs and wait maximum of 30secs for the element. If the element appears before 30sec then it will break the wait loop and resume executing next step.

 

PageLoadTimeout Command:

It sets the amount of time to wait for a page-load to complete before throwing an error.

driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS)

Here webdriver will wait for maximum of 30secs for the page to get load completely