Skip to content

Latest commit

 

History

History
184 lines (142 loc) · 9 KB

File metadata and controls

184 lines (142 loc) · 9 KB
id class-pageassertions
title PageAssertions

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import HTMLCard from '@site/src/components/HTMLCard';

The 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

ToHaveTitleAsync {#page-assertions-to-have-title}

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.20pageAssertions.ToHaveTitleAsync

Ensures the page has the given title.

Usage

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

Arguments

  • titleOrRegExp string | Regex Added in: v1.18#

    Expected title or RegExp.

  • options PageAssertionsToHaveTitleOptions? (optional)

    • Timeout [float]? (optional) Added in: v1.18#

      Time to retry the assertion for in milliseconds. Defaults to 5000.

Returns


ToHaveURLAsync {#page-assertions-to-have-url}

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.20pageAssertions.ToHaveURLAsync

Ensures the page is navigated to the given URL.

Usage

await Expect(Page).ToHaveURLAsync(new Regex(".*checkout"));

Arguments

  • urlOrRegExp string | Regex Added in: v1.18#

    Expected URL string or RegExp.

  • options PageAssertionsToHaveURLOptions? (optional)

    • IgnoreCase bool? (optional) Added in: v1.44#

      Whether to perform case-insensitive match. IgnoreCase option takes precedence over the corresponding regular expression parameter if specified. A provided predicate ignores this flag.

    • Timeout [float]? (optional) Added in: v1.18#

      Time to retry the assertion for in milliseconds. Defaults to 5000.

Returns


Properties

Not {#page-assertions-not}

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.20pageAssertions.Not

Makes 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