Skip to main content
Version: 1.23

Network

Playwright provides APIs to monitor and modify network traffic, both HTTP and HTTPS. Any requests that page does, including XHRs and fetch requests, can be tracked, modified and handled.


HTTP Authentication

const context = await browser.newContext({
httpCredentials: {
username: 'bill',
password: 'pa55w0rd',
},
});
const page = await context.newPage();
await page.goto('https://example.com');

API reference

HTTP Proxy

You can configure pages to load over the HTTP(S) proxy or SOCKSv5. Proxy can be either set globally for the entire browser, or for each browser context individually.

You can optionally specify username and password for HTTP(S) proxy, you can also specify hosts to bypass proxy for.

Here is an example of a global proxy:

const browser = await chromium.launch({
proxy: {
server: 'http://myproxy.com:3128',
username: 'usr',
password: 'pwd'
}
});

When specifying proxy for each context individually, Chromium on Windows needs a hint that proxy will be set. This is done via passing a non-empty proxy server to the browser itself. Here is an example of a context-specific proxy:

const browser = await chromium.launch({
// Browser proxy option is required for Chromium on Windows.
proxy: { server: 'per-context' }
});
const context = await browser.newContext({
proxy: { server: 'http://myproxy.com:3128' }
})

Network events

You can monitor all the requests and responses:

const { chromium, webkit, firefox } = require('playwright');

(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();

// Subscribe to 'request' and 'response' events.
page.on('request', request =>
console.log('>>', request.method(), request.url()));
page.on('response', response =>
console.log('<<', response.status(), response.url()));
await page.goto('https://example.com');

await browser.close();
})();

Or wait for a network response after the button click:

// Use a glob URL pattern
const [response] = await Promise.all([
page.waitForResponse('**/api/fetch_data'),
page.click('button#update'),
]);

Variations

// Use a RegExp
const [response] = await Promise.all([
page.waitForResponse(/\.jpeg$/),
page.click('button#update'),
]);

// Use a predicate taking a Response object
const [response] = await Promise.all([
page.waitForResponse(response => response.url().includes(token)),
page.click('button#update'),
]);

API reference


Handle requests

await page.route('**/api/fetch_data', route => route.fulfill({
status: 200,
body: testData,
}));
await page.goto('https://example.com');

You can mock API endpoints via handling the network requests in your Playwright script.

Variations

// Set up route on the entire browser context.
// It will apply to popup windows and opened links.

await browserContext.route('**/api/login', route => route.fulfill({
status: 200,
body: 'accept',
}));
await page.goto('https://example.com');

API reference


Modify requests

// Delete header
await page.route('**/*', route => {
const headers = route.request().headers();
delete headers['X-Secret'];
route.continue({headers});
});

// Continue requests as POST.
await page.route('**/*', route => route.continue({method: 'POST'}));

You can continue requests with modifications. Example above removes an HTTP header from the outgoing requests.

Abort requests

await page.route('**/*.{png,jpg,jpeg}', route => route.abort());

// Abort based on the request type
await page.route('**/*', route => {
return route.request().resourceType() === 'image' ?
route.abort() : route.continue();
});

API reference


Modify responses

To modify a response use APIRequestContext to get original response and then pass the response to route.fulfill([options]). You can override individual fields on the response via options:

await page.route('**/title.html', async route => {
// Fetch original response.
const response = await page.request.fetch(route.request());
// Add a prefix to the title.
let body = await response.text();
body = body.replace('<title>', '<title>My prefix:');
route.fulfill({
// Pass all fields from the response.
response,
// Override response body.
body,
// Force content type to be html.
headers: {
...response.headers(),
'content-type': 'text/html'
}
});
});

API reference


Record and replay requests

You can record network activity as an HTTP Archive file (HAR). Later on, this archive can be used to mock responses to the network requests. You'll need to:

  1. Record a HAR file.
  2. Commit the HAR file alongside the tests.
  3. Route requests using the saved HAR files in the tests.

Recording HAR with CLI

Open the browser with Playwright CLI and pass --save-har option to produce a HAR file. Optionally, use --save-har-glob to only save requests you are interested in, for example API endpoints. If the har file name ends with .zip, artifacts are written as separate files and are all compressed into a single zip.

# Save API requests from example.com as "example.har" archive.
npx playwright open --save-har=example.har --save-har-glob="**/api/**" https://example.com

Recording HAR with a script

Alternatively, instead of using the CLI, you can record HAR programmatically. Pass har option when creating a BrowserContext with browser.newContext([options]) to create an archive. If the har file name ends with .zip, artifacts are written as separate files and are all compressed into a single zip.

const context = await browser.newContext({
recordHar: { path: 'example.har', urlFilter: '**/api/**' }
});

// ... Perform actions ...

// Close context to ensure HAR is saved to disk.
await context.close();

Replaying from HAR

Use page.routeFromHAR(har[, options]) or browserContext.routeFromHAR(har[, options]) to serve matching responses from the HAR file.

// Replay API requests from HAR.
// Either use a matching response from the HAR,
// or abort the request if nothing matches.
await page.routeFromHAR('example.har');

HAR replay matches URL and HTTP method strictly. For POST requests, it also matches POST payloads strictly. If multiple recordings match a request, the one with the most matching headers is picked. An entry resulting in a redirect will be followed automatically.

Similar to when recording, if given HAR file name ends with .zip, it is considered an archive containing the HAR file along with network payloads stored as separate entries. You can also extract this archive, edit payloads or HAR log manually and point to the extracted har file. All the payloads will be resolved relative to the extracted har file on the file system.


WebSockets

Playwright supports WebSockets inspection out of the box. Every time WebSocket is created, page.on('websocket') event is fired. This event contains the WebSocket instance for further web socket frames inspection:

page.on('websocket', ws => {
console.log(`WebSocket opened: ${ws.url()}>`);
ws.on('framesent', event => console.log(event.payload));
ws.on('framereceived', event => console.log(event.payload));
ws.on('close', () => console.log('WebSocket closed'));
});

API reference