Version 1.19.1 of Playwright has been released and by reading the release notes I noticed a couple of cool new features (one of which was introduced in 1.18)
The first allows you to pass a message as an optional second parameter to an expect()
call.
Say you’re calling an API and expect a certain status code:
test('can POST a REST API and check response using approval style', async ({ request }) => {
const response = await request.post('https://my-json-server.typicode.com/webdriverjsdemo/webdriverjsdemo.github.io/posts', { data: { title: 'Post 4' } })
expect(response.status()).toBe(202)
})
If this fails you don’t have much info:
1) scenarios/alltests.spec.ts:62:3 › All tests › can POST a REST API and check response using approval style
Error: expect(received).toBe(expected) // Object.is equality
Expected: 202
Received: 201
With this new second parameter we can provide more info:
test('can POST a REST API and check response using approval style', async ({ request }) => {
const response = await request.post('https://my-json-server.typicode.com/webdriverjsdemo/webdriverjsdemo.github.io/posts', { data: { title: 'Post 4' } })
await expect(response.status(), `Response: ${await response.text()}`).toBe(202)
})
and get more detailed output
Error: Response: {
"title": "Post 4",
"id": 4
}
Expected: 202
Received: 201
The second new feature is the ability to use .toBeOK()
assertions instead of having to assert on status codes. This asserts the response is in the 2xx status range which means the request was OK.
We can now do this:
test('can POST a REST API and check response using approval style', async ({ request }) => {
const response = await request.post('https://my-json-server.typicode.com/webdriverjsdemo/webdriverjsdemo.github.io/posts', { data: { title: 'Post 4' } })
await expect(response, `Response: ${await response.text()}`).toBeOK();
})
note: you can also call .not.toBeOK()
if you are expecting an error.
Have you found any useful new Playwright features being released recently that you now can’t live without?