1

Currently in one of my test I am trying to check PUT request status. In one of the user scenarios when a user clicks Confirm button a PUT request is sent and if all good status code 200 is present in the Network tab. As far as I am aware in Playwright there is an option to check such things. According to https://playwright.dev/docs/api/class-page#page-wait-for-response it is possible to achieve that.

Now the issue that I am facing is that each time when a PUT request is sent to the destination endpoint, a random case number is present in the request. To avoid time and work I thought that I could use a ReqExp such as \d+ for that. However during the execution of the test, I can see by using Playwright API logs that the page.waitForResponse() fails each time. I know that the endpoint works correctly so there is no issue with it. I assume that my implementation of using ReqExp is causing all the fuzz.

Below I am placing my example method with the ReqExp. The place in the URL where the number is generated has been replaced with the following expression \d+. I want to make it a reusable method so I am not interested in the actual number, I know it will be there. I am more interested in the status code. If someone is more experience with those, please share some useful guidance. Thanks you all!

async confirmDealAsXYZ() {
    await this.page.waitForSelector(
      'span:has-text("Please confirm you would like to mark this deal as containing XYZ")'
    );
    await this.page.waitForSelector('div[class="ant-modal-footer"]');
    // await this.page.click('div[role="dialog"] button:has-text("Confirm")');

    // eslint-disable-next-line prefer-regex-literals
    const re = new RegExp(
      `https://myapplication-dev.net/some_ship_financing/some_new_finance/api/v1/some_cases/d+/assessment/xyz/`
    );

    await Promise.all([
      // Waits for the next response with the specified url
      this.page.waitForResponse(response => response.url() === re && response.status() === 200),
      // Triggers the response
      this.page.click('div[role="dialog"] button:has-text("Confirm")'),
    ]);
  }
1

There are 2 mistakes as far as I can see:

1/ strictly comparing a string object with a different object (re) will always return false:

You need to use some method like RegExp.prototype.test(), for example:

let re = new RegExp('\\d+');
re.test('1234'); // this returns true

2/ if you pass a string to the RegExp constructor, you need to escape characted classes with \\:

let re = new RegExp('\\d+');
re.test('1234'); // this returns true

doing only:

let re = new RegExp('\d+');
re.test('1234'); // this returns false

or:

let re = new RegExp('d+');
re.test('1234'); // this returns false

doesn't make sense in this context.

1
  • thanks for useful answers, share more light on my work. Now I know where was wrong. Cheers! Jul 2 at 9:08
0

Simple alternative using 'includes'

Responses also can be verified simply using 'includes':

(by including the desired unique substring from the url)

page.waitForResponse(resp => resp.url().includes('/assessment/xyz/') && resp.status() === 200)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.