
Is it good practice to use Thread.sleep while doing selenium Test ?
I would like to write this in detailed way + real time example (read till end)
Different ways of wait used in selenium web driver
- Thread.sleep() method.
- Implicit wait.
- Explicit wait.
- Fluent wait.
Thread is a class in JAVA and sleep() is the static method inside Thread class. That’s why when we want to use sleep method() we need to use syntax as classname.methodname() so we use Thread.sleep() in order to add some wait time during executing selenium scripts.
What exactly happens when we use Thread.sleep() in the script ?
Thread.sleep causes the current thread to suspend execution for a specified period
sleep() methods accept duration in milliseconds. ( 1 s= 1000 ms) actual Syntax: Thread.sleep(3000);
So when we add above step in the script we are saying “Please wait for 3000ms before proceeding to the next step/line to execute”
Question is why we need sleep() ?
As you all know automation tool works so fast (click and submit happens faster than human actions/response) so web pages needs some time to load the page/elements on the page and in such scenarios when selenium script tries to click/locate some element on the page which is not yet loaded or available on the page then in this situation script fails with NoSuchElementException because that element is still loading and scripts wanted to locate it and perform an action on it and hence fails.
With the use of sleep() method we wait for sometime so that webpage can get some time to load new page or elements and then script can continue to locate them for performing any action like click/submit etc
In such case script avoids failure and hence we are able to overcome such failures.
But is Thread.sleep() good to use ?
No, In a simple example I will try to explain why – You went to a restaurant and at the reception you been asked to wait for 10min (sleep()) to get a table and you started waiting (like script starts to wait for defined time) and you observed that table is available before 10min wait completes and you are asked to come inside and take the table …. In this situation will you still want to wait for complete 10min or go straight to the table ? Answer is simple right 🙂 !!
Same situation happens with the web driver, when we add Thread.sleep(3000) in this situation even when elements/page loads completely web driver can’t proceed with the next steps to execute unless it completes the defined sleep time and this add’s more execution time to the script .
In shot it makes your script passive and slow.
That’s why adding Thread.sleep() is not a good idea always !!
Because of above disadvantages of sleep() method we go for other types of wait provided by selenium webdriver.
Hope my explanation is simple and clear to understand.
I try to teach “Software Automation Concepts” in a very simple way which makes it easy and exciting to learn. You can drop me an email/whatsapp at Contact@ministryofautomation.com
Best Luck !!
I can help anyone who is looking to learn Software Automation Tools like ‘Selenium, OATS- Oracle Application Testing Suite, Openscript etc’ you can learn form some of my FREE courses on Ministry of Automation and from YouTube
1 Comment
Hi Saurabh you are awesome, thanks for clarifying it in detail.
I also watched your Java for Tester course and learned JAVA for the first time….Big big thanks to you.