What is the difference between Thread.sleep(6000); and driver.manage().timeouts().implicitlyWait(6,TimeUnit.SECONDS);?
This is a very nice and important question. WebDriver waits are smart in nature as compared to Thread.Sleep.
Let me point out the differences in bullets
– Thread.Sleep waits stops the thread execution for the specified amount of seconds, 6 seconds in your case. During this time there is no other execution that happens in that thread.
– If your element is visible with in the the time limit specified in Thread.Sleep you can do anything, your thread will wait for the specified time before moving to the next statement in the code.
– However, in case of your implicitWait your code will not always wait for the time limit specified. It will wait till the time your element is visible or till the time you have specified. Basically till either of the two condition occurs first.
Next quest is how does implicit wait achieves that?
Implicit wait consists of following steps
1. Look in the DOM for a particular element. If found move out of the wait.
2. If not found wait for 250 milli seconds.
3. After 250 milli seconds poll the DOM again and check for the element. If element found move out of the wait.
4. If element not found, go back to step number 2 and repeat the following steps.
Essentially in implict wait you end up saving time.
A diagram of the sequence will look something like this
I hope that clears your doubt.