Nisha
Nisha

Reputation: 15

Selenium Webdriver - Difference between Waits

What is the difference between WebDriverWait and FluentWait. I understand WebDriverWait extends FluentWait. Webdriverwait has derived all the methods from FluentWait. When would I use WebdriverWait instead of FluentWait?

Upvotes: 0

Views: 623

Answers (4)

virusrocks
virusrocks

Reputation: 871

This might be little late to answer the question. However, I am tempted to answer it.

FluentWait is a generic class, FluentWait<T> where T can be anything. It could be a WebElement or may be a custom class that you want to pass on. Using the class you can apply your wait logic. Pay attention to .until(<Predicate> or <Function>) method of FluentWait. This method will get Predicate<T> Or Function<T,Y> based on what T you have passed above.

As we learnt that in FluentWait you can pass any reference class. This makes FluentWait generic for any wait logic that you want to implement.

On the contrary WebDriverWait is configured for WebDriver only. In the WebDriverWait.until method you can always create a Predicate<WebDriver> or Function<WebDriver, Y>.

So in pseudo code WenDriverWait = FluentWait<WebDriver> that is all it is. To understand the until method, Functions and Predicated. I wrote a tutorial some time back here: http://toolsqa.com/selenium-webdriver/advance-webdriver-waits/

Upvotes: 0

Rishu Saxena
Rishu Saxena

Reputation: 169

FluentWait is an implementation of the Wait interface that may have its timeout and polling interval configured on the fly.

Each FluentWait instance 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 whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

Sample usage:

// 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"));
  }
  });

Upvotes: 0

Helping Hands
Helping Hands

Reputation: 5396

Here are some external sites/blogs which explain difference between WAITs in selenium in awesome way :

1 - TOOLSQA - DIFFERENT SELENIUM WAITS

2 - BLOG - THESOFTWARETESTING.COM

3 - BLOG - SOFTWARE TESTING HELP

Upvotes: 1

Paras
Paras

Reputation: 3235

In FluentWait implementation you can configure the timeout and frequency with which to check the condition. In addition to that you can configure to ignore certain type of exceptions.

Example:

// 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")); } });

You can refer it here

Upvotes: 0

Related Questions