Skip to main content

PageAssertions

PageAssertions 类提供了断言方法,可用于对测试中的 Page 状态进行断言。

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

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

not_to_have_title

Added in: v1.20 pageAssertions.not_to_have_title

expect(page).to_have_title() 相反。

¥The opposite of expect(page).to_have_title().

用法

¥Usage

expect(page).not_to_have_title(title_or_reg_exp)
expect(page).not_to_have_title(title_or_reg_exp, **kwargs)

参数

¥Arguments

预期的标题或正则表达式。

¥Expected title or RegExp.

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

重试断言的时间(以毫秒为单位)。默认为 5000

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

返回

¥Returns


not_to_have_url

Added in: v1.20 pageAssertions.not_to_have_url

expect(page).to_have_url() 相反。

¥The opposite of expect(page).to_have_url().

用法

¥Usage

expect(page).not_to_have_url(url_or_reg_exp)
expect(page).not_to_have_url(url_or_reg_exp, **kwargs)

参数

¥Arguments

需要 URL 字符串或正则表达式。

¥Expected URL string or RegExp.

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

是否进行不区分大小写的匹配。如果指定,ignore_case 选项优先于相应的正则表达式标志。

¥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#

重试断言的时间(以毫秒为单位)。默认为 5000

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

返回

¥Returns


to_have_title

Added in: v1.20 pageAssertions.to_have_title

确保页面具有给定的标题。

¥Ensures the page has the given title.

用法

¥Usage

import re
from playwright.sync_api import expect

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

参数

¥Arguments

预期的标题或正则表达式。

¥Expected title or RegExp.

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

重试断言的时间(以毫秒为单位)。默认为 5000

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

返回

¥Returns


to_have_url

Added in: v1.20 pageAssertions.to_have_url

确保页面导航到给定的 URL。

¥Ensures the page is navigated to the given URL.

用法

¥Usage

import re
from playwright.sync_api import expect

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

参数

¥Arguments

需要 URL 字符串或正则表达式。

¥Expected URL string or RegExp.

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

是否进行不区分大小写的匹配。如果指定,ignore_case 选项优先于相应的正则表达式参数。提供的谓词会忽略此标志。

¥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#

重试断言的时间(以毫秒为单位)。默认为 5000

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

返回

¥Returns