Use Tracing to Find Problems in Your Playwright Tests

Playwright has a great trace viewer that allows us to see exactly what is happening in our tests. This is especially helpful when something goes wrong. Let’s take a look at how we activate tracing and access the information it collects.

 

Activate tracing

We need a bit of code that runs before and after our tests. The SetUp and TearDown methods of NUnit are the perfect place to put them. However, we need a way to tell those methods which tests to trace, otherwise every test collects tracing info. The simplest way I found is to use a test category, like TraceIt, that we put on the tests we are interested in:

We can then check for this attribute in the SetUp and TearDown methods and only create the tracing data if the attribute is present:

 

Collect the tracing data

With our methods in place, we can add the test category to the tests we want to trace and run NUnit. At the end we should find a *.zip file with the test name in the output directory:

For every test with the TraceIt category is a *.zip file created

 

Open the trace file

We can go to the output directory and run playwright.ps1 with the option show-trace and the *.zip file we are interested in:

If Playwright complains that the *.zip file does not exist, omit it from the command and pick it in the trace viewer manually:

Pick the trace file manually

 

Inspect the trace

The trace viewer shows us the browser through the whole test. We have snapshots of the DOM at all times and can interact with the site as if we had it open in the developer tools of a regular browser:

We can see the whole timeline and use the developer tools to inspect the page.

I find this one of the greatest improvements over the tooling we had so far for end-to-end tests. It makes everything so much simpler to debug, then everything the browser interacted with is here.

 

Next

Should your tests stop working, use the trace viewer to figure out what is going on. But something that is still not enough. Next week we take screenshots and record videos of our tests.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.