用于显式等待的Selenium Java Lambda实现

Vis*_*ady 7 java lambda selenium-webdriver

我正在尝试为selenium webdriver等实现Java Lambda概念.我需要转换自定义webdriver等待这样的事情

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

 (new WebDriverWait(driver(), 5)).until((driver) -> driver.findElement(By.linkText("")));
Run Code Online (Sandbox Code Playgroud)

但它与'until'引用的函数接口不匹配并抛出错误.

所以我尝试传递Lambda,因为它支持.

Attempt1

Predicate<WebDriver> isVisible = (dr) -> dr.findElement(
     By.linkText("")).isDisplayed();
     webDriverWait.until(isVisible);
Run Code Online (Sandbox Code Playgroud)

它有点工作,但不是我需要的,因为它只返回空白.

需要你的帮助或建议.

Mad*_*han 7

问题在于你的语法.下面对我来说非常合适

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

你的代码问题

 //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("")));
Run Code Online (Sandbox Code Playgroud)

  • 参考[this](http://stackoverflow.com/questions/14502654/automatically-infer-return-type-using-enum-parameter) (2认同)