Skip to main content

斷言

斷言列表

AssertionDescription
Expect(Locator).ToBeAttachedAsync()元素已附加
Expect(Locator).ToBeCheckedAsync()核取方塊已勾選
Expect(Locator).ToBeDisabledAsync()元素已停用
Expect(Locator).ToBeEditableAsync()元素可編輯
Expect(Locator).ToBeEmptyAsync()容器為空
Expect(Locator).ToBeEnabledAsync()元素已啟用
Expect(Locator).ToBeFocusedAsync()元素已聚焦
Expect(Locator).ToBeHiddenAsync()元素不可見
Expect(Locator).ToBeInViewportAsync()元素與視口相交
Expect(Locator).ToBeVisibleAsync()元素可見
Expect(Locator).ToContainTextAsync()元素包含文字
Expect(Locator).ToHaveAccessibleDescriptionAsync()元素具有匹配的可訪問描述
Expect(Locator).ToHaveAccessibleNameAsync()元素具有匹配的可訪問名稱
Expect(Locator).ToHaveAttributeAsync()元素具有 DOM 屬性
Expect(Locator).ToHaveClassAsync()元素具有類別屬性
Expect(Locator).ToHaveCountAsync()列表具有確切的子項數量
Expect(Locator).ToHaveCSSAsync()元素具有 CSS 屬性
Expect(Locator).ToHaveIdAsync()元素具有 ID
Expect(Locator).ToHaveJSPropertyAsync()元素具有 JavaScript 屬性
Expect(Locator).ToHaveRoleAsync()元素具有特定的ARIA 角色
Expect(Locator).ToHaveTextAsync()元素匹配文字
Expect(Locator).ToHaveValueAsync()輸入具有值
Expect(Locator).ToHaveValuesAsync()選擇具有選定的選項
Expect(Page).ToHaveTitleAsync()頁面具有標題
Expect(Page).ToHaveURLAsync()頁面具有 URL
Expect(Response).ToBeOKAsync()響應具有 OK 狀態

設定 a custom timeout

您可以為斷言指定自訂的超時時間,無論是全域還是每個斷言。預設的超時時間為 5 秒。

全域 timeout

UnitTest1.cs
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PlaywrightTests;

[TestClass]
public class UnitTest1 : PageTest
{
[ClassInitialize]
public static void GlobalSetup(TestContext context)
{
SetDefaultExpectTimeout(10_000);
}
// ...
}

每個斷言超時

UnitTest1.cs
await Expect(Page.GetByText("Name")).ToBeVisibleAsync(new() { Timeout = 10_000 });