安装
介绍
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 14 Ventura 或更高版本。
- Debian 12、Ubuntu 22.04、Ubuntu 24.04,支持 x86-64 和 arm64 架构。