跳到主内容

Playwright断言

Playwright 提供“网页优先”的断言,这些便捷方法创建的断言会等待并重试,直到满足预期条件为止。

请看以下示例

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");

参数

返回值