测试运行器
介绍
虽然 Playwright for .NET 不与特定的测试运行器或测试框架绑定,但根据我们的经验,最简单的入门方法是使用我们提供的适用于 MSTest、NUnit 或 xUnit 的基类。这些类开箱即用地支持在多个浏览器引擎上运行测试,调整启动/上下文选项,并为每个测试获取一个 Page/BrowserContext 实例。
Playwright 和 Browser 实例将在测试之间重用,以获得更好的性能。我们建议在新的 BrowserContext 中运行每个测试用例,这样可以隔离测试之间的浏览器状态。
- MSTest
- NUnit
- xUnit
Playwright 通过 Microsoft.Playwright.NUnit
包提供了用于使用 NUnit 编写测试的基类。
Playwright 通过 Microsoft.Playwright.MSTest
包提供了用于使用 MSTest 编写测试的基类。
Playwright 通过 Microsoft.Playwright.Xunit
包提供了用于使用 xUnit 编写测试的基类。
请查阅安装指南以开始使用。
并行运行测试
- MSTest
- NUnit
- xUnit
默认情况下,NUnit 将并行运行所有测试文件,同时顺序运行每个文件内的测试 (ParallelScope.Self
)。它将创建与主机系统核心数量一样多的进程。您可以使用 NUnit.NumberOfTestWorkers 参数调整此行为。仅支持 ParallelScope.Self
。
对于 CPU 密集型测试,我们建议使用与系统核心数除以 2 相同数量的工作进程。对于 IO 密集型测试,您可以使用与核心数相同数量的工作进程。
dotnet test -- NUnit.NumberOfTestWorkers=5
默认情况下,MSTest 将并行运行所有类,同时顺序运行每个类内的测试 (ExecutionScope.ClassLevel
)。它将创建与主机系统核心数量一样多的进程。您可以使用以下 CLI 参数或使用 .runsettings
文件(见下文)调整此行为。不支持在方法级别并行运行测试 (ExecutionScope.MethodLevel
)。
dotnet test --settings:.runsettings -- MSTest.Parallelize.Workers=4
默认情况下,xUnit 将并行运行所有类,同时顺序运行每个类内的测试。默认情况下,它将创建与系统核心数量一样多的进程。您可以使用以下 CLI 参数或使用 .runsettings
文件(见下文)调整此行为。
dotnet test -- xUnit.MaxParallelThreads=5
我们推荐 xUnit 2.8+,它默认使用 conservative
并行算法。
自定义 BrowserContext 选项
- MSTest
- NUnit
- xUnit
要自定义上下文选项,您可以覆盖派生自 Microsoft.Playwright.MSTest.PageTest
或 Microsoft.Playwright.MSTest.ContextTest
的测试类的 ContextOptions
方法。请参阅以下示例:
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
方法。请参阅以下示例:
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
方法。请参阅以下示例:
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",
};
}
}
自定义 Browser/启动选项
可以使用运行设置文件或通过 CLI 直接设置运行设置选项来覆盖 Browser/启动选项。请参阅以下示例:
<?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 日志
当您通过 DEBUG
环境变量启用详细 API 日志时,您将在标准错误流中看到消息。在 Visual Studio 中,这将是 Output
窗口的 Tests
面板。它也将显示在每个测试的 Test Log
中。
使用 .runsettings 文件
在 Visual Studio 中运行测试时,可以利用 .runsettings
文件。以下显示了支持值的参考。
- MSTest
- NUnit
- xUnit
例如,要指定工作进程数量,您可以使用 NUnit.NumberOfTestWorkers
,或者要启用 DEBUG
日志,可以使用 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
日志。
<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
日志。
<?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 的基类
- MSTest
- NUnit
- xUnit
Microsoft.Playwright.NUnit
命名空间中有一些可用的基类:
Microsoft.Playwright.MSTest
命名空间中有一些可用的基类:
Microsoft.Playwright.Xunit
命名空间中有一些可用的基类:
测试 | 描述 |
---|---|
PageTest | 每个测试都会在自己独特的 BrowserContext 中获得一个全新的 Web Page 实例。扩展此类是编写功能齐全的 Playwright 测试的最简单方法。 注意:您可以在每个测试文件中覆盖 ContextOptions 方法来控制上下文选项,这些选项通常传递给 Browser.NewContextAsync() 方法。这样您就可以为每个测试文件单独指定各种模拟选项。 |
ContextTest | 每个测试都会获得一个全新的 BrowserContext 实例。您可以在此上下文中创建任意数量的页面。使用此类测试是测试需要多个标签页的多页面场景的最简单方法。 注意:您可以在每个测试文件中覆盖 ContextOptions 方法来控制上下文选项,这些选项通常传递给 Browser.NewContextAsync() 方法。这样您就可以为每个测试文件单独指定各种模拟选项。 |
BrowserTest | 每个测试将获得一个浏览器实例,并可以根据需要创建任意数量的上下文。每个测试负责清理其创建的所有上下文。 |
PlaywrightTest | 这为每个测试提供了一个 Playwright 对象,以便测试可以根据需要启动和停止任意数量的浏览器。 |