Wednesday, 16 March 2016

Webdriver | Implicit wait vs Explicit wait vs Fluentwait

Implicit Wait:
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

When to use: Not recommended

Explicit wait:
An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully.

WebDriver driver = new FirefoxDriver();
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
WebDriver Explicit Waits
Explicit Waits

When to use: If element takes a long time to load. Also, used to check property of an element (presence, clickability. etc).

For each FluentWait instance, you can specify:

  1. Frequency with which FluentWait has to check the conditions defined.
  2. Ignore specific types of exception waiting such as NoSuchElementExceptions while searching for an element on the page.
  3. Maximum amount of time to wait for a condition

When to use FluentWait: When you try to test the presence of an element that may appear after every x seconds/minutes (Just an example, this is my guess of where such a thing can be used).

// Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(30, SECONDS)
    .pollingEvery(5, SECONDS)

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

Difference Between Implicit, Explicit and Fluent Wait
Implicit Wait:
1.      During Implicit wait if the Web Driver cannot find it immediately because of its availability,
2.      The WebDriver will wait for mentioned time and it will not try to find the element again during the specified time period.
3.      Once the specified time is over, it will try to search the element once again the last time before throwing exception.
4.      The default setting is zero.
5.      Once we set a time, the Web Driver waits for the period of the WebDriver object instance.
Explicit Wait:
·         Suppose there is a scenario, when a particular element takes more than a minute to load.
·         In that case we would definitely not like to set a huge time to implicit wait, as if we do this, Browser will go to wait for the same time for every element.
In order to avoid such situation,
1.      Introduce a separate time on the required element only.
2.      By following this browser implicit wait time would be short for every element and it would be large for specific element.
Fluent Wait:
Scenario:  Suppose there is an element which takes some time to appear may be in just 1 second & some time it takes minutes to appear.
In that case it is better to use fluent wait,  1 .As this will try to find element again and again until it find it or until the final timer runs out.
Solutions: We always get confuse when it comes to using Wait commands, to better understand it we need to remember that there is a difference between several scenarios:
An element not being present at all in the DOM.
An element being present in the DOM but not visible.
An element being present in the DOM but not enabled. (i.e. clickable)
There are pages which get displayed with the JavaScript, the elements are already present in the browser DOM, but are not visible. The implicit wait only waits for an element to appear in the DOM, so it returns immediately, but when you try to interact with the element you get aNoSuchElementException. You could test this hypothesis by writing a helper method that explicit wait for an element to be visible or clickable.
public WebElement getWhenVisible(By locator, int timeout) {
WebElement element = null;
WebDriverWait wait = new WebDriverWait(driver, timeout);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
return element;
public void clickWhenReady(By locator, int timeout) { WebElement element = null; WebDriverWait wait = new WebDriverWait(driver, timeout); element = wait.until(ExpectedConditions.elementToBeClickable(locator)); element.click(); }

Which one is right ?