Skip to main content

入门 - 库

介绍

🌐 Introduction

Playwright 可以与 MSTest、NUnit、xUnit 或 xUnit v3 基类 一起使用,也可以作为 Playwright 库使用(本指南)。如果你正在开发一个使用 Playwright 功能的应用,或者你正在将 Playwright 与其他测试运行器一起使用,请继续阅读。

🌐 Playwright can either be used with the MSTest, NUnit, xUnit, or xUnit v3 base classes or as a Playwright Library (this guide). If you are working on an application that utilizes Playwright capabilities or you are using Playwright with another test runner, read on.

用法

🌐 Usage

创建一个控制台项目并添加 Playwright 依赖。

🌐 Create a console project and add the Playwright dependency.

# Create project
dotnet new console -n PlaywrightDemo
cd PlaywrightDemo

# Add project dependency
dotnet add package Microsoft.Playwright
# Build the project
dotnet build
# Install required browsers - replace netX with actual output folder name, e.g. net8.0.
pwsh bin/Debug/netX/playwright.ps1 install

# If the pwsh command does not work (throws TypeNotFound), make sure to use an up-to-date version of PowerShell.
dotnet tool update --global PowerShell

创建一个 Program.cs,它将导航到 https://playwright.nodejs.cn/dotnet 并在 Chromium 中截图。

🌐 Create a Program.cs that will navigate to https://playwright.nodejs.cn/dotnet and take a screenshot in Chromium.

using Microsoft.Playwright;

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
await page.GotoAsync("https://playwright.nodejs.cn/dotnet");
await page.ScreenshotAsync(new()
{
Path = "screenshot.png"
});

现在运行它。

🌐 Now run it.

dotnet run

默认情况下,Playwright 以无头模式运行浏览器。要查看浏览器界面,请将 Headless 选项设置为 false。你还可以使用 SlowMo 来减慢执行速度。更多内容请参见调试工具的 章节

🌐 By default, Playwright runs the browsers in headless mode. To see the browser UI, set Headless option to false. You can also use SlowMo to slow down execution. Learn more in the debugging tools section.

await using var browser = await playwright.Firefox.LaunchAsync(new()
{
Headless = false,
SlowMo = 50,
});

使用断言

🌐 Using Assertions

当你使用自己的测试框架时,可以通过以下方式利用 Playwright 的以网页为先的断言。这些断言会自动重试,直到条件满足,例如某个元素具有特定的文本,或者达到超时时间:

🌐 You can do the following to leverage Playwright's web-first assertions when you are using your own test framework. These will automatically retry until the condition is met, e.g. an element has a certain text or the timeout is reached:

using Microsoft.Playwright;
using static Microsoft.Playwright.Assertions;

// Change the default 5 seconds timeout if you'd like.
SetDefaultExpectTimeout(10_000);

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
await page.GotoAsync("https://playwright.nodejs.cn/dotnet");
await Expect(page.GetByRole(AriaRole.Link, new() { Name = "Get started" })).ToBeVisibleAsync();

不同平台的驱动程序包

🌐 Bundle drivers for different platforms

默认情况下,Playwright 仅打包用于 .NET 发布目标运行时的驱动程序。如果你想为其他平台打包,你可以在项目文件中使用 allnonelinuxwinosx 来覆盖此行为。

🌐 Playwright by default does bundle only the driver for the .NET publish target runtime. If you want to bundle for additional platforms, you can override this behavior by using either all, none or linux, win, osx in your project file.

<PropertyGroup>
<PlaywrightPlatform>all</PlaywrightPlatform>
</PropertyGroup>

or:

<PropertyGroup>
<PlaywrightPlatform>osx;linux</PlaywrightPlatform>
</PropertyGroup>