Selenium文档中until()
方法的语法如下:
public <V> V until(java.util.function.Function<? super T,V> isTrue)
其用法类似:
WebDriver wait = new WebDriver(driver, 20);
WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("lgn-btn")));
我与until()
方法的语法和用法无关。我想知道语法的实现方式。
是的,我了解泛型,我们使用它来了解编译时的错误,以便可以在运行时避免ClassCastException。另外,我了解功能接口,我们使用它来实现行为参数化。
我没有得到的是java.util.function.Function<? super T,V> isTrue)
和ExpectedConditions.elementToBeClickable(By.id("id))
之间的对等。
java.util.function.Function<? super T,V> isTrue
表示什么?
您的问题中提到了四个不同的主题,您可以在下面找到详细信息:
java.util.function程序包包括功能接口,它为lambda表达式和方法引用提供了目标类型。
一些例子是:
BiConsumer<T,U>
:表示一个接受两个输入参数并且不返回结果的操作。BiFunction<T,U,R>
:表示一个接受两个参数并产生结果的函数。BinaryOperator<T>
:表示对两个相同类型的操作数的运算,产生与操作数相同类型的结果。BiPredicate<T,U>
:表示两个参数的谓词(布尔值函数)。Consumer<T>
:表示一个接受单个输入参数并且不返回结果的操作。Function<T,R>
:表示一个接受一个参数并产生结果的函数。public class FluentWait<T>
类扩展了public class FluentWait<T>
并实现了java.lang.Object
,这意味着它是Wait<T>
接口的一种实现,可以动态配置其超时和轮询间隔。每个FluentWait实例都定义了等待条件的最长时间,以及检查条件的频率。此外,用户可以配置等待以在等待时忽略特定类型的异常,例如在页面上搜索元素时的Wait。
其中一个修饰符是:
NoSuchElementExceptions
此实现将实例的输入值重复应用于给定功能,直到发生以下情况之一:
Modifier and Type Method and Description
----------------- ----------------------
<V> V until(java.util.function.Function<? super T,V> isTrue)
Specified by:
until in interface Wait<T>
Type Parameters:
V - The function's expected return type.
Parameters:
isTrue - the parameter to pass to the ExpectedCondition
Returns:
The function's return value if the function returned something different from null or false before the timeout expired.
Throws:
TimeoutException - If the timeout expires.
接口扩展了public interface ExpectedCondition<T>
,该模型对一个条件进行了建模,该条件的期望值不能为null或false。示例包括确定网页是否已加载或元素可见。
注意,期望
public interface ExpectedCondition<T>
是幂等的。它们将由com.google.common.base.Function<WebDriver,T>
循环调用,对被测应用程序状态的任何修改都可能会产生意想不到的副作用。
ExpectedConditions
类是固定的WebDriverWait
,通常在Webdriver测试中有用。
一些用法示例:
ExpectedCondition
elementToBeClickable()
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_css")));
visibilityOfElementLocated()
:
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element_css")));
frameToBeAvailableAndSwitchToIt()
:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("element_css")));
visibilityOfAllElementsLocatedBy()
:
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("element_css")));