跳至主要内容

测试运行器

简介

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

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

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

查看安装指南开始使用。

并行运行测试

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

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

自定义BrowserContext选项

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",
};
}
}

自定义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文件。以下展示了支持值的参考。

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 的基础类

Microsoft.Playwright.MSTest命名空间中,有几个基类可供您使用:

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.
BrowserTest每个测试都会获得一个浏览器实例,并可以创建任意数量的上下文。每个测试需负责清理自己创建的所有上下文。
PlaywrightTest这会为每个测试提供一个Playwright对象,使测试能够根据需要启动和停止任意数量的浏览器。