• Home
  • Courses
  • Enroll For Training
  • Blog
  • Become a Tutor
  • About Us
  • Contact Us
  • Register
Have any question?

(+1) 408-901-9645
contact@ministryofautomation.com
RegisterLogin
Ministry of Automation
  • Home
  • Courses
  • Enroll For Training
  • Blog
  • Become a Tutor
  • About Us
  • Contact Us
  • Register

Selenium

  • Home
  • Blog
  • Selenium
  • Is it good practice to use Thread.sleep while doing selenium Test ?

Is it good practice to use Thread.sleep while doing selenium Test ?

  • Posted by Saurabh Thawali
  • Date December 30, 2020
  • Comments 1 comment

I would like to write this in detailed way + real time example (read till end)

Different ways of wait used in selenium web driver

  1. Thread.sleep() method.
  2. Implicit wait.
  3. Explicit wait.
  4. 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

 

  • Share:
author avatar
Saurabh Thawali

I am Senior Automation Engineer working in an IT industry (Software Testing) for 11+years and have worked with different types of companies (product as well as services) ranging from small to large scale companies.
My Working Experience includes,
Product Companies: Oracle, Cisco & Workday
Large Service Companies: Cognizant
Medium and Small Consulting Companies: Zensar Technologies and Nihilent Technologies

I have started my career with Manual Testing and learned Automation along the journey working on various automation projects offered to me. I was fortunate to work on different type of automation frameworks including wide range of tools and technologies.

I have enjoyed this organic growth in Automation which enabled me to share my experience and knowledge with others in simple words and help them understand how automation testing concepts practically fit in an industry.

Training is my passion and i really enjoy sharing knowledge with others, I try to explain complex topics and automation concepts in much simpler way. I believe everyone need someone to hold their hands initially when you are learning to walk, after that you can RUN on your own.

    1 Comment

  1. Ankit Prajapati
    December 30, 2020

    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.

Recent Posts

  • Is it good practice to use Thread.sleep while doing selenium Test ?

Recent Comments

  • Amit on Java For Testers – From Scratch
  • dbharshavardhan on Demo Session with Course Contain Walkthrough
  • Ministry of Automation on Oracle Openscript ,OATS Testing for beginners, Oracle Apps Automation !!
  • giulianam on Oracle Openscript ,OATS Testing for beginners, Oracle Apps Automation !!
  • Supriya on Exception Handling in Software Automation using JAVA

(+1) 408-901-9645

contact@ministryofautomation.com

  • Home
  • Blog
  • About Us
  • Become a Tutor
  • Contact Us

Made by Passionate Automation Engineers for Ministry of Automation. All rights reserved.

Become an instructor?

Join the group of passionate instructors and earn recognition around globe.

Get started now

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now