
In this post I will describe what the get, wait for page load and wait for jquery to load methods from the 'waiter2' library do, and how to use them in your tests. For setup details, please read the corresponding post.
Waiting for a page to load: waitForPageLoadComplete
There are 2 methods with the name 'waitForPageLoadComplete'. The first one does not take any parameters. The second one takes an int as parameter, corresponding to how many seconds to wait for the page to finish loading successfully. I will refer to the no parameter variant in the following paragraph.
This method simply waits for the page document and corresponding static resources to finish loading for up to 30 seconds (the default timeout set in the method). The method uses the 'document.readyState' property to check for this. When the value of this property is 'complete', the wait method exits successfully. If the condition is not met within the specified timeout, the method throws a RunTimeException, with the following message: "waiter2.FAILURE: Page did not finish loading within 30 seconds.". This message should make it easy to identify, in the test output, why the test failed (in case the page did not load successfully during the specified timeout period).
Since version: 1.0
The code
The code of this method is:
public void waitForPageLoadComplete() {
waitForPageLoadComplete(TIMEOUT);
}
This method actually calls the method variant that takes a specified timeout as parameter. It sets that timeout to the default value of 30 seconds, defined in the Waiter class through the TIMEOUT constant. This latter method is:
public void waitForPageLoadComplete(int specificTimeout) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(specificTimeout));
try {
wait.until(driver -> String
.valueOf(((JavascriptExecutor)
driver).executeScript("return document.readyState")).equals("complete"));
} catch (TimeoutException e) {
throw new RuntimeException("waiter2.FAILURE: Page did not finish loading within " + specificTimeout + " seconds.");
}
}
Note: ideally you wouldn't use this method directly. It is called from a method described below for opening a page (called 'get'). Normally you would wait for a page to load after opening it.
Usage examples
Inside a test, once the waiter class was initialized and a driver instance was created, calling this method is simply done as:
waiter.waitForPageLoadComplete();
Waiting for jQuery to load: waitForJQuery
In order to wait for jQuery to finish loading, one of the 2 variants of the waitForJQuery method can be used: one without passing the timeout value as parameter (whose default timeout is 30 seconds) and another one that takes an int timeout value. If there is no jQuery on the page, the method exits immediately, successfully. Otherwise, in case it is present, the jQuery.active property is waited for, up to the defined timeout, to equal 0.
Since version: 1.0
The code
The method without parameters is as follows (and calls the other variant of the method, passing to it the TIMEOUT constant to specify 30 seconds as timeout):
public void waitForJQuery() {
waitForJQuery(TIMEOUT);
}
The variant which takes an int parameter for the timeout is as follows:
public void waitForJQuery(int specificTimeout) {
WebDriverWait wait = new WebDriverWait(driver,
Duration.ofSeconds(specificTimeout));
try {
ExpectedCondition<Boolean> condition = arg -> {
try {
return (Boolean) ((JavascriptExecutor)
driver).executeScript("return jQuery.active == 0");
} catch (Exception e) {
return true;
}
};
wait.until(condition);
} catch (TimeoutException e) {
throw new RuntimeException("waiter2.FAILURE: JQuery did not finish loading within "
+ specificTimeout + " seconds.");
}
}
Note: like with waitForPageLoadComplete ideally you wouldn't use this method directly. It is also called from a method described below for opening a page (called 'get').
Opening a page and waiting for it to load, together with jQuery: get
The 'get' method will be used for: opening a URL in the browser, followed by waiting for the page to load (by calling the waitForPageLoadComplete method), followed by waiting for jQuery to finish loading if present (by calling the waitForJQuery method).
For 'get' there are also 2 variants: one without a timeout parameter, and one to which you can pass an int for the timeout. For the version without the timeout parameter, the method will wait for up to 30 seconds for the page to load after opening the URL (which is done by calling Selenium's 'get' method), then for up to 30 seconds for jQuery. So in total there will be a wait of up to 60 seconds. The other parameter taken by the method is a String that represents the URL you want to open.
Since version: 1.0
The code
The variant of the 'get' method with default timeout is (this method calls the one described right below it):
public void get(String url) {
get(url, TIMEOUT);
}
The variant of the 'get' method to which you need to pass an int timeout value is:
public void get(String url, int specificTimeout) {
driver.get(url);
waitForPageLoadComplete(specificTimeout);
waitForJQuery(specificTimeout);
}
Usage examples
Due to the methods called inside the 'get' method, in your tests you never have to call those methods yourself. Here of course I am talking about the wait for page and jQuery to load ones. This helps reduce the code you write in each of your tests, since otherwise you would have to write 3 lines of code every time you opened a page. Now you just need to write 1. To open a valid URL, just pass in the valid URL, and either no timeout value or a desired int value, representing seconds. For the first case, you would proceed as follows:
waiter.get("https://www.selenium.dev/documentation/");
Or, to wait for 60 seconds instead of the default 30:
waiter.get("https://www.selenium.dev/documentation/", waiter.MEDIUM_TIMEOUT);
So that's it regarding the methods for opening and waiting for the page to load. The test code can be found here. Next up I will discuss some further useful methods, so stay tuned!