安装
简介
Playwright 专为满足端到端测试的需求而创建。Playwright 支持所有现代渲染引擎,包括 Chromium、WebKit 和 Firefox。在 Windows、Linux 和 macOS 上进行测试,本地或在 CI 上,无头或有头模式并带有原生移动设备仿真。
您可以选择使用 Playwright 提供的 MSTest、NUnit 或 xUnit 基类来编写端到端测试。这些类支持在多个浏览器引擎上运行测试、并行化测试、调整启动/上下文选项以及开箱即用地为每个测试获取一个 Page/BrowserContext 实例。或者,您可以使用 库 手动编写测试基础结构。
- 通过使用
dotnet new创建一个新项目开始。这将创建PlaywrightTests目录,其中包含一个UnitTest1.cs文件
- MSTest
- NUnit
- xUnit
- xUnit v3
dotnet new nunit -n PlaywrightTests
cd PlaywrightTests
dotnet new mstest -n PlaywrightTests
cd PlaywrightTests
dotnet new xunit -n PlaywrightTests
cd PlaywrightTests
dotnet new xunit3 -n PlaywrightTests
cd PlaywrightTests
- 安装必要的 Playwright 依赖项
- MSTest
- NUnit
- xUnit
- xUnit v3
dotnet add package Microsoft.Playwright.NUnit
dotnet add package Microsoft.Playwright.MSTest
dotnet add package Microsoft.Playwright.Xunit
dotnet add package Microsoft.Playwright.Xunit.v3
- 构建项目,以便
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
- xUnit v3
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();
}
}
UnitTest1.cs
using System.Text.RegularExpressions;
using Microsoft.Playwright;
using Microsoft.Playwright.Xunit.v3;
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 11+、Windows Server 2019+ 或 Windows Subsystem for Linux (WSL)。
- macOS 14 Ventura 或更高版本。
- Debian 12、Debian 13、Ubuntu 22.04、Ubuntu 24.04,在 x86-64 和 arm64 架构上。