跳转到主要内容

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

参数

返回