安装
简介
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
- 构建项目,以便在
bin目录下可以使用playwright.ps1。
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 环境变量或通过调整启动配置选项进行配置。测试以无头模式(headless mode)运行,这意味着运行测试时不会打开浏览器界面。测试结果和测试日志将显示在终端中。
dotnet test
请查看我们关于运行和调试测试的文档,了解更多关于在有头模式下运行测试、运行多个测试、运行特定配置等信息。
系统要求
- Playwright 以 .NET Standard 2.0 库的形式分发。我们推荐使用 .NET 8。
- Windows 11+、Windows Server 2019+ 或 Windows Subsystem for Linux (WSL)。
- macOS 14 Sonoma 或更高版本。
- Debian 12、Debian 13、Ubuntu 22.04、Ubuntu 24.04,在 x86-64 和 arm64 架构上。