测试运行器
介绍
¥Introduction
虽然 Playwright for .NET 并不依赖于特定的测试运行器或测试框架,但根据我们的经验,最简单的入门方法是使用我们为 MSTest、NUnit 或 xUnit 提供的基类。这些类支持在多个浏览器引擎上运行测试,调整启动/上下文选项,并为每个测试获取一个 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, or xUnit. 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.
- MSTest
- NUnit
- xUnit
Playwright 提供了通过 Microsoft.Playwright.NUnit
包使用 NUnit 编写测试的基类。
¥Playwright provides base classes to write tests with NUnit via the Microsoft.Playwright.NUnit
package.
Playwright 提供了通过 Microsoft.Playwright.MSTest
包使用 MSTest 编写测试的基类。
¥Playwright provides base classes to write tests with MSTest via the Microsoft.Playwright.MSTest
package.
Playwright 提供了通过 Microsoft.Playwright.Xunit
包使用 xUnit 编写测试的基类。
¥Playwright provides base classes to write tests with xUnit via the Microsoft.Playwright.Xunit
package.
查看 安装指南 开始使用。
¥Check out the installation guide to get started.
并行运行测试
¥Running tests in Parallel
- MSTest
- NUnit
- xUnit
默认情况下,NUnit 将并行运行所有测试文件,同时在每个文件中顺序运行测试(ParallelScope.Self
)。它将创建与主机系统核心数量相同的进程。你可以使用 NUnit.NumberOfTestWorkers 参数调整此行为。仅支持 ParallelScope.Self
。
¥By default NUnit will run all test files in parallel, while running tests inside each file sequentially (ParallelScope.Self
). It will create as many processes as there are cores on the host system. You can adjust this behavior using the NUnit.NumberOfTestWorkers parameter. Only ParallelScope.Self
is supported.
对于 CPU 密集型测试,我们建议使用的 Worker 数量等于系统核心数除以 2。对于 IO 密集型测试,你可以使用与核心数相同的 Worker 数量。
¥For CPU-bound tests, we recommend using as many workers as there are cores on your system, divided by 2. For IO-bound tests you can use as many workers as you have cores.
dotnet test -- NUnit.NumberOfTestWorkers=5
默认情况下,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
默认情况下,xUnit 将并行运行所有类,同时在每个类中顺序运行测试。默认情况下,它将创建与系统核心数量相同的进程。你可以使用以下 CLI 参数或 .runsettings
文件调整此行为,详见下文。
¥By default xUnit will run all classes in parallel, while running tests inside each class sequentially. It will create by default as many processes as there are cores on the system. You can adjust this behavior by using the following CLI parameter or using a .runsettings
file, see below.
dotnet test -- xUnit.MaxParallelThreads=5
我们推荐使用 xUnit 2.8+,它默认使用 conservative
并行算法。
¥We recommend xUnit 2.8+ which uses the conservative
parallelism algorithm by default.
自定义 [BrowserContext]选项
¥Customizing BrowserContext options
- MSTest
- NUnit
- xUnit
要自定义上下文选项,你可以重写从 Microsoft.Playwright.MSTest.PageTest
或 Microsoft.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 Microsoft.Playwright.NUnit;
namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class MyTest : PageTest
{
[Test]
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",
};
}
}
要自定义上下文选项,你可以重写从 Microsoft.Playwright.MSTest.PageTest
或 Microsoft.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",
};
}
}
要自定义上下文选项,你可以重写从 Microsoft.Playwright.Xunit.PageTest
或 Microsoft.Playwright.Xunit.ContextTest
派生的测试类的 ContextOptions
方法。请参见以下示例:
¥To customize context options, you can override the ContextOptions
method of your test class derived from Microsoft.Playwright.Xunit.PageTest
or Microsoft.Playwright.Xunit.ContextTest
. See the following example:
using Microsoft.Playwright;
using Microsoft.Playwright.Xunit;
namespace PlaywrightTests;
public class UnitTest1 : PageTest
{
[Fact]
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
可以使用运行设置文件或直接通过 CLI 设置运行设置选项来覆盖 Browser/启动选项。请参见以下示例:
¥Browser/launch options can be overridden either using a run settings file or by setting the run settings options directly via the CLI. See the following example:
<?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
- NUnit
- xUnit
例如,要指定 Worker 数量,你可以使用 NUnit.NumberOfTestWorkers
;要启用 DEBUG
日志,请使用 RunConfiguration.EnvironmentVariables
。
¥For example, to specify the number of workers you can use NUnit.NumberOfTestWorkers
or to enable DEBUG
logs RunConfiguration.EnvironmentVariables
.
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- NUnit adapter -->
<NUnit>
<NumberOfTestWorkers>24</NumberOfTestWorkers>
</NUnit>
<!-- 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>
例如,要指定 Worker 数量,你可以使用 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>
例如,要指定 Worker 数量,你可以使用 xUnit.MaxParallelThreads
。你还可以使用 RunConfiguration.EnvironmentVariables
启用 DEBUG
日志。
¥For example, to specify the number of workers, you can use xUnit.MaxParallelThreads
. You can also enable DEBUG
logs using RunConfiguration.EnvironmentVariables
.
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- See https://xunit.net/docs/runsettings -->
<xUnit>
<MaxParallelThreads>1</MaxParallelThreads>
</xUnit>
<!-- 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
- MSTest
- NUnit
- xUnit
Microsoft.Playwright.NUnit
命名空间中提供了一些可用的基类:
¥There are a few base classes available to you in Microsoft.Playwright.NUnit
namespace:
Microsoft.Playwright.MSTest
命名空间中提供了一些可用的基类:
¥There are a few base classes available to you in Microsoft.Playwright.MSTest
namespace:
Microsoft.Playwright.Xunit
命名空间中提供了一些可用的基类:
¥There are a few base classes available to you in Microsoft.Playwright.Xunit
namespace:
测试 | 描述 |
---|---|
PageTest | 每个测试都会在其唯一的 [BrowserContext] 中创建一个全新的网页 [Page] 副本。扩展此类是编写功能齐全的 Playwright 测试的最简单方法。 注意:你可以覆盖每个测试文件中的 ContextOptions 方法来控制上下文选项,这些选项通常会传递给 Browser.NewContextAsync() 方法。这样,你可以为测试文件单独指定各种模拟选项。 |
ContextTest | 每个测试都会获得一个 [BrowserContext] 的全新副本。你可以根据需要在此上下文中创建任意数量的页面。使用此测试是测试需要多个选项卡的多页面场景的最简单方法。 注意:你可以覆盖每个测试文件中的 ContextOptions 方法来控制上下文选项,这些选项通常会传递给 Browser.NewContextAsync() 方法。这样,你可以为测试文件单独指定各种模拟选项。 |
BrowserTest | 每个测试都会获取一个浏览器,并且可以创建任意数量的上下文。每个测试负责清理其创建的所有上下文。 |
PlaywrightTest | 这会为每个测试提供一个 Playwright 对象,以便测试可以启动和停止任意数量的浏览器。 |