How do I display custom error messages in Playwright HTML reports based on assertion results?


To display custom error messages in Playwright HTML reports based on assertion results, use the expect function with a custom error message as the second argument. For example, to check if an element with text "Name" is visible and provide a custom error message if it's not, use the following code:

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

If the assertion fails, the error message in the HTML report will be "should be logged in".

To continue running tests even if an assertion fails, use soft assertions by adding ".soft" after "expect":

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

To stop further tests from running if there were any soft assertion failures, use:


You can also pre-configure expect instances with their own defaults, such as timeout and softness, using expect.configure(). This helps reduce repetitive code and streamline testing.

In summary, Playwright offers various options for displaying custom messages based on assertion outcomes in HTML reports through its expect function and configuration options like attachmentsBaseURL.

Thank you!
Was this helpful?
Still have questions?

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

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.