I am struggling to figure out how the WebDriver instance is passed to Selenium's expected conditions.
So I have a simple WebDriverWait for the visibility of a web element:
new WebDriverWait(webDriver, Configuration.WEB_DRIVER_WAIT_TIMEOUT)
.until(ExpectedConditions.visibilityOf(element));
The webDriver instance here is a chromedriver which has been instantiated above.
My question is: in the method visibilityOf():
public static ExpectedCondition<WebElement> visibilityOf(final WebElement element) {
return new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return elementIfVisible(element);
}
@Override
public String toString() {
return "visibility of " + element;
}
};
}
Just above, how and which instance of WebDriver gets passed to the apply()? I understand that the ExpectedCondition implements the Function Interface
public interface ExpectedCondition<T> extends Function<WebDriver, T> {}
which takes as first argument WebDriver.
How the instance of the WebDriver gets passed to ExpectedCondition of the visibilytOf()?
Thanks
When you are creating the WebDriverWait
you are passing the webdriver instance. The same instance will be passed to the apply
method of the functional interface of ExpectedCondition
by the until
method.
WebDriverWait
extends FluentWait<WebDriver>
which actually implements the until
method. When you construct the WebDriverWait
the driver is passed as a parameter to FluentWait
which keeps it in a attribute.
Now when you call until
on WebDriverWait
the until
method calls the apply
method passing the driver
you actually passed to the constructor. The ExpectedCondition
object that you are constructing in-effect receives two inputs. The WebElement
you are passing to it as well as the driver
that you pass to WebDriverWait
.
The source code is not too complicated - you can have a look at it for finer details :)