microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
67.2k stars 3.69k forks source link

[BUG] HAR data not matching requests actual network traffic #17745

Closed smckee-r7 closed 2 years ago

smckee-r7 commented 2 years ago

Context:

Code Snippet

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({
    headless: false
  });
  const context = await browser.newContext({ recordHar: { path: './harFile.har' } });
  const page = await context.newPage();

  await page.route('**/*', async (route) => {
    const headers = {
      ...route.request().headers(),
      ...{ 'injected-header': '1' }
    };
    route.continue({ headers });
  });

  await page.goto('https://www.google.com/');

  page.close();
  context.close();
  browser.close();
})();

Describe the bug The har data that's recorded in harFile.har matches the original request information(before page.route() injects a header) rather than the actual network request traffic and therefore doesn’t contain the injected header. I would have thought that the HAR data should match the actual network requests and what the server would see?

Another example would be, when using page.route to change the request method type from GET to POST. Playwrights HAR file would incorrectly say it was a GET request when the server actually received a POST request.

arlendarcy commented 2 years ago

Additionally OPTIONS requests are missing from the output har file but if you output the har from the browser it contains the options requests.

yury-s commented 2 years ago

It was fixed by https://github.com/microsoft/playwright/pull/17027, you should update to Playwright v1.26.0+

arlendarcy commented 2 years ago

It was fixed by #17027, you should update to Playwright v1.26.0+

@yury-s OPTIONS requests are still missing on v1.26.1

EDIT: I can open a separate issue if required.

smckee-r7 commented 2 years ago

@yury-s that's perfect, thanks for that! I have found & opened another related issue that doesn't appear to be fixed yet #17802. If you get to chance to look over it that'd be great