Replace Implicit Wait with Explicit Wait (Selenium Webdriver & Java)

Changing Selenium implicit wait inside test process

Perfectly valid to do this. More importantly correct way to avoid mixing up implicit and explicit waits. That mixture of waits leads to some hard to debug issues with long wait times. Problem is you could forget to turn off and on the implicit wait. Better to write a utility function to wrap this logic by passing the explicit wait criterion and use it across your code base.

I took a stab at creating a generic method to use. Though generic ain't my strong point.

protected static <T> T waitECTimeoutWrapped(WebDriver driver, ExpectedCondition<T> ec, int timeout) {
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, timeout);
T data = waitDriver.until(ec);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
return data;
}

You can ignore the return data for many cases but this way you can retrieve elements that you may be looking for in one call.

Probable impact of implicit wait removal

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

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.

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


Now, as per the constructors of WebDriverWait:

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

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

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

From the source code of 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)
*/

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

Jmeter webdriver implicit wait implementation

There is no JAVA in JMeter's WebDriver Sampler, it's Beanshell interpreter and in order to be able to use Explicit Wait with the above code snippet you will need to switch the language to groovy (moreover it's recommended scripting option since JMeter 3.1)

You won't have to change a single line or even character, valid Java code in majority of cases is valid Groovy code.

Given you switch to Groovy the piece of code implementing the explicit wait which you copied and pasted from somewhere will start working.

Also don't mix implicit and explicit waits:

Warning: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.



Related Topics



Leave a reply



Submit