new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Playwright · API reference · all subjects

network & routing

13 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Page.requests return type and behavior

Page.requests returns Array<Request>. It returns up to (currently) 100 last network requests from this page. Returned requests should be accessed immediately as they might be collected to prevent unbounded memory growth. Once collected, retrieving most information is impossible. Requests from Page.request event are not collected. Available since v1.56.

Page.route method behavior and routing

Page.route provides the capability to modify network requests made by a page. Once routing is enabled, every request matching the URL pattern will stall unless continued, fulfilled, or aborted. The handler is only called for the first URL if the response is a redirect. Page.route will not intercept requests intercepted by Service Worker or the first request of a popup page. Page.route takes precedence over BrowserContext.route. Enabling routing disables HTTP cache.

Page.route return type

Page.route returns Disposable.

Page.route URL parameter types

Page.route url parameter accepts: string (glob pattern), RegExp (regex pattern), URLPattern (URL pattern - JavaScript only), or function receiving URL object returning boolean (predicate). If Browser.newContext.baseURL is set and URL is a string not starting with *, it is resolved using new URL() constructor.

Page.routeFromHAR method signature

Page.routeFromHAR is an async method available since v1.23. It serves network requests from a HAR file. Playwright will not serve requests intercepted by Service Worker from the HAR file, and disabling Service Workers is recommended by setting Browser.newContext.serviceWorkers to 'block'.

Page.routeFromHAR parameters and options

Page.routeFromHAR accepts: har (path, required) - path to HAR file; notFound (optional, HarNotFound<"abort"|"fallback">) - defaults to 'abort', controls behavior for missing requests; update (optional, boolean) - if true, updates HAR with actual network data instead of serving from file, written to disk when BrowserContext.close is called; url (string|RegExp, required) - glob pattern, regex or predicate to match request URL, only matching requests served from HAR; updateMode (optional, HarMode<"full"|"minimal">) - defaults to 'minimal', controls what information is recorded; updateContent (optional, RouteFromHarUpdateContentPolicy<"embed"|"attach">) - controls resource content management, 'attach' persists as separate files, 'embed' stores inline.

Page.routeWebSocket method signature

Page.routeWebSocket is an async method available since v1.48. It allows modifying websocket connections made by the page. Only WebSockets created after this method is called will be routed, so it should be called before navigating the page.

Page.routeWebSocket example

```js await page.routeWebSocket('/ws', ws => { ws.onMessage(message => { if (message === 'request') ws.send('response'); }); }); ``` This example shows a simple WebSocket mock that responds to a 'request' message with a 'response'.

Page.setExtraHTTPHeaders does not guarantee header order

Page.setExtraHTTPHeaders does not guarantee the order of headers in the outgoing requests.

Page.unrouteAll method signature

Page.unrouteAll is an async method (since v1.41) that removes all routes created with Page.route and Page.routeFromHAR.

Page.unroute method signature

Page.unroute is an async method that removes a route created with Page.route. When handler is not specified, removes all routes for the url.

Page.waitForResponse example

```js // Start waiting for response before clicking. Note no await. const responsePromise = page.waitForResponse('https://example.com/resource'); await page.getByText('trigger response').click(); const response = await responsePromise; // Alternative way with a predicate. Note no await. const responsePromise = page.waitForResponse(response => response.url() === 'https://example.com' && response.status() === 200 && response.request().method() === 'GET' ); await page.getByText('trigger response').click(); const response = await responsePromise; ``` This example shows how to wait for a response matching a URL or predicate.

Request resourceTiming structure

The resourceTiming property returns an Object with the following properties: startTime (float, request start time in milliseconds since January 1, 1970 00:00:00 UTC), domainLookupStart (float, milliseconds relative to startTime, -1 if not available), domainLookupEnd (float, milliseconds relative to startTime, -1 if not available), connectStart (float, milliseconds relative to startTime, -1 if not available), secureConnectionStart (float, milliseconds relative to startTime, -1 if not available), connectEnd (float, milliseconds relative to startTime, -1 if not available), requestStart (float, milliseconds relative to startTime, -1 if not available), responseStart (float, milliseconds relative to startTime, -1 if not available), responseEnd (float, milliseconds relative to startTime, -1 if not available).

Give your agent this brain