【问题标题】:Selenium Webdriver - Difference between WaitsSelenium Webdriver - 等待之间的区别
【发布时间】:2015-07-29 07:17:39
【问题描述】:

WebDriverWait 和 FluentWait 有什么区别。我了解 WebDriverWait 扩展了 FluentWait。 Webdriverwait 继承了 FluentWait 的所有方法。我什么时候可以使用 WebdriverWait 而不是 FluentWait?

【问题讨论】:

标签: selenium selenium-webdriver webdriver wait


【解决方案1】:

以下是一些外部网站/博客,它们以令人敬畏的方式解释了 selenium 中 WAIT 之间的区别:

1 - TOOLSQA - DIFFERENT SELENIUM WAITS

2 - BLOG - THESOFTWARETESTING.COM

3 - BLOG - SOFTWARE TESTING HELP

【讨论】:

    【解决方案2】:

    在 FluentWait 实现中,您可以配置 timeoutfrequency 来检查条件。除此之外,您还可以配置为忽略某些类型的异常。

    示例:

    // Waiting 30 seconds for an element to be present on the page, checking // for its presence once every 5 seconds. Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("foo")); } });

    您可以参考here

    【讨论】:

    • 同样的配置可以用 WebDriverWait 来完成。我正在寻找我宁愿使用 WebDriverWait 而不是 Fluentwait 的实例。我知道我的问题听起来有点笼统,我只想找出两者在用法上的区别。
    【解决方案3】:

    FluentWait 是 Wait 接口的实现,它可以动态配置超时和轮询间隔。

    每个 FluentWait 实例定义等待条件的最长时间,以及检查条件的频率。此外,用户可以将等待配置为在等待时忽略特定类型的异常,例如在页面上搜索元素时的 NoSuchElementExceptions。

    示例用法:

    // 等待一个元素出现在页面上的 30 秒,检查 // 因为它每 5 秒出现一次。

      Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);
    
     WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
      }
      });
    

    【讨论】:

    • 同样可以使用 WebDriverWait 来实现。我错了吗?
    • WebDriverWait 和 FluentWait 或多或少相同。WebDriverWait 是 Fl​​uentWait 的扩展,我看到的唯一区别是两者中的参数。在 FluentWait 中,我们有: sleepInMillis - 以毫秒为单位的睡眠时间WebDriverWait 中没有的投票。
    【解决方案4】:

    现在回答这个问题可能有点晚了。但是,我很想回答它。

    FluentWait 是一个泛型类,FluentWait 其中 T 可以是任何东西。它可能是一个 WebElement,也可能是您想要传递的自定义类。使用该类,您可以应用您的等待逻辑。注意 FluentWait 的 .until( or ) 方法。此方法将根据您在上面传递的 T 获取 Predicate 或 Function

    我们了解到,在 FluentWait 中,您可以传递任何引用类。这使得 FluentWait 对于您想要实现的任何等待逻辑都是通用的。

    相反,WebDriverWait 仅配置为 WebDriver。在 WebDriverWait.until 方法中,您始终可以创建 Predicate 或 Function

    所以在伪代码 WenDriverWait = FluentWait 中就是这样。了解 until 方法、函数和谓词。前段时间我在这里写了一个教程:http://toolsqa.com/selenium-webdriver/advance-webdriver-waits/

    【讨论】:

      猜你喜欢
      • 2017-04-19
      • 2012-11-15
      • 2014-05-04
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      相关资源
      最近更新 更多