Search code examples
node.jspuppeteerplaywright

Get all responses of the same url in a page using playwright


good day guys,

I want get all the responses of the same url in a page, so that I can merge the responses to process.

enter image description here

I tried with this code, it gives first response only, but there are ten of the same url request data/api.json.

 const responsePromise = page.waitForResponse(response => {
      response.url().includes('data/api.json') && response.status() === 200
    });
    const response = await responsePromise;
    const data = await response.json()
    console.log(await response.allHeaders(), "allheader")

Solution

  • You can assign handlers on your requests, using page.on.

    You can declare an array, then push needed requests to it on event requestfinished inside handler, then load the site (or do action that leads to your requests), use waitForResponse of first request, wait for some condition when you expect that all requests were received (for example, for requests collection length is not changing along some time interval or network idle).

        const responses: HTTPResponse[] = [];
        const urlPart = 'data/api.json';
        const handler = req => {
          if (req.url().includes(urlPart) && req.response() && req.response().ok()) {
            responses.push(req.response());
          }
        }
        page.on('requestfinished', handler);
        await page.goto("https://yourURL");
        await page.waitForResponse(res => res.request().url().includes(urlPart));
        // propbably there should be another condition where you are sure that no new responses are come
        const jsons = await Promise.all(responses.map(r => r.json()));
        jsons.forEach(json => console.log(json));