Sunday, 16 July 2023

Playwright: define timeouts globally

We can customize global timeout using following methods of BrowserContext.

 

void setDefaultTimeout(double timeout)

Set the default maximum time for all the methods accepting timeout option. Timeout is specified in milliseconds. Page.setDefaultNavigationTimeout(), Page.setDefaultTimeout()} and BrowserContext.setDefaultNavigationTimeout() take priority over BrowserContext.setDefaultTimeout().

 

void setDefaultNavigationTimeout(double timeout)

This setting will set the default maximum navigation time for the following methods and related shortcuts.  Timeout is specified in milliseconds.

 

a.   Page.goBack()

b.   Page.goForward()

c.    Page.navigate()

d.   Page.reload()

e.   Page.setContent()

f.     Page.waitForNavigation()

 

Page.setDefaultNavigationTimeout() and Page.setDefaultTimeout() take priority over BrowserContext.setDefaultNavigationTimeout().

Example

browserContext.setDefaultTimeout(5000);
browserContext.setDefaultNavigationTimeout(5000);

 

Find the below working application.

 

CustomizeTimeoutDemo.java
package com.sample.app.miscellaneous;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class CustomizeTimeoutDemo {

	public static void main(String[] args) {
		try (Playwright playwright = Playwright.create();
				Browser browser = playwright.chromium().launch();
				BrowserContext browserContext = browser.newContext();) {

			browserContext.setDefaultTimeout(5000);
			browserContext.setDefaultNavigationTimeout(5000);

			Page page = browser.newPage();
			page.navigate("https://self-learning-java-tutorial.blogspot.com/");
			System.out.println(page.title());
		}
	}

}

 

 


Previous                                                 Next                                                 Home

No comments:

Post a Comment