Skip to main content

安装

介绍

🌐 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.

你可以选择使用 Playwright 提供的 MSTest、NUnit 或 xUnit 基类 来编写端到端测试。这些类支持在多个浏览器引擎上运行测试、并行化测试、调整启动/上下文选项,并开箱即用地为每个测试获取一个 Page/BrowserContext 实例。或者,你也可以使用 来手动编写测试基础设施。

🌐 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.

  1. 首先使用 dotnet new 创建一个新项目。这将创建包含 UnitTest1.cs 文件的 PlaywrightTests 目录:
dotnet new mstest -n PlaywrightTests
cd PlaywrightTests
  1. 安装必要的 Playwright 依赖:
dotnet add package Microsoft.Playwright.MSTest
  1. 构建项目,使 playwright.ps1 可在 bin 目录中使用:
dotnet build
  1. 安装所需的浏览器。本示例使用 net8.0,如果你使用的是不同版本的 .NET,则需要调整命令并将 net8.0 改为你的版本。
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:

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.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();
}
}

运行示例测试

🌐 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。
  • 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 架构。

下一步是什么

🌐 What's next