跳到主要内容

PlaywrightAssertions

Playwright 为您提供了以 Web 为优先的断言功能,以及用于创建断言的便捷方法,这些断言将等待并重试,直到满足预期条件。

考虑以下示例:

using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;

namespace PlaywrightTests;

[TestClass]
public class ExampleTests : PageTest
{
[TestMethod]
public async Task StatusBecomesSubmitted()
{
await Page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).ClickAsync();
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
}

Playwright 将反复测试选择器为 .status 的节点,直到获取到的节点包含文本 "Submitted"。它将反复重新获取该节点并进行检查,直到满足条件或达到超时时间。您可以将此超时时间作为一个选项传入。

默认情况下,断言的超时时间设置为 5 秒。


方法

Expect(response)

新增于:v1.18 playwrightAssertions.Expect(response)

为给定的 APIResponse 创建一个 APIResponseAssertions 对象。

用法

参数

返回值


Expect(locator)

新增于:v1.18 playwrightAssertions.Expect(locator)

为给定的 Locator 创建一个 LocatorAssertions 对象。

用法

await Expect(locator).ToBeVisibleAsync();

参数

返回值


Expect(page)

新增于:v1.18 playwrightAssertions.Expect(page)

为给定的 Page 创建一个 PageAssertions 对象。

用法

await Expect(Page).ToHaveTitleAsync("News");

参数

返回值