Skip to main content

跟踪查看器

介绍

🌐 Introduction

Playwright 跟踪查看器是一款 GUI 工具,可让你浏览测试中记录的 Playwright 跟踪,这意味着你可以前后移动测试的每个操作,并直观地查看每个操作期间发生的情况。

🌐 Playwright Trace Viewer is a GUI tool that lets you explore recorded Playwright traces of your tests meaning you can go back and forward though each action of your test and visually see what was happening during each action.

你将学习

  • 如何记录痕迹
  • 如何打开跟踪查看器

记录跟踪

🌐 Recording a trace

可以使用 BrowserContext.Tracing API 如下记录跟踪:

🌐 Traces can be recorded using the BrowserContext.Tracing API as follows:

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()
{
await Context.Tracing.StopAsync(new()
{
Path = Path.Combine(
Environment.CurrentDirectory,
"playwright-traces",
$"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}.zip"
)
});
}

[TestMethod]
public async Task GetStartedLink()
{
// ...
}
}

这将为每个测试记录一个压缩文件,例如 PlaywrightTests.ExampleTest.GetStartedLink.zip,并将其放入 bin/Debug/net8.0/playwright-traces/ 目录中。

🌐 This will record a zip file for each test, e.g. PlaywrightTests.ExampleTest.GetStartedLink.zip and place it into the bin/Debug/net8.0/playwright-traces/ directory.

打开跟踪

🌐 Opening the trace

你可以使用 Playwright CLI 打开已保存的 trace,或在浏览器中通过 trace.playwright.dev 打开。请确保添加 trace zip 文件所在的完整路径。打开后,你可以点击每个操作或使用时间轴查看每个操作前后的页面状态。你还可以在测试的每个步骤中检查日志、源代码和网络。trace 查看器会创建一个 DOM 快照,因此你可以完全与其交互,打开开发者工具等。

🌐 You can open the saved trace using the Playwright CLI or in your browser on trace.playwright.dev. Make sure to add the full path to where your trace's zip file is located. Once opened you can click on each action or use the timeline to see the state of the page before and after each action. You can also inspect the log, source and network during each step of the test. The trace viewer creates a DOM snapshot so you can fully interact with it, open devtools etc.

pwsh bin/Debug/net8.0/playwright.ps1 show-trace bin/Debug/net8.0/playwright-traces/PlaywrightTests.ExampleTest.GetStartedLink.zip

playwright trace viewer dotnet

查看我们关于跟踪查看器的详细指南,了解更多关于跟踪查看器的信息,以及如何设置测试,以便仅在测试失败时记录跟踪。

🌐 Check out our detailed guide on Trace Viewer 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