Skip to main content

编写测试

介绍

🌐 Introduction

Playwright 测试很简单,他们

🌐 Playwright tests are simple, they

  • 执行操作,并
  • 在预期中断言状态

在执行操作之前无需等待任何事情:Playwright 会自动等待各种 可操作性 检查通过,然后再执行每个操作。

🌐 There is no need to wait for anything prior to performing an action: Playwright automatically waits for the wide range of actionability checks to pass prior to performing each action.

在进行检查时也不需要处理竞争条件——Playwright 的断言设计的方式是,它们描述了最终需要满足的预期。

🌐 There is also no need to deal with the race conditions when performing the checks - Playwright assertions are designed in a way that they describe the expectations that need to be eventually met.

就是这样!这些设计选择让 Playwright 用户可以完全不用担心测试中的不稳定超时和竞态检查问题。

🌐 That's it! These design choices allow Playwright users to forget about flaky timeouts and racy checks in their tests altogether.

你将学习

第一次测试

🌐 First test

看一下下面的示例,了解如何编写测试。

🌐 Take a look at the following example to see how to write a test.

UnitTest1.cs
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;

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

行动

🌐 Actions

🌐 Navigation

大多数测试将通过导航页面到一个 URL 开始。之后,测试将能够与页面元素进行交互。

🌐 Most of the tests will start by navigating the page to a URL. After that, the test will be able to interact with the page elements.

await Page.GotoAsync("https://playwright.nodejs.cn");

Playwright 会在页面达到加载状态后再继续操作。了解有关 Page.GotoAsync() 的更多选项。

🌐 Playwright will wait for the page to reach the load state prior to moving forward. Learn more about the Page.GotoAsync() options.

交互

🌐 Interactions

执行操作首先需要定位元素。Playwright 使用 Locators API 来实现这一点。定位器是一种可以在任何时间找到页面上元素的方法,了解更多关于可用的 不同类型 定位器的信息。Playwright 会在执行操作之前等待元素变为 可操作 状态,因此无需等待元素变得可用。

🌐 Performing actions starts with locating the elements. Playwright uses Locators API for that. Locators represent a way to find element(s) on the page at any moment, learn more about the different types of locators available. Playwright will wait for the element to be actionable prior to performing the action, so there is no need to wait for it to become available.

// Create a locator.
var getStarted = Page.GetByRole(AriaRole.Link, new() { Name = "Get started" });

// Click it.
await getStarted.ClickAsync();

大多数情况下,它会写成一行:

🌐 In most cases, it'll be written in one line:

await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();

基本动作

🌐 Basic actions

这是最受欢迎的 Playwright 操作列表。请注意,还有更多操作可用,因此请务必查看 Locator API 部分以了解更多信息。

🌐 This is the list of the most popular Playwright actions. Note that there are many more, so make sure to check the Locator API section to learn more about them.

行动描述
[定位器.CheckAsync()](/api/class-locator.mdx#locator-check)勾选输入复选框
[定位器.点击异步()](/api/class-locator.mdx#locator-click)点击元素
[定位器。UncheckAsync()](/api/class-locator.mdx#locator-uncheck)取消勾选输入复选框
[定位器.HoverAsync()](/api/class-locator.mdx#locator-hover)将鼠标悬停在元素上
[Locator.FillAsync()](/api/class-locator.mdx#locator-fill)填写表单栏,输入文本
[定位器.焦点异步()](/api/class-locator.mdx#locator-focus)聚焦元素
[定位器.PressAsync()](/api/class-locator.mdx#locator-press)按单键
[Locator.SetInputFilesAsync()](/api/class-locator.mdx#locator-set-input-files)选择上传文件
[Locator.SelectOptionAsync()](/api/class-locator.mdx#locator-select-option)在下拉菜单中选择选项

断言

🌐 Assertions

Playwright 提供了一个名为 Expect 的异步函数,用于断言并等待直到满足预期条件。

🌐 Playwright provides an async function called Expect to assert and wait until the expected condition is met.

await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));

这是最流行的异步断言列表。请注意,还有 更多 可供熟悉:

🌐 Here is the list of the most popular async assertions. Note that there are many more to get familiar with:

断言描述
Expect(Locator).ToBeCheckedAsync()复选框已选中
Expect(Locator).ToBeEnabledAsync()控件已启用
Expect(Locator).ToBeVisibleAsync()元素可见
Expect(Locator).ToContainTextAsync()元素包含文本
Expect(Locator).ToHaveAttributeAsync()元素拥有属性
Expect(Locator).ToHaveCountAsync()元素列表具有指定长度
Expect(Locator).ToHaveTextAsync()元素匹配文本
Expect(Locator).ToHaveValueAsync()输入元素具有值
Expect(Page).ToHaveTitleAsync()页面具有标题
Expect(Page).ToHaveURLAsync()页面具有 URL

测试隔离

🌐 Test Isolation

Playwright NUnit、MSTest、xUnit 和 xUnit v3 测试框架基类将通过提供一个单独的 Page 实例来隔离每个测试。页面在测试之间是隔离的,这是由于浏览器上下文的作用,它相当于一个全新的浏览器配置文件,每个测试都能获得一个新的环境,即使在单个浏览器中运行多个测试时也是如此。

🌐 The Playwright NUnit, MSTest, xUnit, and xUnit v3 test framework base classes will isolate each test from each other by providing a separate Page instance. Pages are isolated between tests due to the Browser Context, which is equivalent to a brand new browser profile, where every test gets a fresh environment, even when multiple tests run in a single Browser.

UnitTest1.cs
using System.Threading.Tasks;
using Microsoft.Playwright.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PlaywrightTests;

[TestClass]
public class ExampleTest : PageTest
{
[TestMethod]
public async Task BasicTest()
{
await Page.GotoAsync("https://playwright.nodejs.cn");
}
}

使用测试钩子

🌐 Using Test Hooks

你可以使用 TestInitialize/TestCleanup 来准备和清理你的测试环境:

🌐 You can use TestInitialize/TestCleanup to prepare and clean up your test environment:

UnitTest1.cs
using System.Threading.Tasks;
using Microsoft.Playwright.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PlaywrightTests;

[TestClass]
public class ExampleTest : PageTest
{
[TestMethod]
public async Task MainNavigation()
{
// Assertions use the expect API.
await Expect(Page).ToHaveURLAsync("https://playwright.nodejs.cn/");
}

[TestInitialize]
public async Task TestInitialize()
{
await Page.GotoAsync("https://playwright.nodejs.cn");
}
}

下一步是什么

🌐 What's Next