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?"
Related Ask AI answer for "How do I record and attach traces to test results in MSTest using 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;
[TestClass]
public class MyTests
{
[TestMethod]
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();
TestContext.AddResultFile(tracePath);
}
}
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.
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].