Tuesday, January 26, 2016

Selenium WebDriver Waits : Explicit Vs Implicit

 During test execution, we may require test execution to wait for a particular period of time. The time to wait may depend on following factors:

  • Waiting for specific process to complete, which can be waiting for some condition in the database or some external process to complete.
  • Waiting for specific objects/Page to load completely.
  • Waiting for specific object to be enabled or disabled.
  • Failure in automation execution due to exceptions for objects being not available in the DOM.

Problem Statement of this article

  • What are different types of waits in Selenium WebDriver.
  • What are implicit waits, explicit waits in selenium.

What are different types of waits in Selenium WebDriver?

The different types of waits, we can use in Selenium WebDriver are:

  • Implicit Waits.
  • Explicit Waits
  • Hard Coded waits using thread.sleep

What are implicit Waits?

  • Implicit wait is not defined for specific object but checks for the existence of each element used and are implied until the driver instance is closed.
  • Implicit waits will wait for element to appear in the Document Object Model.

    • Implicit wait once set remains for the instance of WebDriver object.
    • Implicit wait tells Webdriver to poll the DOM for a certain amount of time to wait for availability of object.
    • In case object is found before the set wait time, it will move to the next step in the script.

    WebDriver driver = new InternetExplorerDriver();  
    driver.manage().timeouts().implicitlyWait(150, TimeUnit.SECONDS);  

    What are Explicit waits?


    • Using Explicit waits,execution of code is paused to wait for specific condition to be satisfied, before continuing with further execution.
    • We can provide explicit wait by using WebDriverWait and providing expected conditions.
    • There are two types of explicit waits in Selenium Webdriver
      • WebDriverWait
      • Fluent Waits

    How are fluent Waits implemented in selenium webdriver?

    Below code explains how fluent waits are implemented in selenium webdriver.

    FluentWait<WebDriver> flwait = new FluentWait<WebDriver>(driver);  
    //define the maximum time for the object to be available  
    flwait.withTimeout(5000, TimeUnit.MILLISECONDS);  
    // define the poll time for object to be loaded.  
    flwait.pollingEvery(400, TimeUnit.MILLISECONDS);  
    driver.get(url);  
    flwait.until(ExpectedConditions.titleContains("qaautomationqtp.blogspot.in"));  
    //We can also define to ignore specific types of exceptions while using fluent waits.  
    flwait.ignoring(NoSuchElementException.class); 

    How are WebdriverWait implemented in Selenium Webdriver?

    WebdriverWait is an extension of fluent class but has less functionality compared to fluent class for waiting for an object including pooling time and ignore settings.

     WebDriverWait wdwait = new WebDriverWait(driver, 50)  
     wdwait.until(ExpectedConditions.elementToBeClickable(By.Id(“Loginbtn")));  

    How to hard code the pause time for execution?

    Thread.sleep – This is hard code wait. Execution will pause for the time provided as argument in thread.sleep

    For e.g.: Thread. Sleep (10000) will make the script pause for 10 sec. Thread.sleep is useful for waiting for some external processes to complete. e.g: Interaction with database.
    If used, thread.sleep should be used with while loop with small time for wait in each of the loop.

    What are different expected conditions for which we can define explicit waits?

    Below  are the different ways to use expected condition based on which we can add explicit wait in the object.


    a. presenceOfElementLocated - Verify presence of element in the DOM.

    WebDriverWait wbwait = new WebDriverWait(driver, 1000);
    wbwait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Testing"))); 

    b. Using elementToBeClickable – verify element is present and clickable


    wbwait.until(ExpectedConditions.elementToBeClickable(By.linkText("Testing")));

    c. Using invisibilityOfElementLocated

    wbwait.until(ExpectedConditions.invisibilityOfElementLocated(By.linkText("Testing")));

    d. invisibilityOfElementWithText - Validating the invisibility of element with text for the element provided.

    wbwait.until(ExpectedConditions.invisibilityOfElementWithText(By.xpath("//div[@id='_ttE']"), " Testing ")); 


    e. textToBePresentInElement - Validating the text to be present in the element defined by locator.

    wbwait.until(ExpectedConditions.textToBePresentInElement(By.xpath("//div[@id='_tt545']")," Testing")); 

    f. visibilityOfElementLocated by locator.

    wbwait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='_tt5433']"))); 

    g. titleContains - Wait until the title is displayed correctly

    wbwait.until(ExpectedConditions.titleContains("Testing"));

    h. alertIsPresent - waits for alert to appear in the window.

    wbwait.until(ExpectedConditions.alertIsPresent());


    1 comment: