Implicit wait Vs. Explicit wait

Implicit wait

 WebDriver driver = new FirefoxDriver();
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  

Implicit waits are basically your way of telling WebDriver the latency that you want to see if specified web element is not present that WebDriver looking for. So in this case, you are telling WebDriver that it should wait 10 seconds in cases of specified element not available on the UI (DOM).

  • Implicit wait time is applied to all elements in your script

Explicit wait (By Using FluentWait)

public static WebElement explicitWait(WebDriver driver,By by)
{
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
             .withTimeout(20, TimeUnit.SECONDS)
             .pollingEvery(2, TimeUnit.SECONDS)
             .ignoring(NoSuchElementException.class); 

     WebElement element= wait.until(new Function<WebDriver, WebElement>() {
           public WebElement apply(WebDriver driver) {
             return driver.findElement(By.id("foo"));
            }
      });
  return element;
 }

Explicit waits are intelligent waits that are confined to a particular web element. Using explicit waits you are basically telling  WebDriver at the max it is to wait for X units of time before it gives up.

  • Explicit wait time is applied only for particular specified element.
  •  In Explicit you can configure, how frequently (instead of 2 seconds) you want to check condition
Explicit wait
 
public static WebElement explicitWait(WebDriver driver,By by)
 {
      WebDriverWait wait = new WebDriverWait(driver, 30);
      wait.until(ExpectedConditions.presenceOfElementLocated(by));
 }

In above method it will wait until either ExpectedConditions become true or Timeout (30 sec).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s