Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium implicit wait vs Thread.sleep()

According to selenium, an implicit wait polls the DOM for a certain amount of time to see if an element shows up. My understanding is that it will poll up to a specified amount of time, but if an element shows up before, then it will continue without waiting further.

http://seleniumhq.org/docs/04_webdriver_advanced.html

I have a method that runs in about 13 seconds. When I set the implicit wait to 100 seconds, it takes 213 seconds.

driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);

It appears that during this method, I'm waiting 2 times (100 seconds each). Setting the implicit wait to 0 or 100 doesn't affect my method. In both cases, they finish correctly.

My question is this. I thought the implicit wait waits for the shortest amount of time for an element to show up. Is this right? Or am I doing something wrong?

Furthermore, why is it waiting 2 times, when it apparently does not need to wait? (My method finishes correctly even if I set the wait to 0)

like image 712
Steven Avatar asked Dec 20 '12 15:12

Steven


1 Answers

Short answer:

implicit wait - It's global setting applicable for all elements and if element appear before specified time than script will start executing otherwise script will throw NoSuchElementException. Best way to use in setup method. Only affect By.findelement().

Thread.sleep() - It will sleep time for script, not good way to use in script as it's sleep without condition.

like image 87
Anand Somani Avatar answered Oct 11 '22 19:10

Anand Somani