17. Using Thread.sleep(Millie Seconds) to pause the execution






Thread.sleep( ) statement belongs to Java's Thread concept.

This statement is used in Selenium Automation to pause the execution for the specified time.

Before understanding the below example you have to understand that 1 second = 1000 Millie seconds.

Example:

Thread.sleep(1000) -> This statement will pause the execution for 1000 Millie seconds (i.e. 1 second).

But if you want the execution to stop for 15 seconds then you have to write down the above statement as shown below:

Thread.sleep(15000) -> This statement will pause the execution for 15000 Millie seconds (i.e. 15 seconds).

Lets Implement This: 


1. Start the Selenium RC Driver as explained in our previous Post#14
2. Open the URL using the Selenium RC command as explained in our previous Post#15
3. Maximize the browser window having the opened URL using the Selenium RC command as explained in our previous Post#16
4. Click here to watch the video before inserting the Thread.sleep(20000); statement in between the Open the URL and Maximize the window selenium commands
5. Insert the Thread.sleep(20000); statement between the Open the URL and Maximize the window selenium commands as shown below:


6. Observe that the above Thread.sleep( ) java statement is giving an error as shown below:


7. In order to resolve the error write throws Exception  beside the method name which is holding the Thread.sleep( ) statement as shown below and ensure that the error got resolved:



8. Save and Run the code using JUnit and observe that the Test Browser opens the specified URL, gets paused for 20 seconds before maximizing the browser window as shown in the below video:

Click here to watch the video.

This is how the Thread.sleep( ) statement works. But do you really think the Thread.sleep( ) is required to pause for 20 seconds before maximizing the browser window. The Answer is No. We've to use Thread.sleep( ) where ever it is necessary. When to use the Thread.sleep( ) method will be explained in future posts but for now just understand how 'Thread.sleep( )' statement works.


Please comment below to feedback or ask questions.

Using getEval( ) to re-size the test browser window will be explained in the next post.





No comments:

Post a Comment

Followers

Labels