I am new to Playwright, need some help on intercepting http calls.
I have scenario, on clicking on button , will get 2 requests
request 1 -> baseurl/session/
request 2 -> sessiondata
I need to verify if request1 got 200 response and also from request 2 I need to validate request payload. In cypress I can achieve this by:
cy.intercept(request1).as(req1)
cy.intecpt(requrest2).as (req2)
cy.get(button).click()
cy.get(req1) .its('response.statusCode')
.should('eq', 200);
cy.get(req2).its("request.body")
.then((body) => {
console.log(body);
});
I am struggling to achieve the same in Playwright, confused with waitforRequest & waitforResponse,
Thanks for response, but getting I am request body is [obj][obj], request2.header is undefined, what am I missing here?
const requestPromise1 = this.page.waitForResponse((res) =>
res.url().includes("url1")&& res.status() === 202);
const requestPromise2 = this.page.waitForResponse((res) =>
res.url().includes("url2")&& res.status() === 204);
await page.locator("<button locator>").click();
const request1 = await requestPromise1;
const request2 = await requestPromise2;
expect(request1.status()).toBe(202);
expect(request2.status()).toBe(204);
console.log("headerheader"+request2.header)
console.log("request2"+await request2.request());
Note: Url1 is POST , Url2 is PATCH with 204 response from which I need to get payload
Playwright can use
waitForResponse. The general workflow is to define the wait as a variable, do the action that triggers the network request, then await the wait.Then
responsevariable is a PlaywrightResponsetype, which means we can check the status from that variable. I think the easiest way would be to just use.ok(), which "Contains a boolean stating whether the response was successful (status in the range 200-299) or not."If we needed to check a specific status number, we could use
.status().Additionally, if you need the request associated with the response, that can be found by
.request()