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

7
252
Selenium
Selenium
Spread the love

Table of Contents

Thread.Sleep in Java

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(){
     do
            {
      pause(2);//use the first approach for implementing this pause method
            }
     until(driver.findElements(By.xpath("//*[starts-with(@id,'frm')]")).get(1).isDisplayed())
//the element is displayed

 

Thread.Sleep in Java

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


Spread the love
READ  How To Handle Keyboard and Mouse Operation In Selenium Via Action class?
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

7 COMMENTS

  1. Hello Abhranil,
    you can join via this link-https://feedburner.google.com/fb/a/mailverify?uri=TechTravelHub
    Regards,
    Animesh

  2. My brother recommended I may like this website. He was entirely right.

    This post actually made my day. You can not
    consider just how so much time I had spent for this information! Thank you!

  3. What’s up all, here every one is sharing such familiarity, thus
    it’s fastidious to read this webpage, and I used to go to
    see this weblog all the time.

  4. Wow, this piece of writing is fastidious, my younger sister is analyzing these kinds of things,
    therefore I am going to let know her.

  5. I don’t even know how I ended up here, but I thought this post was great.
    I do not know who you are but definitely you’re going to a famous blogger
    if you aren’t already 😉 Cheers!

LEAVE A REPLY

Please enter your comment!
Please enter your name here