In Playwright, you can handle failed assertions and create a 'warning' state using soft assertions and custom error messages.
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');
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.
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.
If you still have questions, please ask a question and I will try to answer it.
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 [email protected].