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.

1

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
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 wait is declared
  2. this new instance of wait has a time-out of 30 seconds
  3. Every 5 seconds, the script will look at wether the element you are looking for is present, this will be repeated until 30 seconds has elapsed.
  4. NoSuchElementException exception is to be ignored should it is invoked.
  5. You apply this dynamic wait onto this element foo.
  6. WebDriver driver is driving
  7. Element foo is to be searched by its id.
  • The original question was how do steps 5/6/7 work. WebDriver is driving seems like an over simplification of what is happening. – Niels van Reijmersdal Jan 29 '17 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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