Thursday, December 25, 2014

Difference between implicit wait and explicit wait in Selenium Webdriver




Implicit Wait

1.     Implicit wait provide to load DOM object for a particular of time before trying to locate element on page.
2.     Default implicit wait is 0.
3.     We need to set implicit wait ,it will apply for Web driver Object.
  1. Implicit wait time is applied to all elements in your script
5.     But the disadvantage is it will slow down out test execution

i.e) driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);


Example program

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class implicitwait {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
driver.close();
}
}


Explicit wait

1.     An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code.
2.     With the help of some predefined ExpectedConditions we can make Selenium to wait for elements to become clickable, visible, invisible, etc.
      WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

Some of the condition used along with ExpectedConditions.

·                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 program.

package wait;
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 test_wait {
 private WebDriver driver;
private String Homepage;
@BeforeMethod
 public void setUp() throws Exception {
driver = new FirefoxDriver();
 Homepage = "http://www.google.com";
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }
@Test
public void testtextbox() throws Exception {
    driver.get(Homepage);
    //explicit wait for search field
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("gbqfq")));
    driver.findElement(By.id("gbqfq")).clear();     
    driver.findElement(By.id("gbqfq")).sendKeys("automationplace.blogspot");

  }
  @AfterMethod

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

  } 

}

Hop you like this post.
Hit LIKE button on facebook

No comments:

Post a Comment