In this post i will discuss the methods you can find in thewaiter library, for waiting for a page to load completely. I will show the methods you can use from thewaiter that can help you with these waits, and some test examples for each, that you can also find in GitHub.  

Each of the methods implemented in the library for waiting for an event, have two versions: one does not need to receive a timeout parameter, and one does. The methods without the timeout parameter in their signature will use the default timeout set in thewaiter library, which is currently set to 30 seconds. That means they will wait for an event for up to 30 seconds to occur. If in your tests you feel that 30 seconds is too much, you can call the version of the wait method by specifying an int value representing the number of seconds up to which you want the method to wait.

All the wait methods can be found in the Waiter class of thewaiter library.

Waiting for a page to load

This wait is useful when you need to interact with a page, but before doing that, you need to make sure the page loaded fully. Otherwise, the interactions you want to make might fail.

Normally you might think you should open a page (by using Selenium’s get() method, to which you provide a URL to open), and then call the wait for page load method. This means however that in each test where you want to do this, you need to write 2 lines of code: the line for calling Selenium’s get method, and the line for calling the wait method. If you write, let’s say, 1000 tests, that would mean 2000 lines of code for waiting for the page to open and load completely. Therefore, for this scenario, the get() method from thewaiter library is better to use instead. It is described a bit lower in the post.

Having said that, using the waitForPageLoadComplete is more suited for scenarios like: refreshing a page (when you don’t need to use Selenium’s get method), navigating back and forth on pages (by using the navigate() options from Selenium).

The wait methods: waitForPageLoadComplete

The methods that wait for the page to load will execute a Javascript code that checks that the “readyState” parameter, that gives the status of the document loading, returns “complete”. More details on the “readyState” can be found here: .

Here is the wait method which needs to receive the WebDriver instance you have started in your test, and the timeout amount as an int.

public void waitForPageLoadComplete(WebDriver driver, int specifiedTimeout) {
    Wait<WebDriver> wait = new WebDriverWait(driver, specifiedTimeout);
    wait.until(driver1 -> String
            .valueOf(((JavascriptExecutor) driver1).executeScript("return document.readyState"))

If you don’t want to send the timeout value to the method call, but instead use the default timeout of 30 seconds defined in the Waiter class, you can use this version:

public void waitForPageLoadComplete(WebDriver driver) {
    waitForPageLoadComplete(driver, TIMEOUT);

This last method just calls the first one, by sending the constant defined in the Waiter class, TIMEOUT.


Here is an example of opening a page, refreshing it, and then using the waitForPageLoadComplete method with default timeout. By the time this test finishes, the page refresh is done and the page is completely loaded.

public void waitForPageLoadComplete() {
    waiter.get("", driver);

Here is the same example, but with another timeout defined for the wait, worth 10 seconds.

public void waitForPageLoadCompleteTimeout10Seconds() {
    waiter.get("", driver);
    waiter.waitForPageLoadComplete(driver, 10);

Opening a page and waiting for it to load

In the examples above you saw how to wait for a page to load, but as i said, in tests you usually open a page first, and only after that you wait for it to load. Whenever you want to open the page, you need to remember to also call the wait for page load method.

Well, from now on you can use the “get” methods from thewaiter library which take care of both steps: first they will open a URL that you pass to them as a String parameter, and then they wait for the page to load completely.

The wait methods: get

The get methods are quite straightforward. They call Selenium’s get() method to open the page, then they call the waitForPageLoadComplete method described above.

The variant to which you pass the timeout value as int is:

public void get(String url, WebDriver driver, int specifiedTimeout) {
    waitForPageLoadComplete(driver, specifiedTimeout);

The variant which uses the default timeout value as set in the Waiter class is:

public void get(String url, WebDriver driver) {
    get(url, driver, TIMEOUT);

Here is a simple example that opens a URL and waits for the page to load, all in one line, by calling the get() method from thewaiter library:

public void getTest() {
    waiter.get("", driver);

The same example but with a specified timeout of 10 seconds:

public void getTestTimeout10Seconds() {
    waiter.get("", driver, 10);

Where to find the code and examples

The GitHub repo of thewaiter library, with a “how to use” documentation:

The downloadable release version of thewaiter:

The library’s javadoc:

The tests that use the methods from this post:

5 thoughts on “thewaiter: opening a page and waiting for it to load with Selenium

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.