Skip to main content

测试运行器

介绍

🌐 Introduction

虽然 Playwright for .NET 并不依赖于特定的测试运行器或测试框架,但根据我们的经验,入门最简单的方式是使用我们为 MSTest、NUnit、xUnit 或 xUnit v3 提供的基础类。这些类支持在多个浏览器引擎上运行测试,调整启动/上下文选项,并且可以开箱即用地为每个测试获取 Page/BrowserContext 实例。

🌐 While Playwright for .NET isn't tied to a particular test runner or testing framework, in our experience the easiest way of getting started is by using the base classes we provide for MSTest, NUnit, xUnit, or xUnit v3. These classes support running tests on multiple browser engines, adjusting launch/context options and getting a Page/BrowserContext instance per test out of the box.

为了提高性能,Playwright 和浏览器实例将在测试之间重复使用。我们建议在每个测试用例中使用新的 BrowserContext,这样浏览器状态将在测试之间保持隔离。

🌐 Playwright and Browser instances will be reused between tests for better performance. We recommend running each test case in a new BrowserContext, this way browser state will be isolated between the tests.

Playwright 提供了通过 Microsoft.Playwright.MSTest 包使用 MSTest 编写测试的基础类。

🌐 Playwright provides base classes to write tests with MSTest via the Microsoft.Playwright.MSTest package.

查看安装指南以开始使用。

🌐 Check out the installation guide to get started.

并行运行测试

🌐 Running tests in Parallel

默认情况下,MSTest 会并行运行所有类,同时在每个类内部顺序运行测试(ExecutionScope.ClassLevel)。它会根据主机系统的核心数创建相应数量的进程。你可以使用以下 CLI 参数或使用 .runsettings 文件来调整此行为,见下文。在方法级别(ExecutionScope.MethodLevel)并行运行测试是不被支持的。

🌐 By default MSTest will run all classes in parallel, while running tests inside each class sequentially (ExecutionScope.ClassLevel). It will create as many processes as there are cores on the host system. You can adjust this behavior by using the following CLI parameter or using a .runsettings file, see below. Running tests in parallel at the method level (ExecutionScope.MethodLevel) is not supported.

dotnet test --settings:.runsettings -- MSTest.Parallelize.Workers=4

自定义 BrowserContext选项

🌐 Customizing BrowserContext options

要自定义上下文选项,你可以重写从 Microsoft.Playwright.MSTest.PageTestMicrosoft.Playwright.MSTest.ContextTest 派生的测试类中的 ContextOptions 方法。请参见以下示例:

🌐 To customize context options, you can override the ContextOptions method of your test class derived from Microsoft.Playwright.MSTest.PageTest or Microsoft.Playwright.MSTest.ContextTest. See the following example:

using System.Threading.Tasks;
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PlaywrightTests;

[TestClass]
public class ExampleTest : PageTest
{
[TestMethod]
public async Task TestWithCustomContextOptions()
{
// The following Page (and BrowserContext) instance has the custom colorScheme, viewport and baseURL set:
await Page.GotoAsync("/login");
}

public override BrowserNewContextOptions ContextOptions()
{
return new BrowserNewContextOptions()
{
ColorScheme = ColorScheme.Light,
ViewportSize = new()
{
Width = 1920,
Height = 1080
},
BaseURL = "https://github.com",
};
}
}

自定义 [浏览器]/启动选项

🌐 Customizing Browser/launch options

[浏览器]/启动选项可以通过使用运行设置文件或直接通过命令行接口设置运行设置选项来覆盖。请参阅以下示例:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<Playwright>
<BrowserName>chromium</BrowserName>
<LaunchOptions>
<Headless>false</Headless>
<Channel>msedge</Channel>
</LaunchOptions>
</Playwright>
</RunSettings>
dotnet test -- Playwright.BrowserName=chromium Playwright.LaunchOptions.Headless=false Playwright.LaunchOptions.Channel=msedge

使用详细 API 日志

🌐 Using Verbose API Logs

当你通过 DEBUG 环境变量启用详细 API 日志时,你将会在标准错误流中看到这些消息。在 Visual Studio 中,这将显示在 Output 窗口的 Tests 窗格中。对于每个测试,它也会显示在 Test Log 中。

🌐 When you have enabled the verbose API log, via the DEBUG environment variable, you will see the messages in the standard error stream. Within Visual Studio, that will be the Tests pane of the Output window. It will also be displayed in the Test Log for each test.

使用 .runsettings 文件

🌐 Using the .runsettings file

在 Visual Studio 中运行测试时,你可以利用 .runsettings 文件。以下显示了支持值的参考。

🌐 When running tests from Visual Studio, you can take advantage of the .runsettings file. The following shows a reference of the supported values.

例如,要指定工作进程的数量,可以使用 MSTest.Parallelize.Workers。你也可以使用 RunConfiguration.EnvironmentVariables 启用 DEBUG 日志。

🌐 For example, to specify the number of workers, you can use MSTest.Parallelize.Workers. You can also enable DEBUG logs using RunConfiguration.EnvironmentVariables.

<RunSettings>
<!-- MSTest adapter -->
<MSTest>
<Parallelize>
<Workers>4</Workers>
<Scope>ClassLevel</Scope>
</Parallelize>
</MSTest>
<!-- General run configuration -->
<RunConfiguration>
<EnvironmentVariables>
<!-- For debugging selectors, it's recommend to set the following environment variable -->
<DEBUG>pw:api</DEBUG>
</EnvironmentVariables>
</RunConfiguration>
<!-- Playwright -->
<Playwright>
<BrowserName>chromium</BrowserName>
<ExpectTimeout>5000</ExpectTimeout>
<LaunchOptions>
<Headless>false</Headless>
<Channel>msedge</Channel>
</LaunchOptions>
</Playwright>
</RunSettings>

Playwright 的基类

🌐 Base classes for Playwright

Microsoft.Playwright.MSTest 命名空间中有一些可用的基类:

🌐 There are a few base classes available to you in Microsoft.Playwright.MSTest namespace:

TestDescription
PageTestEach test gets a fresh copy of a web Page created in its own unique BrowserContext. Extending this class is the simplest way of writing a fully-functional Playwright test.

Note: You can override the ContextOptions method in each test file to control context options, the ones typically passed into the Browser.NewContextAsync() method. That way you can specify all kinds of emulation options for your test file individually.
ContextTestEach test will get a fresh copy of a BrowserContext. You can create as many pages in this context as you'd like. Using this test is the easiest way to test multi-page scenarios where you need more than one tab.

Note: You can override the ContextOptions method in each test file to control context options, the ones typically passed into the Browser.NewContextAsync() method. That way you can specify all kinds of emulation options for your test file individually.
BrowserTestEach test will get a browser and can create as many contexts as it likes. Each test is responsible for cleaning up all the contexts it created.
PlaywrightTestThis gives each test a Playwright object so that the test could start and stop as many browsers as it likes.