安装
简介
Playwright 专门为满足端到端测试的需求而创建。Playwright 支持所有现代渲染引擎,包括 Chromium、WebKit 和 Firefox。在 Windows、Linux 和 macOS 上进行测试,本地或 CI,无头或有头模式,并具有原生移动设备模拟。
您可以选择使用 Playwright 提供的 MSTest、NUnit 或 xUnit 基类来编写端到端测试。这些类支持在多个浏览器引擎上运行测试、并行化测试、调整启动/上下文选项,并开箱即用地为每个测试获取 Page/BrowserContext 实例。或者,您可以使用库手动编写测试基础设施。
- 首先使用
dotnet new
创建一个新项目。这将创建PlaywrightTests
目录,其中包含一个UnitTest1.cs
文件
- MSTest
- NUnit
- xUnit
dotnet new nunit -n PlaywrightTests
cd PlaywrightTests
dotnet new mstest -n PlaywrightTests
cd PlaywrightTests
dotnet new xunit -n PlaywrightTests
cd PlaywrightTests
- 安装必要的 Playwright 依赖项
- MSTest
- NUnit
- xUnit
dotnet add package Microsoft.Playwright.NUnit
dotnet add package Microsoft.Playwright.MSTest
dotnet add package Microsoft.Playwright.Xunit
- 构建项目,以便
playwright.ps1
在bin
目录中可用
dotnet build
- 安装所需的浏览器。此示例使用
net8.0
,如果您使用的是不同版本的 .NET,则需要调整命令并将net8.0
更改为您的版本。
pwsh bin/Debug/net8.0/playwright.ps1 install
如果 pwsh
不可用,您将需要安装 PowerShell。
添加示例测试
使用以下代码编辑 UnitTest1.cs
文件以创建一个示例端到端测试
- MSTest
- NUnit
- xUnit
UnitTest1.cs
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class ExampleTest : PageTest
{
[Test]
public async Task HasTitle()
{
await Page.GotoAsync("https://playwright.net.cn");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[Test]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.net.cn");
// Click the get started link.
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
// Expects page to have a heading with the name of Installation.
await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Installation" })).ToBeVisibleAsync();
}
}
UnitTest1.cs
using System.Text.RegularExpressions;
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;
namespace PlaywrightTests;
[TestClass]
public class ExampleTest : PageTest
{
[TestMethod]
public async Task HasTitle()
{
await Page.GotoAsync("https://playwright.net.cn");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[TestMethod]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.net.cn");
// Click the get started link.
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
// Expects page to have a heading with the name of Installation.
await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Installation" })).ToBeVisibleAsync();
}
}
UnitTest1.cs
using System.Text.RegularExpressions;
using Microsoft.Playwright;
using Microsoft.Playwright.Xunit;
namespace PlaywrightTests;
public class UnitTest1: PageTest
{
[Fact]
public async Task HasTitle()
{
await Page.GotoAsync("https://playwright.net.cn");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[Fact]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.net.cn");
// Click the get started link.
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
// Expects page to have a heading with the name of Installation.
await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Installation" })).ToBeVisibleAsync();
}
}
运行示例测试
默认情况下,测试将在 Chromium 上运行。这可以通过 BROWSER
环境变量配置,或通过调整启动配置选项进行配置。测试以无头模式运行,这意味着运行测试时不会打开浏览器。测试结果和测试日志将显示在终端中。
dotnet test
请参阅我们关于运行和调试测试的文档,以了解有关在有头模式下运行测试、运行多个测试、运行特定配置等的更多信息。
系统要求
- Playwright 作为 .NET Standard 2.0 库分发。我们建议使用 .NET 8。
- Windows 10+、Windows Server 2016+ 或适用于 Linux 的 Windows 子系统 (WSL)。
- macOS 13 Ventura 或更高版本。
- Debian 12、Ubuntu 22.04、Ubuntu 24.04,在 x86-64 和 arm64 架构上。