跳到主要內容

PlaywrightAssertions

Playwright 提供您 Web-First Assertions,並提供便利的方法來建立 assertions,這些 assertions 將等待並重試直到符合預期條件。

考慮以下範例

import { test, expect } from '@playwright/test';

test('status becomes submitted', async ({ page }) => {
// ...
await page.locator('#submit-button').click();
await expect(page.locator('.status')).toHaveText('Submitted');
});

Playwright 將會重新測試具有選擇器 .status 的節點,直到獲取的節點具有 "Submitted" 文字。它將會一遍又一遍地重新獲取節點並檢查它,直到滿足條件或達到逾時時間。您可以將此逾時時間作為一個選項傳遞。

預設情況下,assertions 的逾時時間設定為 5 秒。


方法

expect(response)

新增於:v1.18 playwrightAssertions.expect(response)

為給定的 APIResponseAssertions 物件建立 APIResponse

用法

引數

返回


expect(value)

新增於:v1.9 playwrightAssertions.expect(value)

為給定的 value 建立 GenericAssertions 物件。

用法

expect(value);

引數

  • value Object#

    將被 assertion 的 Value。

返回


expect(locator)

新增於:v1.18 playwrightAssertions.expect(locator)

為給定的 LocatorAssertions 物件建立 Locator

用法

引數

返回


expect(page)

新增於:v1.18 playwrightAssertions.expect(page)

為給定的 PageAssertions 物件建立 Page

用法

引數

  • page Page#

    用於 assertions 的 Page 物件。

返回