How can I use soft assertions and custom error messages in Playwright to handle failed assertions and create a 'warning' state?


Handling Failed Assertions in Playwright

In Playwright, you can handle failed assertions and create a 'warning' state using soft assertions and custom error messages.

Soft Assertions

Soft assertions don't stop the test execution when they fail. Instead, they compile a list of failed assertions to display at the end of the test. Here's how you can use them:

await expect.soft(page.getByTestId('status')).toHaveText('Success');
await expect.soft(page.getByTestId('eta')).toHaveText('1 day');

Custom Error Messages

You can provide additional context to your failed assertions by adding custom error messages. Here's an example:

await expect(page.getByText('Name'), 'should be logged in').toBeVisible();

If the assertion fails, the error message will include "should be logged in" along with other details about the failure.

Checking for Soft Assertion Failures

You can check if there were any soft assertion failures during the test execution by accessing test.info().errors. If there were failures, you can choose to skip further tests or perform additional actions based on your needs. Here's how:

if (test.info().errors.length > 0) {
  // Perform actions based on failure
  // Add tags or create new "warning" state as needed
} else {
  // Continue running further tests

By using these techniques, you can effectively handle failed assertions and take appropriate actions based on your requirements. For more tips on efficient Playwright test scripts, check out this blog post.


Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.