隐式等待删除的可能影响
Probable impact of implicit wait removal
在我们的 Selenium 自动化测试中,我们有隐式和显式等待。根据 Jim Evan 的想法 ,它们不应该混在一起。因此计划删除隐式等待。
对于我们的测试,每当我们与一个元素交互时,我们都使用显式等待它可见、可点击等而忽略 NoSuchElementException
。这就是为什么我不认为,它会立即抛出NoSuchElementException
。
这确保删除隐式等待不会影响我的测试。除此之外,我想知道它是否有可能破坏测试。根据您的经验,我想了解它的影响,因此在此请求分享您的观点。
你没看错。 @JimEvans 在这个 discussion 中明确指出:
Part of the problem is that implicit waits are often (but may not always be!) implemented on the "remote" side of the WebDriver system. That means they're "baked in" to IEDriverServer.exe, chromedriver.exe, the WebDriver Firefox extension that gets installed into the anonymous Firefox profile, and the Java remote WebDriver server (selenium-server-standalone.jar). Explicit waits are implemented exclusively in the "local" language bindings. Things get much more complicated when using RemoteWebDriver, because you could be using both the local and remote sides of the system multiple times.
因此,在与元素交互时 Explicit Wait 是必须的。
现在,根据 WebDriverWait 的构造函数:
public WebDriverWait(WebDriver driver, long timeOutInSeconds)
:Wait 将忽略在 'until' 条件下默认遇到(抛出)的 NotFoundException
实例,并立即传播所有其他实例。您可以通过调用 ignoring(exceptions to add) 向忽略列表中添加更多内容。
WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)
:Wait 将忽略在 'until' 条件下默认遇到(抛出)的 NotFoundException
实例,并立即传播所有其他实例。您可以通过调用 ignoring(exceptions to add) 向忽略列表中添加更多内容。
所以,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.
在我们的 Selenium 自动化测试中,我们有隐式和显式等待。根据 Jim Evan 的想法 ,它们不应该混在一起。因此计划删除隐式等待。
对于我们的测试,每当我们与一个元素交互时,我们都使用显式等待它可见、可点击等而忽略 NoSuchElementException
。这就是为什么我不认为,它会立即抛出NoSuchElementException
。
这确保删除隐式等待不会影响我的测试。除此之外,我想知道它是否有可能破坏测试。根据您的经验,我想了解它的影响,因此在此请求分享您的观点。
你没看错。 @JimEvans 在这个 discussion 中明确指出:
Part of the problem is that implicit waits are often (but may not always be!) implemented on the "remote" side of the WebDriver system. That means they're "baked in" to IEDriverServer.exe, chromedriver.exe, the WebDriver Firefox extension that gets installed into the anonymous Firefox profile, and the Java remote WebDriver server (selenium-server-standalone.jar). Explicit waits are implemented exclusively in the "local" language bindings. Things get much more complicated when using RemoteWebDriver, because you could be using both the local and remote sides of the system multiple times.
因此,在与元素交互时 Explicit Wait 是必须的。
现在,根据 WebDriverWait 的构造函数:
public WebDriverWait(WebDriver driver, long timeOutInSeconds)
:Wait 将忽略在 'until' 条件下默认遇到(抛出)的NotFoundException
实例,并立即传播所有其他实例。您可以通过调用 ignoring(exceptions to add) 向忽略列表中添加更多内容。WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)
:Wait 将忽略在 'until' 条件下默认遇到(抛出)的NotFoundException
实例,并立即传播所有其他实例。您可以通过调用 ignoring(exceptions to add) 向忽略列表中添加更多内容。
所以,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.