跳至主要内容

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)

Added in: v1.18 playwrightAssertions.Expect(response)

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

用法

参数

返回


预期(定位器)

Added in: v1.18 playwrightAssertions.Expect(locator)

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

用法

await Expect(locator).ToBeVisibleAsync();

参数

返回


Expect(page)

Added in: v1.18 playwrightAssertions.Expect(page)

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

用法

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

参数

返回