安装
介绍
¥Introduction
Playwright 专为满足端到端测试的需求而创建。Playwright 支持所有现代渲染引擎,包括 Chromium、WebKit 和 Firefox。在 Windows、Linux 和 macOS 上进行测试,本地或持续集成 (CI),无头或使用原生移动模拟进行有头测试。
¥Playwright was created specifically to accommodate the needs of end-to-end testing. Playwright supports all modern rendering engines including Chromium, WebKit, and Firefox. Test on Windows, Linux, and macOS, locally or on CI, headless or headed with native mobile emulation.
你可以选择使用 MSTest、NUnit 或 Playwright 提供的 xUnit 基类 来编写端到端测试。这些类支持在多个浏览器引擎上运行测试,并行执行测试,调整启动/上下文选项,并为每个测试获取一个 Page/BrowserContext 实例。或者,你可以使用 library 手动编写测试基础架构。
¥You can choose to use MSTest, NUnit, or xUnit base classes that Playwright provides to write end-to-end tests. These classes support running tests on multiple browser engines, parallelizing tests, adjusting launch/context options and getting a Page/BrowserContext instance per test out of the box. Alternatively you can use the library to manually write the testing infrastructure.
-
首先创建一个包含
dotnet new
的新项目。这将创建包含UnitTest1.cs
文件的PlaywrightTests
目录:¥Start by creating a new project with
dotnet new
. This will create thePlaywrightTests
directory which includes aUnitTest1.cs
file:
- 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 依赖:
¥Install the necessary Playwright dependencies:
- MSTest
- NUnit
- xUnit
dotnet add package Microsoft.Playwright.NUnit
dotnet add package Microsoft.Playwright.MSTest
dotnet add package Microsoft.Playwright.Xunit
-
构建项目,使
playwright.ps1
可在bin
目录中使用:¥Build the project so the
playwright.ps1
is available inside thebin
directory:
dotnet build
-
安装所需的浏览器。此示例使用
net8.0
,如果你使用的是其他版本的 .NET,则需要调整命令并将net8.0
更改为你的版本。¥Install required browsers. This example uses
net8.0
, if you are using a different version of .NET you will need to adjust the command and changenet8.0
to your version.
pwsh bin/Debug/net8.0/playwright.ps1 install
如果 pwsh
不可用,则必须使用 安装 PowerShell。
¥If pwsh
is not available, you will have to install PowerShell.
添加示例测试
¥Add Example Tests
使用以下代码编辑 UnitTest1.cs
文件,创建一个端到端测试示例:
¥Edit the UnitTest1.cs
file with the code below to create an example end-to-end test:
- MSTest
- NUnit
- xUnit
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.nodejs.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.nodejs.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();
}
}
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.nodejs.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.nodejs.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();
}
}
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.nodejs.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.nodejs.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();
}
}
运行示例测试
¥Running the Example Tests
默认情况下,测试将在 Chromium 上运行。这可以通过 BROWSER
环境变量进行配置,也可以通过调整 启动配置选项 进行配置。测试在无头模式下运行,这意味着运行测试时不会打开浏览器。测试结果和测试日志将显示在终端中。
¥By default tests will be run on Chromium. This can be configured via the BROWSER
environment variable, or by adjusting the launch configuration options. Tests are run in headless mode meaning no browser will open up when running the tests. Results of the tests and test logs will be shown in the terminal.
dotnet test
请参阅我们关于 运行和调试测试 的文档,了解更多关于在有头模式下运行测试、运行多个测试、运行特定配置等信息。
¥See our doc on Running and Debugging Tests to learn more about running tests in headed mode, running multiple tests, running specific configurations etc.
系统要求
¥System requirements
-
Playwright 作为 .NET Standard 2.0 库分发。我们推荐使用.NET 8。
¥Playwright is distributed as a .NET Standard 2.0 library. We recommend .NET 8.
-
Windows 10+、Windows Server 2016+ 或适用于 Linux 的 Windows 子系统 (WSL)。
¥Windows 10+, Windows Server 2016+ or Windows Subsystem for Linux (WSL).
-
macOS 14 Ventura 或更高版本。
¥macOS 14 Ventura, or later.
-
Debian 12、Ubuntu 22.04、Ubuntu 24.04,基于 x86-64 和 arm64 架构。
¥Debian 12, Ubuntu 22.04, Ubuntu 24.04, on x86-64 and arm64 architecture.
下一步是什么
¥What's next