17

How is the FluentWait is different from WebDriverWait? I am using WebDriverWait having the polling interval set to : 500 miliseconds. Still, WebDriverWait returns after some delay (i.e. 2 - 3 seconds after the element load is completed)? Any opinion here?

6
  • Hope this will help you : toolsqa.com/selenium-webdriver/implicit-explicit-n-fluent-wait Apr 30, 2015 at 8:33
  • Hi,Thanks much for the very helpful link. But, I didn't understand WebDriverWait is an implicit wait or explicit wait .
    – Dinesh
    Apr 30, 2015 at 11:43
  • If you read the documentation, you will see that both implicit and explicit are functionally equivalent. Both of them poll the DOM every 500 ms. Apr 30, 2015 at 11:51
  • Yeah got it ... But, I am still confused on : WebDriverWait or FluentWait ... any suggestions? Most of the time, I will have to wait for the element to be visible
    – Dinesh
    Apr 30, 2015 at 11:55
  • As per this question, WebDriverWait is and extension of FluentWait. Apr 30, 2015 at 12:46

4 Answers 4

20

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);
driver.get("http://somedomain/url_that_delays_loading");
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();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

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

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

WebElement foo = wait.until(new Function<WebDriver, WebElement>() 
{
  public WebElement apply(WebDriver driver) {
  return driver.findElement(By.id("foo"));
}
});
2
  • Thanks much for the detailed answer. Let me try out in my code to see the change in response time. I am having the following issue in my code : I am using webDriverWait for the loader to complete (ex : in a page, the set of data loads on clicking the checkbox and there is a loading icon appears to indicate user that, the loading is still in progress. I am using webDriverWait to identify if the loading icon is disappeared. The code is working fine, but the webDriverWait returns after few seconds of delay (2-3 seconds)webDriverWait returns with a lag of 2-3 seconds of loading icon disappeared.
    – Dinesh
    May 5, 2015 at 6:04
  • @LittlePanda. Your code for FluentWait is not valid Syntax. May 8, 2018 at 15:02
2

Implicit wait: Implicit wait tells web driver to wait on every instance when try to find element. It is like global wait for all driver.findelement instance. It will force web driver to wait until element is appeared on page or defined time whatever is earliest. Drawback is it throws exception when element is not loaded on page even in defined time span.

Explicit wait: Explicit wait is of two types:

1) WebDriverWait

2) FluentWait

both are classes and implements Wait interface.

WebDriverWait is applied on certain element with defined expected condition and time. This wait is only applied to the specified element. This wait can also throw exception when element is not found.

WebDriverWait wait = new WebDriverWait (driver, 20);
wait.until(ExpectedConditions.VisibilityofElementLocated(By.xpath(""//button[@value='Save Changes']"")));

Fluent wait: Fluent wait is another type of Explicit wait and you can define polling and ignore the exception to continue with script execution in case element is not found.

new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS).pollingevery(10, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
0

Implicit Wait is attached to the WebDriver instance. It will get invoked before performing any operation using driver instance.

Explicit Wait is Highly customizable. You can achieve Explicit Wait using Two classes:

  1. FluentWait (Class)
  2. WebDriverWait (Class)

FluentWait class has below methods to configure the wait.

  1. withTimeOut()
  2. pollingEvery()
  3. ignoring()
  4. until()

WebDriverWait class is an extension of FluentWait class. It doesn't have its own methods.

But it has certain constructors which will help you to configure your wait instance without invoking the methods.

WebDriverWait takes WebDriver instance as an argument, unlike FluentWait. For More Details watch this: https://youtu.be/FbGOhbzCoyk

0

Let me post my answer and finally answer to some of the FAQs:

Difference between FluentWait and WebDriverWait
Both are dynamic waits. FluentWait is a generic class that implements Wait interface whereas WebDriverWait is extending FluentWait.

enter image description here

enter image description here

1. Setting type:
WebDriver sets the type to WebDriver always but in FluentWait we shall set the type to any class including WebDriver.

2. Default Exception ignored Since the WebDriverWait extends FluentWait, it inherits all the methods of FluentWait. So both of them have a method - ignoring. FluentWait's ignoring method does not ignore any Exception by default. FluentWait's constructor is not calling ignoring() method. enter image description here

whereas WebDriverWait is calling ignoring() that ignores NotFoundException by default. enter image description here

If you wonder what is this NotFoundException, its a parent exception that is extended by 8 other exceptions including NoSuchElementException, which means if any such exception gets thrown, WebDriverWait handles it by default. In case of FluentWait, we have to add such exception deliberately.

enter image description here

Other than that, in my smaller knowledge both looks alike. We shall use all these methods in both FluentWait as well as WebDriverWaits: until(), ignoring(), withMessage(), withTimeout(), pollingEvery().

Now, trying to answer related questions:

  1. WebDriverWait has polling time of 500 milliseconds. What is the polling time of FluentWait?
    Same 500 milliseconds.enter image description here
    Its configurable in both the waits.

  2. We have seen people using ExpectedConditions used in WebDriverWait. Can we use ExpectedConditions on FluentWait as well?
    Yes, we can.enter image description here

  3. We have seen people passing user defined functions to FluentWait. Can we pass our own functions to WebDriverWait as well?
    Yes, we can.enter image description here

  4. How does these waits keep looping?
    If you look at the until() implementation, it first executes the function that is passed. While executing that function it may either gives us a result or throw an exception.

    In case of an exception, it checks whether the exception is part of our ignored exception, if so, continue the loop till timeout.

    In case of a return value, check whether the return value is null or false (for boolan types), if so, continue the loop till timeout.

    The loop will be broken if none of the above condition satisfies and the value/exception will be sent back.

    enter image description here

Summary In short, if you are using waits for WebDriver related operations, WebDriverWait is more than enough for you. If you want to use waits for other operations like calling an API number of times till it gives valid response or until a particular amount of max timeout occurs, FluentWait can be used. Hope this is quite detailed. Please upvote if you find this answer helpful. In case I make mistakes, please do mention it in the comments, will correct it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.