← Back to Discord Forum

How to add Trace attachments using .NET

candritzky_11967posted in #help-playwright
Open in Discord

How can I add attachments to traces (aka Trace.zip) from .NET code? I want to make them available via the Trace Viewer.

I found this blog post that shows how to do it from TypeScript using testInfo.attach: https://timdeschryver.dev/blog/include-attachments-to-your-playwright-report#add-a-downloaded-file-to-the-report

But I don't find something like TestInfo in the .NET API (using MSTest).

This thread is trying to answer question "How can I add attachments to traces from .NET code using MSTest?"

0 replies


Attaching Traces to MSTest Results with Playwright in .NET

First, install the necessary dependencies, Microsoft.Playwright and MSTest.TestFramework, using the NuGet Package Manager or by running the following commands:

dotnet add package Microsoft.Playwright
dotnet add package MSTest.TestFramework

Next, create a new test class with the [TestClass] attribute and a test method with the [TestMethod] attribute.

When running your tests, set the --trace flag to "on" to record a trace.

To attach files to traces, use Playwright's Page.Context.Tracing.Start method and Page.Context.Tracing.AddTraceChunk. Here's an example:

using Microsoft.Playwright;
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class MyTests
  public async Task MyTest()
    using var playwright = await Playwright.CreateAsync();
    await using var browser = await playwright.Chromium.LaunchAsync();
    await using var context = await browser.NewContextAsync();
    var page = await context.NewPageAsync();

    var tracePath = Path.Combine(Directory.GetCurrentDirectory(), "trace.zip");
    await page.Context.Tracing.StartAsync(new TracingStartOptions
      Screenshots = true,
      Snapshots = true,
      Path = tracePath

    // Perform your test actions here.

    var traceBuffer = await page.Context.Tracing.StopAsync();

In this example, we start tracing and specify options such as capturing screenshots and snapshots. After performing your test actions, stop tracing and save the resulting trace buffer to a file. Attach this file to the test result using TestContext.AddResultFile.

When running your tests with MSTest, you'll see the attached traces in the test results. For more details, check the official documentation.

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.