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 static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

public class TestPage {
// ...
@Test
void navigatesToLoginPage() {
// ...
page.getByText("Sign in").click();
assertThat(page).hasURL(Pattern.compile(".*/login"));
}
}

方法

🌐 Methods

hasTitle

Added in: v1.20 pageAssertions.hasTitle

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

🌐 Ensures the page has the given title.

用法

assertThat(page).hasTitle("Playwright");

参数

  • titleOrRegExp String | Pattern Added in: v1.18#

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

  • options PageAssertions.HasTitleOptions (optional)

    • setTimeout double (optional) Added in: v1.18#

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

返回


hasURL

Added in: v1.20 pageAssertions.hasURL

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

🌐 Ensures the page is navigated to the given URL.

用法

assertThat(page).hasURL(".com");

参数

  • urlOrRegExp String | Pattern Added in: v1.18#

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

  • options PageAssertions.HasURLOptions (optional)

    • setIgnoreCase boolean (optional) Added in: v1.44#

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

    • setTimeout double (optional) Added in: v1.18#

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

返回


属性

🌐 Properties

not()

Added in: v1.20 pageAssertions.not()

使断言检查相反的条件。例如,这段代码测试页面 URL 不包含 "error"

🌐 Makes the assertion check for the opposite condition. For example, this code tests that the page URL doesn't contain "error":

assertThat(page).not().hasURL("error");

用法

assertThat(page).not()

返回