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
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()
.
- new Function() and Apply() explained: https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html
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
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)
}
});
- A new instance of FluentWait is declared which incorporates your previously instantiated
driver
. - The FluentWait object
wait
is given a time-out of 30 seconds - Every 5 seconds, the
wait
will execute NoSuchElementException
will be ignored during the time-out- A functional interface is instantiated that takes your
driver
and returns the resulting WebElement, which is assigned tofoo
- the
apply
method implements the logic, taking your driver fromwait
as argument. findElement
looks for an element with an id-value offoo
until this is found according to the settings ofwait
. If it isn't found and instead time-out is reached, execution is aborted.
-
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– Niels van Reijmersdal2017年01月29日 21:24:53 +00:00Commented Jan 29, 2017 at 21:24