Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Selenium Pause in Java (WebDriver)

Probably you know from the Selenium IDE the command pause. If you have been looking for the exact same command for the WebDriver in Java you’ve found probably nothing but it is pretty easy to imitate the command in Java.

What exactly does the Pause-command in the Selenium IDE?

pause(waitTime): Wait for the specified Amount of time in milliseconds.

Selenium Pause Option 1:

To imitate exactly this behavior in Java with the Selenium WebDriver just use these code lines:

try {
   Thread.sleep(1000);
} catch (InterruptedException e) {
   e.printStackTrace();
}

In this example we used 1000 milliseconds as waitTime which equals one second. You can substitute this number with any number you like.

Selenium Pause Option 2:

There is also a built in function that lets the WebDriver pause for the given amount of time:

webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

Notice that this second example should wait the same amount of time as the first example since the waitTime is given in seconds here instead of milliseconds.

Which option is the better one?

There is not one true answer to this question. Usually it is better to use the second option instead of calling a thread directly to wait as it’s the case in the first option. Unfortunately sometimes the implicit wait seems to be overwritten so be on the safe side that your Java program will really wait for the given amount of time you should use the first option.

Also consider using alternatives like the explicit wait which waits for a specific element to be loaded. Using explicit waits you make sure not to waste time in your automated test execution.



This post first appeared on FRET, please read the originial post: here

Share the post

Selenium Pause in Java (WebDriver)

×

Subscribe to Fret

Get updates delivered right to your inbox!

Thank you for your subscription

×