top of page

Playwright Python HTML Report and Trace Viewer: Debug a Failing Test

11 hours ago
3 min read

So far you have mostly run tests one at a time and read the result in the terminal. In this lesson you run the whole suite at once, generate an HTML report, and use Playwright's Trace Viewer to see exactly why a test failed.



Before you start: you need the tests from the earlier lessons in tests/concepts. If you are following along, the last one is from Data-Driven Testing in Playwright Python.


Step 1: Run all your tests together

Run pytest with no file path:

pytest

pytest finds every file and function starting with test_ under your project and runs them all. As your suite grows, this becomes the normal way to check the application. But the terminal only shows the outcome: which tests passed and failed, with no further detail.


Step 2: Generate an HTML report

For a report you can open in the browser, add the --html option with a file name:

pytest --html=report.html

The first time, you'll get an error. pytest doesn't include HTML reporting by default, and in Python, Playwright relies on pytest for reporting. Install the pytest-html plugin:

pip install pytest-html

Run the same command again. This time pytest tells you where the report was generated. Open report.html in your browser.

What the report shows

  • The environment: Python packages and plugins installed

  • How many tests ran, passed, failed and were skipped

  • Each test, including which browser it ran on (Chromium here)

  • For a failed test, the same error as in the terminal: what was expected and what it got

Make one test fail on purpose, run the command again, and open the report. It's updated with the failure and its error, so you can see the summary and the reason in one place.


Step 3: Record a trace when a test fails

A report tells you that a test failed. A trace shows you what happened, step by step. To see one, break a test on purpose. In test_locators.py, misspell the button name, for example Add To Cat instead of Add To Cart.

Then run the tests with tracing kept only for failures:

pytest --tracing retain-on-failure

Playwright looks for a button named Add To Cat. It keeps waiting and retrying, as auto-waiting does, but no such button exists, so the test fails after the timeout.

You could turn tracing on for every test, but retain-on-failure keeps a trace only when something goes wrong, which is usually what you want. Playwright creates a test-results folder with a trace.zip file inside it for the failed test.


Step 4: Open the Trace Viewer

Open the trace with the show-trace command, pointing at that trace.zip (use Tab to complete the folder name):

playwright show-trace test-results/<failed-test-folder>/trace.zip

The Trace Viewer opens in a rich UI. It lists every action the test took, with snapshots of the page before and after each one. Here you can see the navigation succeeded, then the click on the button failed with a timeout, because the locator didn't match any element on the page.

Instead of guessing what went wrong, you can see it.

Put the correct button name back when you're done.


From running tests to managing a framework

Right now you run everything by hand from the terminal: you decide when to run, which options to add, and what to look at. That works while you're learning and the suite is small. As a project grows, it gets hard to remember the commands and to add reporting or tracing options to every run.

In real automation projects, these settings are defined once in central configuration. That's the shift from running tests to managing an automation framework, and it's the natural next step after this course.


What you covered

  • Running the whole suite with pytest

  • Installing pytest-html and generating an HTML report

  • Reading a failure in the report: expected versus actual

  • Recording traces with --tracing retain-on-failure

  • Debugging a failure step by step with playwright show-trace


The code for the whole course is in the playwright-python-course-yt repo on GitHub.


Get Started with Playwright Python: the free series

This post is Lesson 6 of my free Playwright Python course on YouTube.

Ready for a full framework? My Udemy course Playwright Automation Testing: Beginner to Pro Framework Design builds a production-style framework step by step, with configuration, reporting and CI/CD. It uses TypeScript, and the TypeScript you need is covered inside.

bottom of page