From d2ea6588d70728da85903923560a585f5bf0e24f Mon Sep 17 00:00:00 2001 From: Debbie O'Brien Date: Mon, 15 May 2023 20:08:26 +0200 Subject: [PATCH] docs(best-practises): improve soft assertions (#22909) Co-authored-by: Max Schmitt --- docs/src/best-practices-js.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/src/best-practices-js.md b/docs/src/best-practices-js.md index 4296675627..d059b34a68 100644 --- a/docs/src/best-practices-js.md +++ b/docs/src/best-practices-js.md @@ -40,20 +40,6 @@ test('second', async ({ page }) => { You can also reuse the signed-in state in the tests with [global setup](/auth.md#reuse-signed-in-state). That way you can log in only once and then skip the log in step for all of the tests. -### Write fewer tests but longer tests - -When it comes to end to end testing having long tests is not a bad thing. It's ok to have multiple actions and assertions in your test so you can test complete App flows. You should avoid separating your assertions into individual test blocks as it doesn't really bring much value and just slows down the running of your tests. - -If your test does fail, Playwright will give you an error message showing what part of the test failed which you can see either in VS Code, the terminal, the HTML report, or the trace viewer. You can also use [soft assertions](/test-assertions.md#soft-assertions) which do not terminate test execution but mark the test as failed. - -```js -// Make a few checks that will not stop the test when failed... -await expect.soft(page.getByTestId('status')).toHaveText('Success'); - -// ... and continue the test to check more things. -await page.getByRole('link', { name: 'next page' }).click(); -``` - ### Avoid testing third-party dependencies Only test what you control. Don't try to test links to external sites or third party servers that you do not control. Not only is it time consuming and can slow down your tests but also you can not control the content of the page you are linking to, or if there are cookie banners or overlay pages or anything else that might cause your test to fail. @@ -286,3 +272,17 @@ Playwright can [shard](./test-parallel.md#shard-tests-between-multiple-machines) ```js npx playwright test --shard=1/3 ``` + +## Productivity tips + +### Use Soft assertions + +If your test fails, Playwright will give you an error message showing what part of the test failed which you can see either in VS Code, the terminal, the HTML report, or the trace viewer. However, you can also use [soft assertions](/test-assertions.md#soft-assertions) these do not immediately terminate the test execution, but rather compile and display a list of failed assertions once the test ended. + +```js +// Make a few checks that will not stop the test when failed... +await expect.soft(page.getByTestId('status')).toHaveText('Success'); + +// ... and continue the test to check more things. +await page.getByRole('link', { name: 'next page' }).click(); +``` \ No newline at end of file