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.
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].