Use of Thread.
sleep()
in Selenium Java
By,
Saajan Varghese
What is Thread.sleep() ?
In Java, Thread.sleep() is a method that belongs to the Thread class.
• It is used to pause the execution of the current thread for a
specified amount of time.
• The Thread.sleep() method takes a single argument, which is the
number of milliseconds that the thread should sleep. It can be
useful for introducing delays in a program.
• Syntax:
prepared by Saajan Varghese
Understanding of Thread.sleep() in Selenium
• Usually Testers will not use Thread.sleep() while
automating or testing as it simply waits till the mentioned
milliseconds.
• It’s a Java command, not a Selenium command.
• Time consuming.
• Instead, they use Selenium Waits.
prepared by Saajan Varghese
we can use
In
prepared by Saajan Varghese
For Example, In this Web Application we want to test
the value attribute
Click on to view the image dynamically
prepared by Saajan Varghese
Understanding of Thread.sleep() in Selenium Java
• We can use Explicit Wait to Test the attribute value.
• But we can also use Thread.sleep() to test the attribute value.
• To my understanding, after sending the keys, Explicit wait takes
one second to validate the value. Mentioning Thread.sleep(1000);
makes no difference.
• Not Recommended to Use Thread.sleep() in Code. I am saying by
Thread.sleep() also we can validate.
prepared by Saajan Varghese
Use of Thread.sleep() in Selenium Java
• Here’s the Test Code to Validate attribute value
prepared by Saajan Varghese
Test Result of the Code:
prepared by Saajan Varghese
Thanks for Reading
By,
Saajan Varghese