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

  1. const context = await browser.newContext({
  2. httpCredentials: {
  3. username: 'bill',
  4. password: 'pa55w0rd',
  5. },
  6. });
  7. const page = await context.newPage();
  8. await page.goto('https://example.com');

You can also use browserContext.setHTTPCredentials to update HTTP credentials of an existing context.

API reference

Handle file downloads

  1. const [ download ] = await Promise.all([
  2. page.waitForEvent('download'), // <-- start waiting for the download
  3. page.click('button#delayed-download') // <-- perform the action that directly or indirectly initiates it.
  4. ]);
  5. const path = await download.path();

For every attachment downloaded by the page, "download" event is emitted. If you create a browser context with the acceptDownloads: true, all these attachments are going to be downloaded into a temporary folder. You can obtain the download url, file system path and payload stream using the Download object from the event.

Variations

If you have no idea what initiates the download, you can still handle the event:

  1. page.on('download', download => download.path().then(console.log));

Note that handling the event forks the control flow and makes script harder to follow. Your scenario might end while you are downloading a file since your main control flow is not awaiting for this operation to resolve.

API reference

Network events

You can monitor all the requests and responses:

  1. const { chromium, webkit, firefox } = require('playwright');
  2. (async () => {
  3. const browser = await chromium.launch();
  4. const page = await browser.newPage();
  5. // Subscribe to 'request' and 'response' events.
  6. page.on('request', request =>
  7. console.log('>>', request.method(), request.url()));
  8. page.on('response', response =>
  9. console.log('<<', response.status(), response.url()));
  10. await page.goto('https://example.com');
  11. await browser.close();
  12. })();

Or wait for a network response after the button click:

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

Variations

  1. // Use a RegExp
  2. const [response] = await Promise.all([
  3. page.waitForResponse(/\.jpeg$/),
  4. page.click('button#update'),
  5. ]);
  6. // Use a predicate taking a Response object
  7. const [response] = await Promise.all([
  8. page.waitForResponse(response => response.url().includes(token)),
  9. page.click('button#update'),
  10. ]);

API reference

Handle requests

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

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

Variations

  1. // Set up route on the entire browser context.
  2. // It will apply to popup windows and opened links.
  3. await browserContext.route('**/api/login', route => route.fulfill({
  4. status: 200,
  5. body: 'accept',
  6. }));
  7. await page.goto('https://example.com');

API reference

Modify requests

  1. // Delete header
  2. await page.route('**/*', route => {
  3. const headers = route.request().headers();
  4. delete headers['X-Secret'];
  5. route.continue({headers});
  6. });
  7. // Continue requests as POST.
  8. 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

  1. await page.route('**/*.{png,jpg,jpeg}', route => route.abort());
  2. // Abort based on the request type
  3. await page.route('**/*', route => {
  4. return route.request().resourceType() === 'image' ?
  5. route.abort() : route.continue();
  6. });

API reference