Tuesday, February 16, 2016

Implicit/Explicit / Fluent Waits




WebDriver supports two waits in order to handle the synchronisation problems.
  • Implicit Wait
  • Explicit Wait
Let us discuss each of them in details considering practical approach.

WebDriver Implicit Wait

Implicit waits are used to provide a default waiting time (say 30 seconds) between each consecutive test step/command across the entire test script. Thus, subsequent test step would only execute when the 30 seconds have elapsed after executing the previous test step/command.
Syntax : Syntax
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
The above snippet code will take two parameters, the first indicates the tie in numeric that system need to wait and the second parameter indicates the time measurement. so in this case it will wait for 10 seconds, and Once set, the implicit wait is set for the life of the WebDriver object instance.

Ex. 

WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://timetracker.anuko.com/login.php"); WebElement loginelement = driver.findElement(By.id("login"));


When to use: Not recommended

WebDriver explicit waits

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://timetracker.anuko.com/login.php");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.visibilityOf(By.id("login")));

Please refer this docs for more details for other method options. 

Will see live implementation :
1. Launch google chrome 
2. open time tracker login page
3. before enter the username data check whether element is present or not and then enter the value
4. Also will create a common method to wait for element present



Fluent wait :


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).

Ex :



// 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)
    .ignoring(NoSuchElementException.class);

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



Example that covers fluent waits 






Some more option :


The following expected conditions can be used for finding a web element.
It is recommended that they are used instead driver.findElement().