Combining implicit wait and explicit wait together leads to unexpected wait times

Two of my scripts -

1) First

@driver.manage.timeouts.implicit_wait = 30 @wait = Selenium::WebDriver::Wait.new(:timeout => 45) # Time greater than implicit @wait.until {@driver.find_element(:tag_name => "body").text.include?("hey")} 

Which gives the driver 45 seconds to search for text (what is expected)

2) Second

 @driver.manage.timeouts.implicit_wait = 30 @wait = Selenium::WebDriver::Wait.new(:timeout => 5) # Time less than implicit @wait.until {@driver.find_element(:tag_name => "body").text.include?("hey")} 

Now it gives the driver 30 seconds to search for text (not expected)

Is there a way to make selenium wait only for explicit , and not for the larger of the two?

Note. Not declaring an implicit wait time is not an option, because I cannot let selenium hang up every time the driver cannot find something.

Using Selenium version 30, windows, ff

+10
ruby selenium selenium-webdriver webdriver
Mar 01 '13 at 18:47
source share
2 answers

Do not mix implicit and explicit expectations. Part of the problem is that implicit expectations are often (but not always!) Implemented on the "remote" side of the WebDriver system. This means that they are β€œbaked” in IEDriverServer.exe, chromedriver.exe, the Firefox WebDriver extension, which is installed in the anonymous Firefox profile and on the remote Java WebDriver server (selenium-server-standalone.jar). Explicit expectations are met exclusively in "local" language bindings. When using RemoteWebDriver, everything is more complicated, since you can use both the local and remote sides of the system several times.

Here's how it works: local code -> remote Java server -> local Java language bindings on the remote server -> "remote", such as the Firefox extension, chromedriver.exe, or IEDriverServer.exe. This is even more complicated with the grid, as there may be other transitions between them.

Thus, when you try to mix implicit and explicit expectations, you deviate into "undefined behavior". You may be able to figure out what the rules for this behavior are, but they can be changed as the details of the driver implementation change. So do not do this.

You should not experience β€œhangs” when an item cannot be found unless you use implicit expectations. The driver should immediately throw a NoSuchElement exception.

+39
Mar 02 '13 at 13:24
source share

Best practice is to set implicitlyWait () at the beginning of each test and use WebDriverWait () to wait for an AJAX element or element to load.

However, implicitlyWait () and WebDriverWait () do not work well together in the same test. You will need to invalidate implicitlyWait () before calling WebDriverWait, because implicitlyWait () also sets the timeout to "driver.findElement ()".

Whenever you use WebDriverWait (), with implicitlyWait () already set the initial value, follow these steps:

  • revocation implicitly Wait ()
  • executing WebDriverWait () and the returned element
  • reset implicitlyWait () again

Java Code Example -

 driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds); element = wait.until(ExpectedConditions.visibilityOfElementLocated(by)); driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); 
-3
Sep 04 '13 at 9:48 on
source share



All Articles