Playwright Assert Response Body

Profile picture for user arilio666

In playwright, the response body can be asserted quickly using the response we used.

test.only('two', async ({request}) => {

       const response = await request.post('https://reqres.in/api/users/2',{
       data: {
           name: "John Wick",
           job: "Assasin"
       },
       })
       
     const responseBody =  JSON.parse(await response.text())
     console.log(responseBody)
     expect(response.status()).toBe(200)
expect(responseBody.name).toBe('John Wick')
expect(responseBody.job).toBe('Assasin')
expect(responseBody.updatedAt).toBeTruthy()

           });
   }
  •   Consider this POST request here, and we send data with name and job.
  •   Now we parse using JSON with the response to the text.
        

    expect(responseBody.name).toBe('John Wick')
expect(responseBody.job).toBe('Assasin')
  • We can now use this responseBody variable and expect assertion to verify the name and job as we did above.
        

Conclusion:

This is how we can assert the response body in the playwright.