Alternative Approach to Solve Thread.Sleep in Java or Selenium or in OpenScript

December 22, 2013

Now a days if we want our execution to be halted for sometimes [mainly observed in Automation Testing when a page needs to be loaded first and then some line of code will be executed], We use Thread.sleep(int Sec) method. Well this is quite recognized/common method to solve if we want my program to wait for specific point of time.More details can be found here in java doc.
As per java doc..
Notice that main declares that it throws InterruptedException. This is an exception that sleep throws when another thread interrupts the current thread while sleep is active. Since this application has not defined another thread to cause the interrupt, it doesn't bother to catch InterruptedException.

But now a days I have seen that it is a requirement to do more customization regarding the same.
One approach can be --[taken from stackoverflow]

public static void pause(int seconds){
     Date start = new Date();
     Date end = new Date();
     while(end.getTime() - start.getTime() < seconds * 1000){
         end = new Date();

Another Approach can be to use.
Pause.pause(int sec)

 The third approach can be..
public static void wait_for_element(){
      pause(2);//use the first approach for implementing this pause method
//the element is displayed

Now the choice is yours ...depending on the scenario, you can take any one of the process. Happy testing.

You Might Also Like