playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question] Observing preflight OPTIONS network requests

Open stephenash opened this issue 5 years ago • 3 comments

I'm working on some tests that want to look at the timings of network requests and am interested in recording the timing for the preflight request. When I run the a script like this in under debug mode and open the browser dev tools, I see the OPTIONS request being made

Screen Shot 2021-09-03 at 12 23 15

But when I setup a page.route() or page.on('requestfinished') listener, I am not able to observe the preflight call, only the GET request. Is there a special flag that I am missing in how the browser is launched that would allow me to view that data?

const playwright = require('playwright');

(async () => {
  const browser = await playwright.webkit.launch();
  const context = await browser.newContext();
  const page = await context.newPage();

  // Log and continue all network requests
  page.route('**', (route, request) => {
    console.log(`page.route: ${request.method()}, ${request.url()}`);
    route.continue();
  });

  page.on('requestfinished', (request) => {
    console.log(`requestfinished: ${request.method()} ${request.url()}`);
    console.dir(request.timing());
  });

  const response = await page.evaluate(async () => {
      const resp = await window.fetch('https://httpbin.org/get', {
          method: 'GET',
          headers: {
              'x-custom': 'true',
          }
      });

      return resp.json();
  });

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

stephenash avatar Sep 03 '21 19:09 stephenash

We don't currently fire network events in playwright for the preflight requests. Do you need it across all browsers? Asking because in Chromium we receive them from the browser and as a temp workaround you can listen to them via bare CDP proocol.

yury-s avatar Sep 03 '21 21:09 yury-s

Thanks for the pointer on using the CDP protocol. I don’t need cross browser support for these tests, so this should work.

stephenash avatar Sep 06 '21 01:09 stephenash

Closing as per above, please feel free to open a new issue if this does not cover your use case.

pavelfeldman avatar Sep 07 '21 20:09 pavelfeldman