无法理解FluentWait中的until()方法的实现 [英] Can not understand the implementation of until() method in FluentWait
问题描述
对于Predicate<T>
和Function<? super T, V>
接口作为参数,org.openqa.selenium.support.ui.FluentWait
中的until()
方法已重载.
The until()
method in org.openqa.selenium.support.ui.FluentWait
is overloaded for Predicate<T>
and Function<? super T, V>
interfaces as parameters.
它应该采用以下(实现apply()
方法的)之一作为参数:
It should take as argument one of the following (which implement the apply()
method):
- 匿名类
- Lambda表达式
- 方法参考
我为此方法定义为参数的任何lambda都会引发以下错误:
Any lambda that I define as argument for this method throws following error:
WebDriverWait类型的直到(Predicate)方法不明确
The method until(Predicate) is ambiguous for the type WebDriverWait
我的Lambda:
x -> x.findElement(byLocator).isDisplayed()
我认为对于任何lambda都是如此,因为对于Function或Predicate的apply()
都可以通过使用这些lambda来实现.
I assume this will be the case for any lambda, as apply()
for either Function or Predicate can be implemented by use of these lambda.
所以我的问题是,以谓词为参数的直到方法的用途是什么?
已更新: 删除了由@drkthng回答的问题的第一部分.
Updated: Removed the first part of question which was answered by @drkthng.
推荐答案
回答第一个问题:
看看我引用:
public interface ExpectedCondition<T>
extends com.google.common.base.Function<WebDriver,T>
您会看到,ExpectedCondition是从Google的Function接口继承的,因此您可以将其用作till()方法的参数.
You see, that ExpectedCondition inherited from google's Function interface, thus you can use it as an argument for the until() method.
关于第二个问题:
我认为您不能像那样交出lambda.直到方法正在等待谓词或函数(如您正确提到的那样)为止.
I don't think you can hand over a lambda just like that. Until method is waiting for a Predicate or Function (as you correctly mentioned).
Regarding the differences between Predicates and lambdas, have a look for example here
因此,您仍然可以使用lambda-expressions尝试使用类似方法:
So you can try something like this instead, still using your lambda-expressions:
Predicate<WebDriver> myPredicate = x ->x.findElement(By.id("id")).isDisplayed();
WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver, 10);
wait.until(myPredicate);
这篇关于无法理解FluentWait中的until()方法的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!