测试运行器
介绍
🌐 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.
- MSTest
- NUnit
- xUnit
- xUnit v3
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.
Playwright 提供了用于通过 Microsoft.Playwright.Xunit.v3 包使用 xUnit v3 编写测试的基类。
🌐 Playwright provides base classes to write tests with xUnit v3 via the Microsoft.Playwright.Xunit.v3 package.
查看安装指南以开始使用。
🌐 Check out the installation guide to get started.
并行运行测试
🌐 Running tests in Parallel
- MSTest
- NUnit
- xUnit
- xUnit v3
默认情况下,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 为瓶颈的测试,我们建议使用的工作进程数量为系统核心数量的一半。对于以 I/O 为瓶颈的测试,你可以使用与核心数量相同的工作进程数量。
🌐 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 并行算法。
默认情况下,xUnit v3 会并行运行所有类,同时在每个类内部顺序执行测试。默认情况下,它会创建与系统核心数量相同的进程。你可以通过使用以下 CLI 参数或使用 .runsettings 文件来调整此行为,见下文。
🌐 By default xUnit v3 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 v3 默认使用 conservative 并行算法。
自定义 BrowserContext选项
🌐 Customizing BrowserContext options
- MSTest
- NUnit
- xUnit
- xUnit v3
要自定义上下文选项,你可以重写从 Microsoft.Playwright.NUnit.PageTest 或 Microsoft.Playwright.NUnit.ContextTest 派生的测试类中的 ContextOptions 方法。请参见以下示例:
🌐 To customize context options, you can override the ContextOptions method of your test class derived from Microsoft.Playwright.NUnit.PageTest or Microsoft.Playwright.NUnit.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",
};
}
}
要自定义上下文选项,你可以重写从 Microsoft.Playwright.Xunit.v3.PageTest 或 Microsoft.Playwright.Xunit.v3.ContextTest 派生的测试类中的 ContextOptions 方法。请参见以下示例:
🌐 To customize context options, you can override the ContextOptions method of your test class derived from Microsoft.Playwright.Xunit.v3.PageTest or Microsoft.Playwright.Xunit.v3.ContextTest. See the following example:
using Microsoft.Playwright;
using Microsoft.Playwright.Xunit.v3;
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
[浏览器]/启动选项可以通过使用运行设置文件或直接通过命令行接口设置运行设置选项来覆盖。请参阅以下示例:
<?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
- xUnit v3
例如,要指定工作进程的数量可以使用 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>
例如,要指定工作进程的数量,可以使用 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>
例如,要指定工作进程的数量,可以使用 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>
例如,要指定工作进程的数量,可以使用 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
- xUnit v3
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:
Microsoft.Playwright.Xunit.v3 命名空间中有一些可用的基类:
🌐 There are a few base classes available to you in Microsoft.Playwright.Xunit.v3 namespace:
| Test | Description |
|---|---|
| PageTest | Each 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. |
| ContextTest | Each 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. |
| BrowserTest | Each 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. |
| PlaywrightTest | This gives each test a Playwright object so that the test could start and stop as many browsers as it likes. |