【问题标题】:How does FluentWait in Selenium implements the until() methodSelenium 中的 FluentWait 如何实现 until() 方法
【发布时间】:2019-02-06 13:53:23
【问题描述】:

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&lt;? super T,V&gt; isTrue)ExpectedConditions.elementToBeClickable(By.id("id)) 之间的等价性。

java.util.function.Function&lt;? super T,V&gt; isTrue这个表达式是什么意思?

【问题讨论】:

标签: java selenium selenium-webdriver webdriverwait fluentwait


【解决方案1】:

您的问题中提到了四个不同的主题,您可以在下面找到详细信息:

java.util.function

java.util.function 包包含 函数接口,它们为 lambda 表达式 和方法引用提供目标类型。

举几个例子:

  • BiConsumer&lt;T,U&gt;:表示接受两个输入参数且不返回结果的操作。
  • BiFunction&lt;T,U,R&gt;:表示一个接受两个参数并产生结果的函数。
  • BinaryOperator&lt;T&gt;:表示对两个相同类型的操作数进行操作,产生与操作数相同类型的结果。
  • BiPredicate&lt;T,U&gt;:表示两个参数的谓词(布尔值函数)。
  • Consumer&lt;T&gt;:表示接受单个输入参数且不返回结果的操作。
  • Function&lt;T,R&gt;:表示接受一个参数并产生结果的函数。

类 FluentWait

public class FluentWait&lt;T&gt; 类扩展了java.lang.Object 并实现了Wait&lt;T&gt;,这意味着它是Wait 接口的实现,可以动态配置其超时和轮询间隔。每个 FluentWait 实例定义等待条件的最长时间,以及检查条件的频率。此外,用户可以将等待配置为在等待时忽略特定类型的异常,例如在页面上搜索元素时的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.

此实现重复将此实例的输入值应用于给定函数,直到发生以下情况之一:

  • 函数既不返回null也不返回false
  • 函数抛出未忽略的异常
  • 超时到期
  • 当前线程被中断

接口预期条件

public interface ExpectedCondition&lt;T&gt; 接口扩展了com.google.common.base.Function&lt;WebDriver,T&gt;,它模拟了一个预期评估为既不为空也不为假的条件。示例包括确定网页是否已加载或元素是否可见。

请注意,ExpectedConditions 是幂等的。它们将被WebDriverWait 循环调用,并且对被测应用程序状态的任何修改都可能产生意想不到的副作用。


类预期条件

ExpectedConditions 类是罐装的ExpectedConditions,通常在 webdriver 测试中很有用。

几个使用示例:

  • 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")));
    
  • attributeContains():

    new WebDriverWait(driver, 20).until(ExpectedConditions.attributeContains(driver.findElement(my_element), "src", "https"));
    

【讨论】:

  • 感谢您的回答。您能否详细说明until(java.util.function.Function&lt;? super T,V&gt; isTrue) 等于until(ExpectedConditions.elementToBeClickable(By.id("lgn-btn")))
  • FluentWait 类修饰符之一是until(java.util.function.Function&lt;? super T,V&gt; isTrue)。其中until(ExpectedConditions.elementToBeClickable(By.id("lgn-btn")))ExpectedConditions 类之一的用法,它是ExpectedCondition 接口的固定形式,它实际上扩展了com.google.common.base.Function&lt;WebDriver,T&gt;。虽然术语听起来很相似,但它们是不同的。花点时间浏览其中嵌入的链接。如有任何疑问,请随时提问。
猜你喜欢
  • 1970-01-01
  • 2019-01-17
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
相关资源
最近更新 更多