Skip to main content

PlaywrightAssertions

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

🌐 Playwright gives you Web-First Assertions with convenience methods for creating assertions that will wait and retry until the expected condition is met.

考虑以下示例:

🌐 Consider the following example:

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" 文本。它会反复重新获取节点并进行检查,直到满足条件或达到超时时间。你可以将此超时作为一个选项传入。

🌐 Playwright will be re-testing the node with the selector .status until fetched Node has the "Submitted" text. It will be re-fetching the node and checking it over and over, until the condition is met or until the timeout is reached. You can pass this timeout as an option.

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

🌐 By default, the timeout for assertions is set to 5 seconds.


方法

🌐 Methods

Expect(response)

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

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

🌐 Creates a APIResponseAssertions object for the given APIResponse.

用法

参数

返回


Expect(locator)

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

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

🌐 Creates a LocatorAssertions object for the given Locator.

用法

await Expect(locator).ToBeVisibleAsync();

参数

返回


Expect(page)

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

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

🌐 Creates a PageAssertions object for the given Page.

用法

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

参数

  • page Page#

    [页面] 对象用于断言。

返回