WebDriverWait: The slim version of FluentWait

WebDriverWait is a subclass of FluentWait and it only takes WebDriver as its input. A Comparation of FluentWait and WebDriverWait constructors:

FluentWait(T input)
FluentWait(T input, Clock clock, Sleeper sleeper)

WebDriverWait(WebDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut)
WebDriverWait(WebDriver driver, long timeOutInSeconds)
WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)

You can see that FluentWait could take anything as its input, while WebDriverWait only takes WebDriver as its input. This makes WebDriverWait not as flexible as FluentWait, but much simpler to use.

WebDriverWait By default calls the ExpectedCondition every 500 milliseconds until it returns successfully.

Sample Usage

WebDriver comes with ExpectedConditions class which can check almost all web conditions for us. It saves us the trouble to define our own apply() method for wait conditions and makes it really handy for us to use WebDriverWait:

//check visibility of this element every 500ms, but for at most 5 seconds
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfEelementLocated(By.id(“username”));

Another one:

(new WebDriverWait(driver, 10).until(new ExpectedCondition(){
Public Boolean apply(WebDriver d){
return d.getTitle().contains(“cheese!”);
}
});

Comparing with FluentWait, WebDriverWait is more simplistic and yet robust. You are likely to use this a lot for waiting in UI Automation.

Q&A on WebDriverWait:

  • What we are waiting for?
    Wait for certain condition is satisfied, most time it has something to do with the status of certain WebElement, but doesn’t have to
  • How long do we wait?
    It is defined in timeout parameter of WebDriverWait constructor
  • How frequently we check the result?
    Every 500 milliseconds by default
  • What we are checking?
    As per .until condition specified in the code
  • What to do if on show at the end?
    Throws different Exception depending upon your waiting condition
  • When to use?
    If element takes a long time to load. Also, used to check property of an element (presence, clickability. etc).

Leave a Reply

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