【问题标题】:Probable impact of implicit wait removal隐式等待移除的可能影响
【发布时间】:2020-04-19 17:26:24
【问题描述】:

在我们的 Selenium 自动化测试中,我们有隐式和显式等待。根据 Jim Evan 的想法https://stackoverflow.com/a/15174978/1471417,它们不应该混在一起。因此计划移除隐式等待。

对于我们的测试,每当我们与元素交互时,我们都会使用显式等待它可见、可点击等,而忽略 NoSuchElementException。这就是为什么我不认为,它会立即抛出NoSuchElementException

这确保删除隐式等待不会影响我的测试。除此之外,我想知道它是否有可能破坏测试。根据您的经验,我想了解它的影响,因此请求在这里分享您的观点。

【问题讨论】:

  • 根据我的经验,您的测试可能会变得不稳定。与其删除隐式等待,不如在显式等待条件周围创建包装器,并在您想使用显式等待条件时将隐式等待设置为零,然后再次将其设置为原始值(隐式等待)。你可以参考这里的例子github.com/springernature/omelet/blob/developer/omelet-core/src/…

标签: selenium selenium-webdriver webdriver webdriverwait nosuchelementexception


【解决方案1】:

你没看错。 @JimEvans 在discussion 中明确指出:

部分问题在于隐式等待通常(但可能并不总是!)在 WebDriver 系统的“远程”端实现。这意味着它们被“嵌入”到 IEDriverServer.exe、chromedriver.exe、安装到匿名 Firefox 配置文件中的 WebDriver Firefox 扩展以及 Java 远程 WebDriver 服务器 (selenium-server-standalone.jar)。显式等待仅在“本地”语言绑定中实现。使用 RemoteWebDriver 时事情会变得更加复杂,因为您可能会多次使用系统的本地端和远程端。

因此,在与元素交互时 Explicit Wait 是任务。


现在,根据WebDriverWait 的构造函数:

  • public WebDriverWait(WebDriver driver, long timeOutInSeconds):Wait 将忽略在“直到”条件下默认遇到(抛出)的 NotFoundException 实例,并立即传播所有其他实例。您可以通过调用 ignoring(添加例外)向忽略列表添加更多内容。
  • WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis):Wait 将忽略在“直到”条件下默认遇到(抛出)的 NotFoundException 实例,并立即传播所有其他实例。您可以通过调用 ignoring(添加例外)向忽略列表添加更多内容。

所以,WebDriverWait() 默认忽略 NotFoundException 并且直接已知的子类是:

  • NoAlertPresentException
  • NoSuchContextException
  • NoSuchCookieException
  • NoSuchElementException
  • NoSuchFrameException
  • NoSuchWindowException

来自WebDriverWait.java的源码:

/**
 * Wait will ignore instances of NotFoundException that are encountered (thrown) by default in
 * the 'until' condition, and immediately propagate all others.  You can add more to the ignore
 * list by calling ignoring(exceptions to add).
 *
 * @param driver The WebDriver instance to pass to the expected conditions
 * @param timeOutInSeconds The timeout in seconds when an expectation is called
 * @param sleepInMillis The duration in milliseconds to sleep between polls.
 * @see WebDriverWait#ignoring(java.lang.Class)
 */

因此,在使用 WebDriverWait 时,您不会遇到 NoSuchElementException。如果在 WebDriverWait 过期之前没有返回所需的元素,您将面临timeoutException

【讨论】:

  • 感谢所有这些细节。您的答案是更详细的解释,最重要的是您指向相应的文档链接,这有助于更清楚地理解事物。我非常感谢您为给出这样的答案所付出的时间和精力!
  • @TDHM 很高兴能为您提供帮助。 Upvote 答案如果这个/任何答案对您/对您有帮助,以造福未来的读者。
猜你喜欢
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-11
  • 2017-01-25
  • 2020-06-30
  • 2021-03-25
相关资源
最近更新 更多