diff --git a/docs/src/trace-viewer-intro-csharp-java-python.md b/docs/src/trace-viewer-intro-csharp-java-python.md index 5f10abe68e..68fcef6acc 100644 --- a/docs/src/trace-viewer-intro-csharp-java-python.md +++ b/docs/src/trace-viewer-intro-csharp-java-python.md @@ -62,7 +62,7 @@ context.tracing.stop(path = "trace.zip") ## Recording a trace -* langs: csharp, java +* langs: java Traces can be recorded using the [`property: BrowserContext.tracing`] API as follows: @@ -84,31 +84,119 @@ context.tracing().stop(new Tracing.StopOptions() .setPath(Paths.get("trace.zip"))); ``` -```csharp -using var playwright = await Playwright.CreateAsync(); -var browser = await playwright.Chromium.LaunchAsync(); -await using var context = await browser.NewContextAsync(); - -// Start tracing before creating / navigating a page. -await context.Tracing.StartAsync(new() -{ - Screenshots = true, - Snapshots = true, - Sources = true -}); - -var page = await context.NewPageAsync(); -await page.GotoAsync("https://playwright.dev"); - -// Stop tracing and export it into a zip archive. -await context.Tracing.StopAsync(new() -{ - Path = "trace.zip" -}); -``` This will record the trace and place it into the file named `trace.zip`. +## Recording a trace +* langs: csharp + +Traces can be recorded using the [`property: BrowserContext.tracing`] API as follows: + + + + +```csharp +namespace PlaywrightTests; + +[Parallelizable(ParallelScope.Self)] +[TestFixture] +public class Tests : PageTest +{ + [SetUp] + public async Task Setup() + { + await Context.Tracing.StartAsync(new() + { + Title = TestContext.CurrentContext.Test.ClassName + "." + TestContext.CurrentContext.Test.Name, + Screenshots = true, + Snapshots = true, + Sources = true + }); + } + + [TearDown] + public async Task TearDown() + { + // This will produce e.g.: + // bin/Debug/net8.0/playwright-traces/PlaywrightTests.Tests.Test1.zip + await Context.Tracing.StopAsync(new() + { + Path = Path.Combine( + TestContext.CurrentContext.WorkDirectory, + "playwright-traces", + $"{TestContext.CurrentContext.Test.ClassName}.{TestContext.CurrentContext.Test.Name}.zip" + ) + }); + } + + [Test] + public async Task TestYourOnlineShop() + { + // .. + } +} +``` + + + + +```csharp +using System.Text.RegularExpressions; +using Microsoft.Playwright; +using Microsoft.Playwright.MSTest; + +namespace PlaywrightTestsMSTest; + +[TestClass] +public class UnitTest1 : PageTest +{ + [TestInitialize] + public async Task TestInitialize() + { + await Context.Tracing.StartAsync(new() + { + Title = TestContext.TestName, + Screenshots = true, + Snapshots = true, + Sources = true + }); + } + + [TestCleanup] + public async Task TestCleanup() + { + // This will produce e.g.: + // bin/Debug/net8.0/playwright-traces/PlaywrightTests.UnitTest1.zip + await Context.Tracing.StopAsync(new() + { + Path = Path.Combine( + Environment.CurrentDirectory, + "playwright-traces", + $"{TestContext.FullyQualifiedTestClassName}.zip" + ) + }); + } + + [TestMethod] + public async Task TestYourOnlineShop() + { + // ... + } +} +``` + + + + +This will record the trace and place it into the `bin/Debug/net8.0/playwright-traces/` directory. + ## Opening the trace You can open the saved trace using the Playwright CLI or in your browser on [`trace.playwright.dev`](https://trace.playwright.dev). Make sure to add the full path to where your `trace.zip` file is located. This should include the `test-results` directory followed by the test name and then `trace.zip`. diff --git a/docs/src/trace-viewer.md b/docs/src/trace-viewer.md index 017679abf1..1f50025c32 100644 --- a/docs/src/trace-viewer.md +++ b/docs/src/trace-viewer.md @@ -211,7 +211,7 @@ context.tracing.stop(path = "trace.zip") ## Recording a trace -* langs: java, csharp +* langs: java Traces can be recorded using the [`property: BrowserContext.tracing`] API as follows: @@ -233,30 +233,117 @@ context.tracing().stop(new Tracing.StopOptions() .setPath(Paths.get("trace.zip"))); ``` +This will record the trace and place it into the file named `trace.zip`. + +## Recording a trace +* langs: csharp + +Traces can be recorded using the [`property: BrowserContext.tracing`] API as follows: + + + + ```csharp -using var playwright = await Playwright.CreateAsync(); -var browser = await playwright.Chromium.LaunchAsync(); -await using var context = await browser.NewContextAsync(); +namespace PlaywrightTests; -// Start tracing before creating / navigating a page. -await context.Tracing.StartAsync(new() +[Parallelizable(ParallelScope.Self)] +[TestFixture] +public class Tests : PageTest { - Screenshots = true, - Snapshots = true, - Sources = true -}); + [SetUp] + public async Task Setup() + { + await Context.Tracing.StartAsync(new() + { + Title = TestContext.CurrentContext.Test.ClassName + "." + TestContext.CurrentContext.Test.Name, + Screenshots = true, + Snapshots = true, + Sources = true + }); + } -var page = await context.NewPageAsync(); -await page.GotoAsync("https://playwright.dev"); + [TearDown] + public async Task TearDown() + { + // This will produce e.g.: + // bin/Debug/net8.0/playwright-traces/PlaywrightTests.Tests.Test1.zip + await Context.Tracing.StopAsync(new() + { + Path = Path.Combine( + TestContext.CurrentContext.WorkDirectory, + "playwright-traces", + $"{TestContext.CurrentContext.Test.ClassName}.{TestContext.CurrentContext.Test.Name}.zip" + ) + }); + } -// Stop tracing and export it into a zip archive. -await context.Tracing.StopAsync(new() -{ - Path = "trace.zip" -}); + [Test] + public async Task TestYourOnlineShop() + { + // .. + } +} ``` -This will record the trace and place it into the file named `trace.zip`. + + + +```csharp +using System.Text.RegularExpressions; +using Microsoft.Playwright; +using Microsoft.Playwright.MSTest; + +namespace PlaywrightTestsMSTest; + +[TestClass] +public class UnitTest1 : PageTest +{ + [TestInitialize] + public async Task TestInitialize() + { + await Context.Tracing.StartAsync(new() + { + Title = TestContext.TestName, + Screenshots = true, + Snapshots = true, + Sources = true + }); + } + + [TestCleanup] + public async Task TestCleanup() + { + // This will produce e.g.: + // bin/Debug/net8.0/playwright-traces/PlaywrightTests.UnitTest1.zip + await Context.Tracing.StopAsync(new() + { + Path = Path.Combine( + Environment.CurrentDirectory, + "playwright-traces", + $"{TestContext.FullyQualifiedTestClassName}.zip" + ) + }); + } + + [TestMethod] + public async Task TestYourOnlineShop() + { + // ... + } +} +``` + + + + +This will record the trace and place it into the `bin/Debug/net8.0/playwright-traces/` directory. ## Opening the trace