Selenium Pause in Java (WebDriver)

Home/Selenium/Selenium Pause in Java (WebDriver)

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 {
} catch (InterruptedException e) {

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.

By |September 14th, 2015|Selenium|0 Comments

Leave A Comment