安装
简介
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 xunit -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 10+、Windows Server 2016+ 或适用于 Linux 的 Windows 子系统 (WSL)。
- macOS 14 Ventura 或更高版本。
- Debian 12、Debian 13、Ubuntu 22.04、Ubuntu 24.04,在 x86-64 和 arm64 架构上。