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.

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

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

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.