diff --git a/docs/src/trace-viewer-intro-csharp.md b/docs/src/trace-viewer-intro-csharp.md index c46edad5fc..f84f522011 100644 --- a/docs/src/trace-viewer-intro-csharp.md +++ b/docs/src/trace-viewer-intro-csharp.md @@ -133,110 +133,9 @@ pwsh bin/Debug/net8.0/playwright.ps1 show-trace bin/Debug/net8.0/playwright-trac Setup your tests to record a trace only when the test fails: - - -```csharp -namespace PlaywrightTests; -[Parallelizable(ParallelScope.Self)] -[TestFixture] -public class ExampleTest : 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() - { - var failed = TestContext.CurrentContext.Result.Outcome == NUnit.Framework.Interfaces.ResultState.Error - || TestContext.CurrentContext.Result.Outcome == NUnit.Framework.Interfaces.ResultState.Failure; - - 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 GetStartedLink() - { - // .. - } -} -``` - - - -```csharp -using System.Text.RegularExpressions; -using Microsoft.Playwright; -using Microsoft.Playwright.MSTest; - -namespace PlaywrightTests; - -[TestClass] -public class ExampleTest : PageTest -{ - [TestInitialize] - public async Task TestInitialize() - { - await Context.Tracing.StartAsync(new() - { - Title = $"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}", - Screenshots = true, - Snapshots = true, - Sources = true - }); - } - - [TestCleanup] - public async Task TestCleanup() - { - var failed = new[] { UnitTestOutcome.Failed, UnitTestOutcome.Error, UnitTestOutcome.Timeout, UnitTestOutcome.Aborted }.Contains(TestContext.CurrentTestOutcome); - - await Context.Tracing.StopAsync(new() - { - Path = Path.Combine( - Environment.CurrentDirectory, - "playwright-traces", - $"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}.zip" - ) - }); - } - - [TestMethod] - public async Task GetStartedLink() - { - // ... - } -} -``` - - - - -To learn more check out our detailed guide on [Trace Viewer](/trace-viewer.md). +Check out our detailed guide on [Trace Viewer](/trace-viewer.md) to learn more about the trace viewer and how to setup your tests to record a trace only when the test fails. ## What's next diff --git a/docs/src/trace-viewer.md b/docs/src/trace-viewer.md index 3cc73165cc..a963c2ae1a 100644 --- a/docs/src/trace-viewer.md +++ b/docs/src/trace-viewer.md @@ -388,7 +388,7 @@ public class ExampleTest : PageTest await Context.Tracing.StopAsync(new() { - Path = Path.Combine( + Path = failed ? Path.Combine( TestContext.CurrentContext.WorkDirectory, "playwright-traces", $"{TestContext.CurrentContext.Test.ClassName}.{TestContext.CurrentContext.Test.Name}.zip" @@ -435,7 +435,7 @@ public class ExampleTest : PageTest await Context.Tracing.StopAsync(new() { - Path = Path.Combine( + Path = failed ? Path.Combine( Environment.CurrentDirectory, "playwright-traces", $"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}.zip"