本文介绍了隐式等待移除的可能影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In our Selenium automated tests, we've implicit and explicit waits. As per Jim Evan's thought https://stackoverflow.com/a/15174978/1471417, they should not be mixed. Hence planning to remove implicit wait.

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

For our tests, whenever we interact with an element, we've used explicit wait for it to be visible, clickable etc. with ignoring NoSuchElementException. That's why I don't think, it will throw NoSuchElementException immediately.

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

This makes sure removing implicit wait won't affect my tests. Apart from this, I want to know if there are any chances it can break tests. Based on your experiences, I want to understand its impact, hence requesting to share your views here.

推荐答案

你没看错.@JimEvans 在这个 讨论明确指出:

You saw it right. @JimEvans in this discussion clearly states that:

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

因此,在与元素交互时 显式等待 是任务.

So, while interacting with an element Explicit Wait is the mandate.

现在,根据 WebDriverWait:

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

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

So, WebDriverWait() by default ignores NotFoundException and the direct known subclasses are:

  • 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.

So, while using WebDriverWait you won't face NoSuchElementException. Incase the desired element is not returned till the WebDriverWait expires you will face timeoutException.

这篇关于隐式等待移除的可能影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 14:47