Tutorial 10 – Set and customize default global timeouts in Playwright Java
What you will Learn in this blog:
Default Global Timeouts in Playwright Java
Custom timeout
Code snippets
Default Global Timeouts in Playwright Java
Launch official url that mentions about the descriptions of various Timeouts in Playwright
https://playwright.dev/docs/test-timeouts
Notice below that there is no limit set for ‘Global Timeout’
Let us see a pratical example to demonstrate the usage of ‘timeout’.
Launch http://uitestingplayground.com/ajax
Read the scenario description that says “after we press the button, the ajax data will appear after 15 seconds”. Thus the wait time here is 15 seconds
This means that, on clicking the button, the ajax data gets loaded after 15 seconds
Let’s inspect the button
So we can write
Once clicking the button, playwright will wait for the ajax data using it’s default timeout.
Let’s inspect the AJAX data.
We can identify the AJAX data using the class ‘bg-success’ or by the text ‘Data loaded with AJAX get request’
Let us locate the ajax data using it’s classname css selector and fetch it’s innertext and store it in a variable
Our code looks like below
Save and execute the test.
Notice that the test passes and the ajxmsg gets printed in the console
So, even though we did not mention any timeout, playwright did wait automatically for the ajax data for 15 seconds, it did not throw any exception.
Custom timeout
Let us now set the default timeout to 14 seconds
Save and execute.
Notice this time that the test fails because we asked playwright to wait for 14 seconds, however the ajax data gets loaded after 15 seconds
Let us comment 14 sec timeout and set 16 second default timeout
Save and execute.
The test will pass since 16 sec default timeout is greater than 15 second timeout to display ajax data
Let us comment 16 sec timeout and set default timeout of exact 15 sec
Save and execute.
The test will fail since 15 sec default timeout is exactly equal to 15 sec timeout to display ajax data
Feel free to try out by setting different timeouts.
So this is how the default timeout works and also how we can set the default timeouts.
Code snippet (default wait)
package com.w2a.pwjava;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class AutoWaitPW {
public static void main(String[] args) {
Playwright pw = Playwright.create();
Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
BrowserContext browserContext = browser.newContext();
Page page = browserContext.newPage();
//page.setDefaultTimeout(14000); //test fails
//page.setDefaultTimeout(16000); //test passes
page.setDefaultTimeout(15000);//test fails
page.navigate("http://uitestingplayground.com/ajax");
page.locator("text=Button Triggering AJAX Request").click();
String ajxmsg = page.locator(".bg-success").innerText();
System.out.println(ajxmsg);
}
}
Thanks for reading!