Monday, 29 September 2014

Customized Explicit Waits in WebDriver

When is the need for Customized Explicit Waits ?

There are situations at times when we need to explicitly write a custom condition which are not present in ExpectedCondition class of WebDriverWait class.

Example of a Customized Explicit Wait Condition:

Lets take an example where we need to wait for a URL to change,

So we will do Anonymous class Instantiation of Expected Condition. In these types, wait is applied on Boolean Condition. Here we have our own Boolean Condition on which wait needs to be applied.

Here is the code for it:


//customized Explicit Wait Condition
  ExpectedCondition e = new ExpectedCondition<Boolean>() {
           public Boolean apply(WebDriver d) {
            String previousURL = driver.getCurrentUrl();
            logger.info("Waiting...");
             return (d.getCurrentUrl() != previousURL);
           }
         };

In this example,
it will wait for sometime and then again check until a URL change is there.

So, this is how customized explicit waits works.