Showing posts with label WebDriverWait. Show all posts
Showing posts with label WebDriverWait. Show all posts

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.

Sunday, 28 September 2014

Difference between Implicit Waits and Explicit Waits

Implicit Waits :

Set implicit wait once and it apply for whole life of WebDriver object.
eg.

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



Explicit Waits : 

- Custom code for a particular element to wait for particular time of period before executing next steps in your test.
-  Webdriver provide “WebDriverWait”, “ExpectedCondition” classes to implement this.

eg.
//explicit wait for search field
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("search")));

This is the basic difference between Explicit and Implicit Waits with an example.

Explicit Waits are good to use as it waits for a particular web page element on which you can apply wait.