Friday, September 13, 2019

Post#45.Fluent Wait in Selenium


Wait Interface in Selenium
Wait is the generic interface, which makes selenium to wait for particular time with associated event. Events like element to be click-able, element to present.
Wait interface present under org.openqa.selenium.support.ui package, FluentWait and WebdriverWait implements Wait interface.

Until Method in WebdriverWait :
until is a abstract method present in Wait interface, whichever driver implements Webdriver the also must implement Wait interface.
Which means browser classes (FirefxDriver, ChromeDriver...) must implement methods present in Wait and Webdriver interfaces.
until method accepts a ExpectedConditions values as parameter, class must wait till the given condition neither becomes null nor false when they implement until method. So until methods implementation must not have return type as void.
FluentWait class implements Wait Interface and WebdriverWait class extends FluentWait class, this is called as multi-level inheritance in java.


Fluent Wait in Selenium

FluentWait class implements Wait interface in selenium, FluentWait object defines the maximum amount of time to wait for a condition.
Fluentwait in selenium webdriver is one of the most versatile wait which will give you the complete power to handle any kind of situation based on your requirement.
In a simple Statement,
Fluent wait = Maximum time to Wait + frequency with which control need to be checked on application +  exceptions to be ignored like NoSuchElementExceptions.
It defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions while waiting, such as NoSuchElementExceptions when searching for an element on the page.

Below is the example for common way of using Fluent Wait.

public void test(){

FluentWait<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("Element"));

}

}

FluentWait methods:
Below are the methods present in the FluentWait, we may not use all of the methods in our practical use, but we can use it reap the maximum benefit. In above example we have used below methods.

withTimeout :
This method set the time limit for Fluent wait i.e how much time to wait for an condition when Fluent wait is used. withTimeout method accepts two parameter first parameter is amount of time, and second parameter is what is the TimeUnit for tha amount of time. TimeUnit could be from Nano-seconds to Days.

pollingEvery :
Frequency of polling the webpage for particular element/condition, if give 10 Seconds FluentWait polls the webpage once in every 10 seconds. Default polling time in FluentWait is 500 Milli-seconds

ignoring :
ignoring method makes the fluent wait to ignore the particular exception threw during the wait time, in below line Fluent wait ignores if NoSuchElementException, and FluentWait continues to poll for the condition, ignoring method is overloaded and it can accept two Exceptions as well.

OtherMethods in fluent wait class:

ignoreAll :

Ignores a list of exceptions that occur during the wait time, we have to pass Collection type parameter to this method, collection type could be List, Set, Queue.

// create a array list to exceptions
List allExceptions = new ArrayList();
// add few exception to the list
allExceptions.add(NoSuchElementException.class);
allExceptions.add(ElementNotVisibleException.class);
allExceptions.add(NoSuchFrameException.class);
// ignore all the exception in the list
fw.ignoreAll(allExceptions);
withMessage :

given string message will be displayed at the final exception.
// custom message to show if exception occurs
fw.withMessage("THIS IS EXCEPTION MESSAGE");
timeoutException :

timeoutException is protected method of the FluentWait class, we have to override this method to raise our own messages.
@override
public RuntimeException timeoutException(String message, Throwable lastException) {
              throw new NoSuchFrameException(message, lastException);
  }

No comments:

Post a Comment