Waits are an important aspect of Automation Testing. Consider a situation where there is a slow network connection or your application is facing a heavy traffic or your application takes a few seconds to load all the web elements in to DOM or frequent page loads. In all the mentioned scenarios, it is advisable to wait for the page/elements to load on the UI. Thus, under such circumstances, we use wait. Therefore in this tutorial, we would discuss “Explicit and Implicit waits in Selenium WebDriver”.

There are primarily two types of waits available in Selenium WebDriver.

– Implicit Wait
– Explicit Wait

Let us discuss each of them in detail.

Implicit Wait

Implicit waits are used to provide the latency within each and every test step of the test script. Thus, the program control would wait for the specified time before moving the execution control to the next step. Thus the implicit waits are applied to all the test step of the testscript. In other words we can say that the system would wait for the specified period of time in order to load the desired object into the DOM.

Code Sample

//Create WebDriver instance variable
WebDriver driver;
//Launch browser
driver=new FirefoxDriver();
//Apply implicit wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

The above code informs the WebDriver to wait for 20 seconds before executing the next test step.

But like I said earlier, the execution control would wait forcefully for specified period of time even if the desired object is found which makes the execution a more time consuming process. In order to troubleshoot this problem, we use Explicit Waits.

Explicit Wait

Explicit waits are smarter waits than implicit waits. Explicit waits can be applied at certain instances instead of applying on every web element within the testscript. Suppose if we are creating a login script for Gmail. We know that we are most likely to wait for a few seconds to let the home page load successfully. Thus, in such cases explicit waits can be used. Another important benefit that we get from explicit wait is that it waits maximum for the specified period of time or a condition to be met. Thus, if our condition (Let’s say an element should be visible before we click on it) is met before the specified time has elapsed, then the system control would move forward rather than waiting for the complete time to elapse in order to save our execution time.

Code Sample

public void explicitWait(WebDriver driver)  
{  
WebDriverWait wait = new WebDriverWait(driver, 20);            
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("element id"))));  
}

In the above method WebDriver would wait for the expected condition (elementToBeClickable) to be met or for the timeout (20 seconds) to occur. As soon as the condition is met, the WebDriver would execute the next test step.

Customized Expected Condition (Fluent Wait)

public static void explicitWait(WebDriver driver,By by)  
    {  
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)  
                 .withTimeout(60, TimeUnit.SECONDS)  
                 .pollingEvery(10, TimeUnit.SECONDS)  
                 .ignoring(NoSuchElementException.class); 

         Boolean result= wait.until(new Function<WebDriver, Boolean>() {  
               public Boolean apply(WebDriver driver) {  
                 return driver.findElement(By.id("ID of the element")).isDisplayed();  
                }  
          });          
     }

In the above code, WebDriver polls every 10 seconds for the element to be displayed until the timeout occurs or the condition is met.

There is another way to use explicit wait with a little change in the code. Checkout the code below:

public void explicitWait(final WebDriver dvr, final By locator, int timeOut, final String str) {
        while (true) {
            Wait<WebDriver> wait = new WebDriverWait(dvr, timeOut);
            ExpectedCondition<Boolean> condition = new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver d) {
                    boolean result;
                    result = dvr.findElement(locator).getText().equals(str) || dvr.findElement(locator).getText().contains(str);
                    return result;
                }
            };
            // Won't get past here till timeout or the condition is met
            wait.until(condition);

            if (dvr.findElement(locator).getText().equals(str) || dvr.findElement(locator).getText().contains(str)) {
                break;
            }
        }
    }

I hope this tutorial caters your needs to introduce waits into your Selenium Scripts. Please write to us if you know a better way to do the same.

Share this:

One thought on “Explicit and Implicit waits in Selenium WebDriver

Leave a Reply

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