Saturday, 22 October 2016

Implicit Wait , Explicit Wait and Fluent Wait in Selenium



As we have all faced that we need to wait for loading in web application.This is where our selenium tests fail if  the web element test trying to interact with is not visible on the screen. In this case If the Web element is not found then it throws the exception "ElementNotVisibleException".We can solve this issue of unpredictable test case failure in Selenium using "Waits".

Video Tutorial -

 

In this post we will see 3 types of Waits which are widely used in Selenium -

1.Implicit Wait.
2.Explicit Wait
3.Fluent Wait

1. Implicit Wait - 

Implicit Wait means informing selenium web driver to wait for specific amount of time and if the web element is not visible after waiting for that specific point then throw "No such Element found Exception". 

Check out the Example given below to understand how to use Implicit wait in Selenium Test Case.In below Test case we will add a wait for 30 seconds after opening the browser so that that the page is gets loaded completely.As we implicitly specify the time and Time Unit this is called as implicit wait.
method implicitlywait() accepts 2 parameters first one is time value and second one is time unit like days,hours,minutes,seconds,milliseconds etc.

package com.selenium.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; 
import java.util.concurrent.TimeUnit; 
 public class BrowserTests {

 public static void main(String[] args) throws Exception {
  System.setProperty("webdriver.chrome.driver", 
 "C:\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
  driver.get("http://qaautomated.blogspot.in");
  driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
  driver.quit();
 }

}
 

2. Explicit Wait -

In Explicit wait along with wait time we also provide the wait condition. It will wait till the condition or the maximum wait time provided before throwing the Exception"ElementNotVisibleException".

We can say that Explicit wait is intelligent wait as it waits dynamically for specified condition. Check out the below test case example for using Selenium Explicit wait.First We need to instantiate WebDriverWait  wait object with Web Driver Reference and time frame in seconds.

Then we can use until() method for passing Expected Wait Condition and use it in our test case for waiting until the element is visible on the screen.Here we are using  visibilityofElementLocated() condition.To find out about other conditions type ExpectedConditions in eclipse IDE and press cltr+space bar and you will get the list of conditions which you can use in with Explicit Wait.

package com.selenium.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; 
import java.util.concurrent.TimeUnit; 
 public class BrowserTests {

 public static void main(String[] args) throws Exception {
 System.setProperty("webdriver.chrome.driver", 
 "C:\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
  WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
  driver.get("http://qaautomated.blogspot.in");
  WebElement aboutMe;
aboutMe= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));      
aboutMe.click();
  driver.quit();
 }

}
3. Fluent Wait -

Fluent wait is when we can setup a repeat time cycle to verify the condition is met or not. Fluent wait allows use to setup frequency with a time frame to verify the given expected condition at regular time interval. This wait is used in test scenario where element loading time is variable (it can be 10,15 or 20 secs) In this case we can use frequency to handle "ElementNotVisibleException".

Check the below example we are defining the wait time as 20 seconds and then 5 seconds for frequency. This means that the to wait for 20 secs to meet the condition after each 5 seconds till the condition is met.




package com.selenium.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; 
import java.util.concurrent.TimeUnit; 
 public class BrowserTests {

 public static void main(String[] args) throws Exception {
 System.setProperty("webdriver.chrome.driver",  
"C:\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
  Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)       
.withTimeout(20, TimeUnit.SECONDS)    
.pollingEvery(5, TimeUnit.SECONDS)    
.ignoring(NoSuchElementException.class); 
  driver.get("http://qaautomated.blogspot.in");
  WebElement aboutMe= wait.until(new Function<WebDriver, WebElement>() {       
public WebElement apply(WebDriver driver) { 
return driver.findElement(By.id("about_me"));     
 }  
});  
  
aboutMe.click(); 

  driver.quit();
 }

}

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. An implicit wait is to tell Web Driver to poll the DOM for a certain amount of time when trying to find an element in selenium or elements if they are not immediately available.
    Software testing selenium training in Chennai

    ReplyDelete
  3. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete