Java – Difference between Wait methods in Selenium and Thread.Sleep

javaselenium-webdriver

Can please help me to understand the difference between below three waiting methods and efficient use in the program,

  1. Thread.sleep(5000);

  2. driver.manage().timeouts().implicit Wait(5, TimeUnit.SECONDS);

  3. ExplicitWait();


Thanks

Best Answer

Blocking the thread is really a block java uses for example to control multithreaded application.

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

You should not use Java's thread.sleep as a replacement for the selenium once, as you could potentially create multithreading errors depending on what else you do within the application. (Deadlocks for example, or race conditions)