Skip to main content

PlaywrightAssertions

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

Expect(response)

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

Creates a APIResponseAssertions object for the given APIResponse.

使用方式

參數

傳回值


Expect(locator)

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

Creates a LocatorAssertions object for the given Locator.

使用方式

await Expect(locator).ToBeVisibleAsync();

參數

傳回值


Expect(page)

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

Creates a PageAssertions object for the given Page.

使用方式

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

參數

  • page Page#

    Page object to use for assertions.

傳回值