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:
expect(test.info().errors).toHaveLength(0);
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
.
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].