PageAssertions
Added in: v1.17The PageAssertions class provides assertion methods that can be used to make assertions about the Page state in the tests.
using System.Text.RegularExpressions;
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;
namespace PlaywrightTests;
[TestClass]
public class ExampleTests : PageTest
{
[TestMethod]
public async Task NavigateToLoginPage()
{
await Page.GetByRole(AriaRole.Button, new() { Name = "Sign In" }).ClickAsync();
await Expect(Page).ToHaveURLAsync(new Regex(".*/login"));
}
}Methods
PageAssertions.ToHaveTitleAsync()
Added in: v1.20Ensures the page has the given title.
Usage
await Expect(Page).ToHaveTitleAsync("Playwright");Arguments
PageAssertions.ToHaveURLAsync()
Added in: v1.20Ensures the page is navigated to the given URL.
Usage
await Expect(Page).ToHaveURLAsync(new Regex(".*checkout"));Arguments
urlOrRegExpstring | RegExpAdded in: v1.18Expected URL string or RegExp.
ignoreCaseboolean(optional) Added in: v1.44Whether to perform case-insensitive match.
ignoreCaseoption takes precedence over the corresponding regular expression parameter if specified. A provided predicate ignores this flag.timeoutfloat(optional) Added in: v1.18Time to retry the assertion for in milliseconds. Defaults to
5000.
PageAssertions.ToMatchAriaSnapshotAsync()
Added in: v1.60Asserts that the page body matches the given accessibility snapshot.
Usage
await page.GotoAsync("https://demo.playwright.dev/todomvc/");
await Expect(page).ToMatchAriaSnapshotAsync(@"
- heading ""todos""
- textbox ""What needs to be done?""
");Arguments
Properties
PageAssertions.Not
Added in: v1.20Makes the assertion check for the opposite condition.
Usage
For example, this code tests that the page URL doesn't contain "error":
await Expect(Page).Not.ToHaveURLAsync("error");Type
PageAssertions