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 System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;

namespace PlaywrightTests;

[TestFixture]
public class ExampleTests : PageTest
{
[Test]
public async Task StatusBecomesSubmitted()
{
await Page.Locator("#submit-button").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.

Usage

Arguments

Returns


Expect(locator)

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

Creates a LocatorAssertions object for the given Locator.

Usage

await Expect(locator).ToBeVisibleAsync();

Arguments

Returns


Expect(page)

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

Creates a PageAssertions object for the given Page.

Usage

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

Arguments

  • page Page#

    Page object to use for assertions.

Returns