测试运行器
简介
虽然Playwright for .NET并不绑定特定的测试运行器或测试框架,但根据我们的经验,最简单的入门方式是使用我们为MSTest、NUnit或xUnit提供的基础类。这些类支持在多个浏览器引擎上运行测试,调整启动/上下文选项,并默认提供每个测试的Page/BrowserContext实例。
为了获得更好的性能,Playwright和浏览器实例将在测试之间重复使用。我们建议在每个测试用例中使用新的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
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",
};
}
}
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",
};
}
}
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",
};
}
}
自定义Browser/启动选项
Browser/启动选项可以通过使用运行设置文件或直接通过CLI设置运行设置选项来覆盖。请参见以下示例:
<?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
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>
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>
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 的基础类
- MSTest
- NUnit
- xUnit
在Microsoft.Playwright.NUnit
命名空间中有几个可用的基类:
在Microsoft.Playwright.MSTest
命名空间中,有几个基类可供您使用:
在Microsoft.Playwright.Xunit
命名空间中有几个可供使用的基础类:
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 | 每个测试都会获得一个浏览器实例,并可以创建任意数量的上下文。每个测试需负责清理自己创建的所有上下文。 |
PlaywrightTest | 这会为每个测试提供一个Playwright对象,使测试能够根据需要启动和停止任意数量的浏览器。 |