跳到主要內容

PlaywrightAssertions

Playwright 提供網頁優先的斷言,並提供便利的方法來建立斷言,這些斷言將等待並重試直到符合預期條件。

考慮以下範例

using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;

namespace PlaywrightTests;

[TestClass]
public class ExampleTests : PageTest
{
[TestMethod]
public async Task StatusBecomesSubmitted()
{
await Page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).ClickAsync();
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
}

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

預設情況下,斷言的超時時間設定為 5 秒。


方法

Expect(response)

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

為給定的 APIResponse 建立 APIResponseAssertions 物件。

用法

參數

返回


Expect(locator)

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

為給定的 Locator 建立 LocatorAssertions 物件。

用法

await Expect(locator).ToBeVisibleAsync();

參數

返回


Expect(page)

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

為給定的 Page 建立 PageAssertions 物件。

用法

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

參數

返回