跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

PlaywrightAssertions ​

Added in: v1.17

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:

csharp
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 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.

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

Methods ​

PlaywrightAssertions.Expect() ​

Added in: v1.18

Creates a APIResponseAssertions object for the given APIResponse.

Usage

Arguments

  • response APIResponse

    APIResponse object to use for assertions.

Returns

  • APIResponseAssertions

PlaywrightAssertions.Expect() ​

Added in: v1.66

Creates a FunctionAssertions object for the given callback.

Usage

csharp
await Expect(async () =>
{
    var response = await Page.APIRequest.GetAsync("https://api.example.com");
    Assert.AreEqual(200, response.Status);
}).ToPassAsync();

Arguments

  • callback Func<Task>

    Callback to retry. A synchronous Action is accepted as well.

Returns

  • FunctionAssertions

PlaywrightAssertions.Expect() ​

Added in: v1.18

Creates a LocatorAssertions object for the given Locator.

Usage

csharp
await Expect(locator).ToBeVisibleAsync();

Arguments

  • locator Locator

    Locator object to use for assertions.

Returns

  • LocatorAssertions

PlaywrightAssertions.Expect() ​

Added in: v1.18

Creates a PageAssertions object for the given Page.

Usage

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

Arguments

  • page Page

    Page object to use for assertions.

Returns

  • PageAssertions

基于 Apache 2.0 许可证发布。