Webdriver implicit and explicit wait for element locator.

Today most of web application using Ajax, When page is load some element load in a different time of interval or some time request send without page loading if the element not present then it through element not found exception or not visible element etc.. To handle this Webdriver provides two type of wait “implicit” and “explicit” wait

Implicit Wait: implicit wait provide to load DOM object for a particular of time before trying to locate element on page. Default implicit wait is 0. We need to set implicit wait once and it apply for whole life of Webdriver object. Add below line of code in test for implicit wait. However implicit wait slow down execution of your test scripts if your application responding normally.

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


Example:
package com.test;

import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class DataDrivenTest {

  private WebDriver driver;
  private String baseUrl;  
 
  @BeforeMethod
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.wikipedia.org/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test 
  public void testUntitled() throws Exception {
    driver.get(baseUrl + "/wiki/Main_Page");
    driver.findElement(By.id("searchInput")).clear();
    driver.findElement(By.id("searchInput")).sendKeys("India");
    driver.findElement(By.id("searchButton")).click();
  
  }

  @AfterMethod
  public void tearDown() throws Exception {
    driver.quit();   
  } 
}

Explicit Wait: In explicit wait you can write custom code for a particular element to wait for particular time of period before executing next steps in your test. This provide you better option than implicit wait. Webdriver provide “WebDriverWait”, “ExpectedCondition” classes to implement this.
ExpectedConditions class provide some set of predefine condition to wait element. Here is some condition which is use in ExpectedConditions class:
  • alertIsPresent() : Alert is present
  • elementSelectionStateToBe: an element state is selection.
  • elementToBeClickable: an element is present and clickable.
  • elementToBeSelected: element is selected
  • frameToBeAvailableAndSwitchToIt: frame is available and frame selected.
  • invisibilityOfElementLocated: an element is invisible
  • presenceOfAllElementsLocatedBy: present element located by.
  • refreshed: wait for a particular condition when page refresh.
  •  textToBePresentInElement: text present on particular an element
  • textToBePresentInElementValue: and element value present for a particular element.
  • visibilityOf: an element visible.
  • titleContains: title contains
Example:

package com.test;

import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class DataDrivenTest {

  private WebDriver driver;
  private String baseUrl;
 

 
  @BeforeMethod
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.wikipedia.org/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testUntitled() throws Exception {
    driver.get(baseUrl + "/wiki/Main_Page");
   
    //explicit wait for search field
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("searchInput")));

    driver.findElement(By.id("searchInput")).clear();     
    driver.findElement(By.id("searchInput")).sendKeys("India");

    driver.findElement(By.id("searchButton")).click();
 
  }

  @AfterMethod
  public void tearDown() throws Exception {
    driver.quit();   
  } 
}


Custom-expected condition: webdriver provide you to create custom wait condition.
Here is an example:

package com.test;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class DataDrivenTest {

  private WebDriver driver;
  private String baseUrl;
 

 
  @BeforeMethod
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.wikipedia.org/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test 
  public void testUntitled() throws Exception {
    driver.get(baseUrl + "/wiki/Main_Page");
   
   //custom explicit wait for search field   
   new WebDriverWait(driver, 10)
                           .until(new ExpectedCondition(){
                           @Override
                           public WebElement apply(WebDriver d) {
                           return d.findElement(By.id("searchInput"));
                           }});
   
   
    driver.findElement(By.id("searchInput")).sendKeys("India");
    driver.findElement(By.id("searchButton")).click();
  
  }

  @AfterMethod
  public void tearDown() throws Exception {
    driver.quit();   
  } 
}

6 comments:

  1. Thank you for this wonderful article.

    ReplyDelete
  2. Hi, Nice article; Could you advice if Imlicit Wait is set to 10 seconds how many times would the driver poll the DOM if element is not found? is it only twice or more if the element is not there? thaks NM pellasg@gmail.com

    ReplyDelete
  3. Hi, Nice Article. Got a question. How many times would webdriver poll the SOM if Implicit wait is set to 10 seconds ? is it only twice for any time set? Start and at the end of time set ... or is it every 500 mls checks? thanks

    ReplyDelete
  4. An implicit wait tells WebDriver or Selenium Scripts to poll the DOM for a certain amount of time when Selenium Scripts trying to find an Object/Element or Object/Elements if they are not visible or Interactable.
    xplicit wait tell the WebDriver to wait for certain time on basis of certain Expected conditions before throwing an “ElementNotVisibleException” exception ,Explicit wait is specific wait applied only for specified elements. Explicit wait have better flexibility then Implicit wait.
    For Detail With Best Example Link :

    http://www.qamantra.com/2018/01/how-to-use-explicit-and-implicit-wait.html

    ReplyDelete
  5. An implicit wait tells WebDriver or Selenium Scripts to poll the DOM for a certain amount of time when Selenium Scripts trying to find an Object/Element or Object/Elements if they are not visible or Interactable.
    xplicit wait tell the WebDriver to wait for certain time on basis of certain Expected conditions before throwing an “ElementNotVisibleException” exception ,Explicit wait is specific wait applied only for specified elements. Explicit wait have better flexibility then Implicit wait.
    For Detail With Best Example Link :

    http://www.qamantra.com/2018/01/how-to-use-explicit-and-implicit-wait.html

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution