Google+

292. 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. Open the specified URL using the Selenium WebDriver command get( ) as explained in our previous Post#14
3. Maximize the browser window having the opened URL using the Selenium WebDriver command as explained in our previous Post#15
4. Resize the browser window having the opened URL using the Selenium WecbDriver command as explained in our previous Post#16
5. Insert the Thread.sleep(20000); statement between the Open the URLMaximize the window  and Re-size the window selenium WebDriver commands as shown below:
6. Before adding the Thead.sleep(20000); statements our Automation Code will look like this:


7. Add Thread.sleep( ) statements to the Automation code as shown below:


8. Observe that the above Thread.sleep( ) java statements are giving errors as shown below:


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





8. Save and Run the code using JUnit Test and observe that the Test Browser opens the specified URL, gets paused for 20 seconds before maximizing the browser window and also gets paused for 20 seconds before re-sizing 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.

And also observe that 'WebDriver' text on the Bottom Right of the Test Browser changes from Red to Blue when our Automation Test executes Thread.sleep(20000) statements (i.e. while sleeping for 20 seconds) as shown below:



Download This Project:

Click Here to download this project and import into Eclipse IDE on your machine.



Please comment below to feedback or ask questions.

Using quit( ) to close the test browser window will be explained in the next post.






No comments:

Blog Index