0

I have this piece of code for a FluentWait.

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

I understand everything in the first part. Please explain how new Function<WebDriver, WebElement> works in the second part.

I have looked at the source code (link) from the package com.google.common.base but couldn't wrap my head around it.

Niels van Reijmersdal
32.7k4 gold badges59 silver badges125 bronze badges
asked Jan 29, 2017 at 10:00

2 Answers 2

2

The FluentWait example in the Selenium documentation is a bit different and simpler.

 Wait wait = new FluentWait(driver)
 .withTimeout(30, SECONDS)
 .pollingEvery(5, SECONDS)
 .ignoring(NoSuchElementException.class);
 WebElement foo = wait.until(new Function() {
 public WebElement apply(WebDriver driver) {
 return driver.findElement(By.id("foo"));
 }
 });

The until() function repeats the public apply() function every 5 seconds until it returns a WebElement or until the 30 seconds have passed. The findElement() function returns a NoSuchElementException when the element is not found, but this is ignored. If a WebElement is found it is placed into foo.

The new Function<WebDriver, WebElement>() is an extended version of the default Java new Function().

Why these extra types have been added to your example is unclear to me. And I am not a Java Guru. They might not be necessary, but probably it makes sure you really are using a WebDriver object and not something else.

From reading the documentation of Function I think that you have to pass these Type arguments in newer versions of Java.

Java language specific details are better asked on StackOverflow.com instead of on the SQA & testing.SE

answered Jan 29, 2017 at 21:23
0
0
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) (1)
 .withTimeout(30, SECONDS) (2)
 .pollingEvery(5, SECONDS) (3)
 .ignoring(NoSuchElementException.class); (4)
WebElement foo = wait.until(new Function<WebDriver, WebElement>() { (5)
 public WebElement apply(WebDriver driver) { (6)
 return driver.findElement(By.id("foo")); (7)
 }
 });
  1. A new instance of FluentWait is declared which incorporates your previously instantiated driver.
  2. The FluentWait object wait is given a time-out of 30 seconds
  3. Every 5 seconds, the wait will execute
  4. NoSuchElementException will be ignored during the time-out
  5. A functional interface is instantiated that takes your driver and returns the resulting WebElement, which is assigned to foo
  6. the apply method implements the logic, taking your driver from wait as argument.
  7. findElement looks for an element with an id-value of foo until this is found according to the settings of wait. If it isn't found and instead time-out is reached, execution is aborted.
answered Jan 29, 2017 at 20:04
1
  • The original question was how do steps 5/6/7 work. WebDriver is driving seems like an over simplification of what is happening. Commented Jan 29, 2017 at 21:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.