Selenium WebDriver Java:将表达式转换为Lambda
我正在寻找一种使用lambda编写此块的方法:Selenium WebDriver Java:将表达式转换为Lambda,java,selenium,selenium-webdriver,lambda,Java,Selenium,Selenium Webdriver,Lambda,我正在寻找一种使用lambda编写此块的方法: public void waitUntilElemIsDisabled(WebElement element) { try { wait.until(new Function<WebDriver, Boolean>() { @Override public Boolean apply(WebDriver driver) {
public void waitUntilElemIsDisabled(WebElement element) {
try {
wait.until(new Function<WebDriver, Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return !element.isEnabled();
}
});
} catch (Exception e) {
e.printStackTrace();
logger.error(e.toString());
}
}
如果有帮助,下面是我如何初始化等待:
wait = new FluentWait<>(webDriver).withTimeout(impTimeout, s).pollingEvery(pollingMsInt, ms)
.ignoring(NoSuchElementException.class);
wait=new FluentWait(webDriver)。带超时(impTimeout,s)。轮询每一个(pollingMsInt,ms)
.忽略(NoSuchElementException.class);
我在使用lambda方面相当新,目前正在遵循指南,但我找不到与我正在使用的模式匹配的模式
提前谢谢。设法解决了这个问题
解决方案:
private Function<WebDriver, Boolean> webElemBooleanFunction(boolean condition) {
return x -> condition;
}
谢谢你的提示 看起来您可能正在使用不推荐的until()
方法。看看这个,我经常使用IDE的重构功能来代替用我的大脑来解决这些问题。在IntelliJ中,我会单击lambda,使用Refactor/Extract-to-Value,根据需要更正类型参数,然后使用Refactor/Inline将值重新集成到原始表达式中。通常,它会自动添加任何需要的强制转换或显式类型参数。
private Function<WebDriver, Boolean> webElemBooleanFunction(boolean condition) {
return x -> condition;
}
public void waitUntilFieldIsPopulated(WebElement element) {
try {
wait.until(webElemBooleanFunction(getElementValue(element).length() > 0));
} catch (Exception e) {
e.printStackTrace();
logger.error(e.toString());
}
}