6

I am using python+selenium webdriver for my automation. I have made use of ImplicitlyWait along with WebDriverWait.

Questions:

  1. Is it a good practice to use both ImplicitlyWait and WebDriverWait in a single script?

  2. Suppose my ImplicitlyWait value is 20 and WebDriverWait value is 10 seconds. Will WebDriverWait override 20 when it waits for a specific element? What happens when ImplicitlyWait value is less than WebDriverWait?

Please suggest. I tried finding this answer on the internet but did not get any full proof or convincing answer.

Praveen Pandey
  • 658
  • 3
  • 16
  • 32
  • 4
    You might want to have a look at JimEvans' thoughts [here](http://stackoverflow.com/a/15174978/1177636). – Yi Zeng Jul 24 '13 at 09:44
  • I had a look of that answer but it just answers the 1st part of my question above. What about the 2nd part? – Praveen Pandey Jul 24 '13 at 09:50
  • That answer also answers the second part of your question as well. The answer (as stated in that other post) is "it's undefined behavior". – JimEvans Jul 24 '13 at 12:06

1 Answers1

6

For the first point, it's probably a personal choice. I don't use implicit waiting at all simply because I like to have the control of where Selenium is waiting and where it is not. Setting the implicit wait is blindly telling Selenium, if you don't find my element, then wait for a certain time until you can. No matter what it is, no matter the consequence, no matter what page, you wait until you either find it or 20 seconds has passed.

That's fine if that's the way you want to go, but for me, the problem comes from if my element is taking 30 seconds to appear on the page then that's a problem in itself anyway. Selenium just hides it, it can cover over the problem.

However, there are some times the element does take a while to appear and to be 'ready' and that's when explicit waiting comes in. When you do expect it.

As for the waiting, what will happen is initially when your WebDriverWait is hit, it will run and try to find the element. Selenium will see you have your implicit wait set so it will constantly try to find that element for up to 20 seconds.

It will do this 'for free', behind the scenes.

Once that's expired, your WebDriverWait will get a response and since your implicit wait timeout is larger than your WebDriverWait timeout, it will fall over with an exception (Timeout exception).

In terms of if the WebDriverWait value is higher than the implicit wait value (your example in the comments), then the process would be:

  1. Run the WebDriverWait initially
  2. It hits the .FindElement call
  3. Selenium runs this, internally, for 10 seconds
  4. After the initial 10 seconds, your WebDriverWait would get a response.
  5. If the element is not found, it is run from step 1 again.
  6. Once it gets to step 4, if the element is not found still, it will now throw a timeout exception.

To further explain (pseudo C# code):

driver.Manage().Timeouts().SetImplicitWait(TimeSpan.FromSeconds(10));
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
wait.Until(w => 
{
    return w.FindElement(By.Id("something")).Displayed;
}

We have a WebDriverWait of 20 seconds, and an implicit wait of 10 seconds.

It will hit:

return w.FindElement(By.Id("something")).Displayed;

for the first time. Selenium, internally, will run the .FindElement for 10 seconds. After this 10 seconds has passed, my WebDriverWait will then check it's condition (.Displayed). If this is not met, then it the WebDriverWait will run the same line of code again. Again, Selenium will do as it did before, and run for 10 seconds. Once it comes back from that, if the condition is not met this then means, the time the WebDriverWait has been waiting is a total of 20 seconds and will then throw an error.

Arran
  • 24,648
  • 6
  • 68
  • 78
  • So you mean, if we have both waits present and when WebDriverWait is encountered, ImplicitlyWait is called first, waits for 20 secs and if element is not found, there is no further wait of 10 secs again from WebDriverWait. Right? – Praveen Pandey Jul 24 '13 at 09:34
  • On that note, just one last question. What if WebDriverWait value is more than ImplicitlyWait value say WebDriverWait = 20 and ImplicitlyWait = 10. Can you please tell me how this will be executed? – Praveen Pandey Jul 25 '13 at 06:52
  • It would be similar to before. In that the WebDriverWait would be called, on each `find element` call, it would wait for 10 seconds, then go round again. Since the WebDriverWait is larger here, it would keep going round checking for the condition you give it, waiting up to 10 seconds each and every time it is checked. Does that make sense? – Arran Jul 25 '13 at 18:55
  • Sorry Arran but I am not able to understand. You are saying WebDriverWait would wait for 10 seconds? but why. I mean WebDriverWait has 20 as the value and ImplicitlyWait has 10 as the value. Can you be a little more clear... Thanks! – Praveen Pandey Jul 26 '13 at 05:10
  • Hopefully my edit has made things clearer. Worth mentioning JimEvan's response - as a core Selenium developer he suggests this is undefined behaviour - my answer is purely based on what I see when I sit down with a debugger and try this out. – Arran Jul 26 '13 at 08:33
  • @Arran - I have a query. Lets say Implicit wait of 10 seconds. And Explicit wait of 15 seconds. So initially, Web driver wait will be called, it checks the condition. And it waits for 10 seconds (as defined in implicit wait). Now web driver gets a response. And if the condition is failed, Now will it wait for another 10 seconds or 5 seconds? Because the web driver wait contains a maximum of 15 seconds – Aishu Feb 25 '19 at 05:27
  • @Aishu the condition will be checked once more because there's still time left, and this will wait for 10 seconds, once the condition has finished & if it failed the time elapsed will be checked, which would be 20ish seconds, which is more than 15 seconds and as such it will timeout. Which means in essence, you are checking the condition only twice within that 15 seconds period. – Arran Feb 25 '19 at 11:20
  • @Arran, you mention that you don't like to use the `implicitlyWait` timeout, preferring `WebDriverWait` instead. We are currently trying to figure which way to go with our test code and a reasonable suggestion seems to be to avoid `WebDriverWait` (or just set its timeout to zero) and set the `implicitlyWait` timeout on each occasion just before any sort of wait is required, and setting _it_ to zero when no wait is wanted. I'm wondering what we would lose if we went down _that_ path. – Glenn Lawrence May 26 '20 at 12:58