Skip to main content

编写测试

介绍

¥Introduction

Playwright 测试很简单,他们

¥Playwright tests are simple, they

  • 执行操作,并且

    ¥perform actions, and

  • 断言状态与预期相反。

    ¥assert the state against expectations.

在执行操作之前不需要等待任何事情:Playwright 在执行每个动作之前会自动等待广泛的 actionability 检查通过。

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

你将学习

¥You will learn

第一次测试

¥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 为此使用 定位器 API。定位器代表了一种随时在页面上查找元素的方法,了解有关可用定位器 不同种类 的更多信息。Playwright 将在执行操作之前等待元素变为 actionable,因此无需等待它变得可用。

¥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 动作列表。请注意,还有更多内容,因此请务必查看 定位器 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.

行动描述
Locator.CheckAsync()检查输入复选框
Locator.ClickAsync()单击该元素
Locator.UncheckAsync()取消选中输入复选框
Locator.HoverAsync()将鼠标悬停在元素上
Locator.FillAsync()填写表单字段,输入文本
Locator.FocusAsync()聚焦元素
Locator.PressAsync()按单个键
Locator.SetInputFilesAsync()选择要上传的文件
Locator.SelectOptionAsync()在下拉菜单中选择选项

断言

¥Assertions

Playwright 提供了一个名为 期望 的异步函数来断言并等待预期条件满足。

¥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 测试框架基类将通过提供单独的 Page 实例来隔离每个测试。由于浏览器上下文的存在,测试之间的页面是隔离的。浏览器上下文相当于一个全新的浏览器配置文件,每个测试都会获得一个全新的环境,即使在单个浏览器中运行多个测试也是如此。

¥The Playwright NUnit and MSTest 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