new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Playwright · API reference · all subjects

core page methods - network

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

route.request().postDataJSON() method

route.request().postDataJSON() extracts the JSON-parsed request body from a POST request. Returns the parsed JSON object from the request payload.

route.fetch() method

route.fetch() makes the actual network request and returns the real response object. Used in route handlers to intercept and modify real responses before fulfilling them.

route.fulfill() method parameters

route.fulfill() completes a route with a mocked response. Parameters include: status (HTTP status code), body (response body as string or JSON), headers (object of response headers), response (real response object to use as base), and json (JSON object to serialize as body).

unroute-all-options-behavior for JS, C#, Python

The behavior parameter is UnrouteBehavior ('wait', 'ignoreErrors', 'default'). 'default': no wait, error may result in unhandled. 'wait': wait for calls to finish. 'ignoreErrors': no wait, errors silently caught. Since v1.41.

Page.request event for issued requests

The request event is emitted when a page issues a request (since v1.8). The request object is read-only. To intercept and mutate requests, use Page.route() or BrowserContext.route().

Page.requestFailed event for failed requests

The requestFailed event is emitted when a request fails, for example by timing out (since v1.9). HTTP Error responses like 404 or 503 are still successful responses from HTTP standpoint, so they complete with requestFinished, not requestFailed. A request is only considered failed when the client cannot get an HTTP response from the server, e.g. due to network error net::ERR_FAILED.

Page.requestFinished event on successful request completion

The requestFinished event is emitted when a request finishes successfully after downloading the response body (since v1.9). For a successful response, the sequence of events is request, response, and requestfinished.

Page.response event on response headers received

The response event is emitted when response status and headers are received for a request (since v1.8). For a successful response, the sequence of events is request, response, and requestfinished.

Page.webSocket event for WebSocket requests

The webSocket event is emitted when a WebSocket request is sent (since v1.9). The event handler receives a WebSocket instance.

Page.requestFailed event example logs failed requests

Example showing how to handle Page.requestFailed event. Code attaches a handler that logs the request URL and failure error text: page.on('requestfailed', request => console.log(request.url() + ' ' + request.failure().errorText))

Page.requests - get network requests

Page.requests() is an async method (since v1.56, returns Array<Request>) that returns up to 100 last network requests from the page. Returned requests should be accessed immediately as they may be collected later to prevent unbounded memory growth. Once collected, most request information becomes unavailable. Requests from Page.request event are not collected.

Page.route - intercept network requests

Page.route() is an async method (since v1.8, returns Disposable) that sets up network request routing/interception. Once enabled, every request matching the URL pattern stalls unless continued, fulfilled, or aborted. Handler is only called for first URL if response is redirect. Page routes take precedence over browser context routes. To remove route, use Page.unroute. Accepts: url (string|RegExp|URLPattern|function returning boolean). Enabling routing disables HTTP cache. Service Workers are not intercepted.

Page.route URL parameter types

The URL parameter for Page.route() accepts: a glob pattern (string), a RegExp pattern, or a function that receives a URL and returns a boolean. If Browser.newContext.baseURL is set and the URL is a string not starting with '*', it is resolved using the new URL() constructor.

Page.route handler signature

The handler parameter for Page.route() has different signatures by language: For JavaScript and Python, handler is a function that receives Route and Request parameters and returns Promise<any> or any. For C# and Java, handler is a function that receives only Route.

Page.route times option

Page.route() accepts a times option (integer, added in v1.15) that specifies how often a route should be used. By default it will be used every time.

Page.routeFromHAR method parameters and options

Page.routeFromHAR() is an async method (added in v1.23) that serves network requests from a HAR file. Required parameter: har (path to HAR file, resolved relative to current working directory if relative). Options: notFound ('abort' or 'fallback', defaults to 'abort'), update (boolean, updates HAR with actual network info instead of serving from file), url (string or RegExp to match request URLs), updateMode ('full' or 'minimal', defaults to 'minimal' in v1.32), updateContent ('embed' or 'attach' for resource content management, added in v1.32).

Page.routeWebSocket method

Page.routeWebSocket() (added in v1.48) allows modifying WebSocket connections made by the page. Only WebSockets created after calling this method will be routed; it is recommended to call this method before navigating the page. The url parameter accepts: for JavaScript, a string, RegExp, URLPattern, or function receiving URL that returns boolean; for Python/C#/Java, a string, RegExp, or function receiving URL that returns boolean. The handler parameter receives WebSocketRoute and returns Promise<any> or any for JavaScript/Python, or just WebSocketRoute for C#/Java.

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 single message.

Page.screencast property

Page.screencast is a property (added in v1.59) that returns a Screencast object associated with the page. It can be used to listen for 'screencastFrame' events and start/stop recording.

Page.screenshot return type and options

Page.screenshot() (async, added in v1.8) returns a Buffer containing the captured screenshot. Options include: timeout, signal, fullPage, clip, maskColor (v1.34), and style (v1.41).

Page.setExtraHTTPHeaders method

Page.setExtraHTTPHeaders() (async, added in v1.8) sends extra HTTP headers with every request the page initiates. Parameter: headers (object with string keys and string values). Note: does not guarantee order of headers in outgoing requests.

Page.unrouteAll method

Page.unrouteAll() (async, added in v1.41) removes all routes created with Page.route and Page.routeFromHAR. Options: behavior (controls what happens to pending requests).

Page.unroute method

Page.unroute() (async, added in v1.8) removes a route created with Page.route. When handler is not specified, removes all routes for the URL. For JavaScript, url accepts string, RegExp, URLPattern, or function receiving URL that returns boolean. For Python/C#/Java, url accepts string, RegExp, or function receiving URL that returns boolean. Handler is optional and varies by language.

Page.video method

Page.video() (added in v1.8) returns null or a Video object associated with the page. Can be used to access the video file when using the recordVideo context option.

Page.waitForRequestFinished method

Page.waitForRequestFinished() (async, added in v1.12, available in Java/Python/C#, with alias expect_request_finished in Python and RunAndWaitForRequestFinished in C#) performs action and waits for a Request to finish loading. Returns Request (or EventContextManager in Python). If predicate provided, passes Request into predicate and waits for truthy return. Throws if page closes before requestFinished event fires. Options: predicate (function receiving Request returning boolean), timeout, signal. Parameter: action (C#), callback (Java).

Page.waitForResponse method

Page.waitForResponse() (async, added in v1.8, alias expect_response in Python and RunAndWaitForResponse in C#) waits for matching response and returns it. Returns Response (or EventContextManager in Python). Parameter: urlOrPredicate (string, RegExp, or function receiving Response returning boolean). For JavaScript, function can return Promise<boolean>. Option: timeout (maximum wait time in milliseconds, defaults to 30 seconds, pass 0 to disable). When Browser.newContext.baseURL is set and URL is a path, it gets merged via new URL() constructor. Option: signal.

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; ``` This demonstrates waiting for a response with a specific URL.

Give your agent this brain