Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Sep 22, 2019 17:34

Test scripts involve finding the UI elements on the display and automating them - either clicking on or sending text to the element. Finding the elements is quite often time sensitive. Simply put, it takes time for the element to be displayed on the screen. If you search too early you may not find the element, or you may find an element that is not clickable.

Therefore, it is important to learn how to use the wait options that Selenium provides:

> Implicit Wait - affects all findElement() calls. 
> Explicit Wait - localized wait, with minimal control over search performance.
> Fluent Wait - localized wait, with extended control over search performance

On this page:

Implicit Wait

Applied to the driver, usually set at the beginning of the test script, may be reset during the flow. Setting the wait period, tells the RWD client to continue searching for the element until it is found or the wait period ends. The following snippet shows how to set the Implicit Wait period.

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

Explicit Wait

Explicit Wait controls a specific verification operation and is implemented by the Selenium WebDriverWait class. When creating the class instance, supply the driver instance and the number of seconds to configure for the timeout. The waiting and repeated validations are performed in the driver server side (as opposed to the Implicit Wait, where the RWD client performs the repeated search function call).

The WebDriverWait class supports the until() method used to supply the validation condition. This method is usually used in conjunction with the ExpectedConditions class. The ExpectedConditions class provides static methods that can check for the following conditions of different UI Elements:

> Selected
> Clickable
> Visible
> Invisible

The following code sample creates an instance of the WebDriverWait class and then checks the visibility of a UI Element based on the element's Name. Note that if the condition is not met within the timeout (in this case 15 seconds) that the verification throws a TimeoutException.

WebDriverWait wait = new WebDriverWait(driver, 15);  // timeout of 15 seconds
 try {
     } catch (TimeoutException t) {
      System.out.println("Did not find the Label within explicit wait time");

Fluent Wait

The third type of wait supported by Selenium is Fluent Wait that extends the Explicit Wait to support more direct control over the frequency of the repeated verification calls.

The FluentWait class is a template class that supports setting the timeout, as well as two more important settings:

  • pollingEvery - configures the interval at which the search action will be performed again.
  • ignoring - configures a lit of Exception conditions ignored by the FluentWait

After the timeout period ends the FluentWait may throw a TimeoutException.

The following example shows a FluentWait instance that checks every 5 seconds, up to a 30 second timeout whether the element whose id is "Start" can be found on the screen.

// First - set the wait parameters
Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver) 
    .withTimeout(30, TimeUnit.SECONDS)        // set the timeout 
    .pollingEvery(5, TimeUnit.SECONDS)        // set the interval between every 2 tries     
	.ignoring(NoSuchElementException.class);  // don't throw this exception

// Then - declare the webElement and use a function to find it
WebElement waitingElement = wait.until(new Function<WebDriver, WebElement>() {
       public WebElement apply(WebDriver driver) {
              return driver.findElement("Start"));