Single Post

Header

Tuesday, January 12, 2016

Difference Between Implicit and Explicit waits in Selenium WebDriver

Difference Between Implicit and Explicit waits in Selenium WebDriver

Implicit Wait 

Implicit Wait is basically the internal timeout for the WebDriver for WebElement we are searching to be available. In this case, we are telling WebDriver that it should wait for certain timeout in case of specified element not available on the UI (DOM). Implicit wait is set to all the WebElement we are using in our script. It will keep on searching for the element again and again for certain time period and if it is not found at last then it throws ‘NoSuchElementException’. The default implicit timeout is set to zero.

Explicit Wait 

In Explicit Wait, we write certain type of code which is used like wait and used as per our convenience. It reaches its timeout period until certain condition is satisfied. It is a custom kind of code for particular WebElement for waiting for particular timeout. Explicit Wait is intelligent kind of wait which provide us better option that Implicit Wait. But Explicit Wait can be applied for particular specified elements.

There are many classes by WebDriver like ‘WebDriverWait’, ‘ExpectedCondition’ which are used for implementing Explicit wait.

These days for implementing this we frequently use ‘Thread.Sleeep()’ in our code, which wait exactly for the same time period we provide in brackets. But this is not the good idea to implement Explicit wait.

package SeleniumTestNG;

import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

public class ImplicitAndExplicitWaits {

  @Test
  public void implicitWait() {
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);  // wait upto 8 seconds for finding any type of element on the web page 
    driver.get("https://www.redbus.in/");
    driver.manage().window().maximize(); // this type of code might not work for implicit wait
    driver.findElement(By.id("txtSource")).sendKeys("Bangalore");
    driver.findElement(By.id("txtDestination")).sendKeys("Guntur");
    driver.findElement(By.id("txtOnwardCalendar")).click();
    int n = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).size();
    System.out.println("the months are "+n);
    WebElement nextMonth = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).get(1);
    nextMonth.findElements(By.cssSelector(".wd.day")).get(0).click();
    driver.findElement(By.id("searchBtn")).click();
    System.out.println("the number of buses are"+driver.findElements(By.cssSelector(".viewSeatsBtn")).size());  // Mainly the implicit wait applies here to identify the particular button on the next web page.
    assertEquals(true,driver.findElements(By.cssSelector(".viewSeatsBtn")).size()!=0);
    driver.quit();
  }

  @Test
  public void withoutImplicitOrExplicitWait() {
    WebDriver driver = new FirefoxDriver();
  //  driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
    driver.get("https://www.redbus.in/");
  // driver.manage().window().maximize(); // this type of code might not work for implicit wait
    driver.findElement(By.id("txtSource")).sendKeys("Bangalore");
    driver.findElement(By.id("txtDestination")).sendKeys("Guntur");
    driver.findElement(By.id("txtOnwardCalendar")).click();
    int n = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).size();
    System.out.println("the months are "+n);
    WebElement nextMonth = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).get(1);
    nextMonth.findElements(By.cssSelector(".wd.day")).get(0).click();
    driver.findElement(By.id("searchBtn")).click();
    assertEquals(true,driver.findElements(By.cssSelector(".viewSeatsBtn")).size()!=0); 
    // it would fail in the above line since the view seats buttons are not visible immediately since they are in the next web page
    driver.quit();
  }

  @Test
  public void explicitWait() {
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.redbus.in/");
  // driver.manage().window().maximize(); // this type of code might not work for implicit wait
    driver.findElement(By.id("txtSource")).sendKeys("Bangalore");
    driver.findElement(By.id("txtDestination")).sendKeys("Guntur");
    driver.findElement(By.id("txtOnwardCalendar")).click();
    int n = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).size();
    System.out.println("the months are "+n);
    WebElement nextMonth = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).get(1);
    nextMonth.findElements(By.cssSelector(".wd.day")).get(0).click();
    driver.findElement(By.id("searchBtn")).click();
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".gtm_availableseatsort"))); // Look for the particular element using this condition
    System.out.println("the number of buses are "+driver.findElements(By.cssSelector(".viewSeatsBtn")).size());
    assertEquals(true,driver.findElements(By.cssSelector(".viewSeatsBtn")).size()!=0);
    driver.quit();
  }


}

3 comments:

  1. Trade Stocks, Forex, And Bitcoin Anywhere In The World:exness login Is The Leading Provider Of Software That Allows You To Trade On Your Own Terms. Whether You Are Operating In The Forex, Stock, cgin Software And Anonymous Digital Wallet To Connect With The Financial World.: exness login Is A Currency Trading Company That Allows You To Trade Stocks, Forex, And Cryptocurrency.

    ReplyDelete
  2. During a OctaFx Review workshop, each attendee will learn how to interpret market action using proven charting techniques, searching for reliable chart patterns and indicators to help them identify trading opportunities.

    ReplyDelete
  3. Are You Aware Of VT Markets Minimum Deposit And Withdrawal Amounts?

    ReplyDelete