In this blog, we will study Playwright Java's ability to set a timeout explicitly for actions.
Topics that we will cover:
- Playwright Java’s explicit timeout for specific actions
- Source code
Playwright Java’s explicit timeout for specific actions
We have already seen setting up of global default timeout.
We will examine the procedure for establishing an explicit timeout for a specific element in playwright-java.
Let us again go to http://uitestingplayground.com/ajax
According to the scenario description, after pressing the button, the ajax data will become visible after a 15-second delay, implying a wait time of 15 seconds

In other words, by clicking the blue button, the ajax data will be loaded after a delay of 15 seconds

Next, the AJAX data output can be identified using class ‘bg-success’ or by text ‘Data loaded with AJAX get request’

Before we set the explicit wait timeout, let us first comment default timeout (line#27 in above snapshot).
In this use case, the idea of explicit wait timeout is that, we will wait explicitly for ajax data output element.

Once the element is visble, we will than fetch the innertext of ajax data output (line#32 in above snapshot).
You can utilize the waitForSelector() method by providing the selector and the timeout as its arguments

The locator of the ajax data output will be stored in the selector argument, as it represents the element we need to wait for

Below is the syntax for the second 'timeout' argument. We will specify a timeout duration of 16 seconds

Execute the code.
The test will be successful. The console prints the ajax output message

Execute the code.
The test fails this time, because the ajax data is loaded after 15 seconds, exceeding the timeout limit of 14 seconds that we have set up

Execute the code.
The test is expected to fail because the default timeout of 15 seconds matches the timeout required to display ajax data within 15 seconds

This is how the explicit timeout functions for a specific element.
Source code
package com.rsa.playwrightjava;
import org.junit.jupiter.api.Test;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Blog9_ExplicitTimeout {
@Test
public void PlaywrightJTest() {
Playwright pt = Playwright.create();
LaunchOptions lo = new LaunchOptions();
lo.setChannel("msedge");
lo.setHeadless(false);
BrowserType btype = pt.chromium();
Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));
Page pg = b.newPage();
//pg.setDefaultTimeout(14000);
//pg.setDefaultTimeout(16000);
//pg.setDefaultTimeout(15000);
pg.navigate("http://uitestingplayground.com/ajax");
pg.locator("text=Button Triggering AJAX Request").click();
//set explicit timeout
//pg.waitForSelector(".bg-success", new Page.WaitForSelectorOptions().setTimeout(16000));
//pg.waitForSelector(".bg-success", new Page.WaitForSelectorOptions().setTimeout(14000));
pg.waitForSelector(".bg-success", new Page.WaitForSelectorOptions().setTimeout(15000));
String ajmsg = pg.locator(".bg-success").innerText();
System.out.println(ajmsg);
pg.pause();
}
}
Thanks.