Selenium Java Lambda for Explicit Expectations

I am trying to implement the Java Lambda concept to wait for selenium webdriver. I need to convert a custom webdriver, expecting something like this

(new WebDriverWait(driver(), 5)) .until(new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver d) { return d.findElement(By.linkText("")); } }); 

to

  (new WebDriverWait(driver(), 5)).until((driver) -> driver.findElement(By.linkText(""))); 

But it does not coincide with the functional interface "before" and refers to an error.

So, I tried to pass the Lambda when it supports.

Attempt1

 Predicate<WebDriver> isVisible = (dr) -> dr.findElement( By.linkText("")).isDisplayed(); webDriverWait.until(isVisible); 

This works, but not what I require, because it only returns void.

You need your help or advice.

+7
java lambda selenium-webdriver
source share
2 answers

The problem is your syntax. Below I worked great for me

 WebElement wer = new WebDriverWait(driver, 5).until((WebDriver dr1) -> dr1.findElement(By.id("q"))); 

Your Code Problem

  //What is this driver() is this a function that returns the driver or what //You have to defined the return type of driver variable in until() function //And you cant use the same variable names in both new WebdriverWait() and until() see my syntax (new WebDriverWait(driver(), 5)).until((driver) -> driver.findElement(By.linkText(""))); 
+4
source share

Since the WebDriverWait#until method is overloaded: before (Function) and before (Predicate) , the compiler cannot infer the type of the argument to the lambda method.

In this case, both function interfaces Function and Predicate take one argument, which in this case is either <? super T> <? super T> , or T Therefore, declarative syntax is required here:

new WebDriverWait(driver, 5).until((WebDriver dr1) -> dr1.findElement(By.id("q")));

Note that lambda return types do not help the compiler distinguish between these cases. In your example, since #findElement returns a WebElement , this is conceptual enough to distinguish Function from Predicate , but clearly not enough for the compiler.

See also: http://tutorials.jenkov.com/java/lambda-expressions.html#parameter-types

+3
source share

All Articles