In this blog, we will customize and set the default global timeouts in Playwright Java to suit our needs.
Topics that we will cover:
- Playwright Java's Default Global Timeouts
- Personalized timeout
- Source code
Playwright Java's Default Global Timeouts
Explore the official webpage that specifically focuses on explaining the different Timeouts in Playwright
https://playwright.dev/docs/test-timeouts
Below, it can be observed that there is no specified limit for 'Global Timeout'

Let's explore the practical implementation of 'timeout' to gain a better understanding of its usage.
Navigate 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

We can use the above text to locate this button

Once the button is clicked, the playwright will wait until the ajax data is received, utilizing its default timeout.
Next, the AJAX data output can be identified using class ‘bg-success’ or by text ‘Data loaded with AJAX get request’

We will next locate the ajax output data by using its classname, retrieve its innertext in a variable and finally print the same

Execute the code.
The ajax output message gets printed in the console, see below

Despite not specifying a timeout, the playwright automatically waited for the ajax output data for 15 seconds without throwing any exception.
Personalized timeout
There is a setDefaultTimeout() method using which we will now update the default timeout to 14 seconds

Execute the code.
This time, the test is unsuccessful because we specified a 14-second wait for the playwright, but the ajax data is loaded after 15 seconds
Let us modify the timeout parameters by commenting the 14-second timeout and implementing a default timeout of 16 seconds
Execute the code.
The test will be successful as the default timeout of 16 seconds exceeds the 15-second timeout required to show the ajax data
Let us modify the timeout parameters by commenting the 16-second timeout and implementing a default timeout of exact 15 seconds
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 timeouts work in playwright java.
Code snippet (default wait)
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;
import com.microsoft.playwright.options.SelectOption;
public class Blog8_Timeouts {
@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.navigate("http://uitestingplayground.com/ajax");
pg.locator("text=Button Triggering AJAX Request").click();
String ajmsg = pg.locator(".bg-success").innerText();
System.out.println(ajmsg);
pg.pause();
}
}
Code snippet (personalized wait)
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;
import com.microsoft.playwright.options.SelectOption;
public class Blog8_Timeouts {
@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();
String ajmsg = pg.locator(".bg-success").innerText();
System.out.println(ajmsg);
pg.pause();
}
}
Thanks.