Skip to content

Playwright: method grabRecordedNetworkTraffics sent error when use twice #4142

@asolodova

Description

@asolodova

What are you trying to achieve?

I want to wait for the specific list of requests.

What do you get instead?

When I call the 'blabla' method sequentially, my test fails with the following error: request.response.then is not a function

  1) Test
       Check main page @test1:
      request.response.then is not a function
      at node_modules/codeceptjs/lib/helper/Playwright.js:2959:71
      at Array.map (<anonymous>)
      at Playwright.grabRecordedNetworkTraffics (node_modules/codeceptjs/lib/helper/Playwright.js:2959:31)
  
  Scenario Steps:
  - I.grabRecordedNetworkTraffics() at Test.<anonymous> (./test/main_test.ts:10:11)
  - I.grabRecordedNetworkTraffics() at Test.<anonymous> (./test/main_test.ts:9:11)
  - I.amOnPage("https://google.com/") at Test.<anonymous> (./test/main_test.ts:8:5)
  - I.startRecordingTraffic() at Test.<anonymous> (./test/main_test.ts:7:5)

Looks like there is an issue with the way you're using await inside the map function. The await should be used inside an async function. Additionally, it needs to return a value from the map callback to ensure that each iteration resolves to a promise

  async grabRecordedNetworkTraffics() {
    if (!this.recording || !this.recordedAtLeastOnce) {
      throw new Error('Failure in test automation. You use "I.grabRecordedNetworkTraffics", but "I.startRecordingTraffic" was never called before.');
    }
    const requests = await this.requests;

    const promises = requests.map(async (request) => {
      try {
        const response = await request.response;

        let body;
        let status;
        let statusText;
        try {
          // There's no 'body' for some requests (redirect etc...)
          body = JSON.parse((await response.body()).toString());
          status = JSON.parse((await response.status()).toString());
          statusText = JSON.parse((await response.statusText()).toString());

        } catch (e) {
          // only interested in JSON, not HTML responses.
        }

        request.response = {
          status,
          statusText,
          body,
        };
      } catch (error) {
        // Handle errors when waiting for request.response
        console.error(error);
      }
    });

    await Promise.all(promises);

    return this.requests;
  }

Details

CodeceptJS version: 3.5.11
nodeInfo: 18.18.2
osInfo: macOS 14.1.2
chromeInfo: 120.0.6099.234
safariInfo: 17.1.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions