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

PageAssertions ​

Added in: v1.17

The PageAssertions class provides assertion methods that can be used to make assertions about the Page state in the tests.

python
import re
from playwright.async_api import Page, expect

async def test_navigates_to_login_page(page: Page) -> None:
    # ..
    await page.get_by_text("Sign in").click()
    await expect(page).to_have_url(re.compile(r".*/login"))
python
import re
from playwright.sync_api import Page, expect

def test_navigates_to_login_page(page: Page) -> None:
    # ..
    page.get_by_text("Sign in").click()
    expect(page).to_have_url(re.compile(r".*/login"))

Methods ​

page_assertions.not_to_have_title() ​

Added in: v1.20

The opposite of page_assertions.to_have_title().

Arguments

  • title_or_reg_exp string | RegExp Added in: v1.18

    Expected title or RegExp.

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

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

page_assertions.not_to_have_url() ​

Added in: v1.20

The opposite of page_assertions.to_have_url().

Arguments

  • url_or_reg_exp string | RegExp Added in: v1.18

    Expected URL string or RegExp.

  • options Object (optional)

    • ignore_case boolean (optional) Added in: v1.44

      Whether to perform case-insensitive match. ignore_case option takes precedence over the corresponding regular expression flag if specified.

    • timeout float (optional) Added in: v1.18

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

page_assertions.not_to_match_aria_snapshot() ​

Added in: v1.60

The opposite of page_assertions.to_match_aria_snapshot().

Arguments

  • expected string

  • options Object (optional)

    • timeout float (optional)

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

page_assertions.to_have_title() ​

Added in: v1.20

Ensures the page has the given title.

Usage

python
import re
from playwright.async_api import expect

# ...
await expect(page).to_have_title(re.compile(r".*checkout"))
python
import re
from playwright.sync_api import expect

# ...
expect(page).to_have_title(re.compile(r".*checkout"))

Arguments

  • title_or_reg_exp string | RegExp Added in: v1.18

    Expected title or RegExp.

  • options Object (optional)

    • timeout float (optional) Added in: v1.18

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

page_assertions.to_have_url() ​

Added in: v1.20

Ensures the page is navigated to the given URL.

Usage

python
import re
from playwright.async_api import expect

# ...
await expect(page).to_have_url(re.compile(".*checkout"))
python
import re
from playwright.sync_api import expect

# ...
expect(page).to_have_url(re.compile(".*checkout"))

Arguments

  • url_or_reg_exp string | RegExp Added in: v1.18

    Expected URL string or RegExp.

  • options Object (optional)

    • ignore_case boolean (optional) Added in: v1.44

      Whether to perform case-insensitive match. ignore_case 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.

page_assertions.to_match_aria_snapshot() ​

Added in: v1.60

Asserts that the page body matches the given accessibility snapshot.

Usage

python
await page.goto("https://demo.playwright.dev/todomvc/")
await expect(page).to_match_aria_snapshot('''
  - heading "todos"
  - textbox "What needs to be done?"
''')
python
page.goto("https://demo.playwright.dev/todomvc/")
expect(page).to_match_aria_snapshot('''
  - heading "todos"
  - textbox "What needs to be done?"
''')

Arguments

  • expected string

  • options Object (optional)

    • timeout float (optional)

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

基于 Apache 2.0 许可证发布。