跳至主要内容

断言

断言列表

AssertionDescription
Expect(Locator).ToBeAttachedAsync()Element is attached
Expect(Locator).ToBeCheckedAsync()Checkbox is checked
Expect(Locator).ToBeDisabledAsync()Element is disabled
Expect(Locator).ToBeEditableAsync()Element is editable
Expect(Locator).ToBeEmptyAsync()Container is empty
Expect(Locator).ToBeEnabledAsync()Element is enabled
Expect(Locator).ToBeFocusedAsync()Element is focused
Expect(Locator).ToBeHiddenAsync()Element is not visible
Expect(Locator).ToBeInViewportAsync()Element intersects viewport
Expect(Locator).ToBeVisibleAsync()Element is visible
Expect(Locator).ToContainTextAsync()Element contains text
Expect(Locator).ToHaveAccessibleDescriptionAsync()Element has a matching accessible description
Expect(Locator).ToHaveAccessibleNameAsync()Element has a matching accessible name
Expect(Locator).ToHaveAttributeAsync()Element has a DOM attribute
Expect(Locator).ToHaveClassAsync()Element has a class property
Expect(Locator).ToHaveCountAsync()List has exact number of children
Expect(Locator).ToHaveCSSAsync()Element has CSS property
Expect(Locator).ToHaveIdAsync()Element has an ID
Expect(Locator).ToHaveJSPropertyAsync()Element has a JavaScript property
Expect(Locator).ToHaveRoleAsync()Element has a specific ARIA role
Expect(Locator).ToHaveTextAsync()Element matches text
Expect(Locator).ToHaveValueAsync()Input has a value
Expect(Locator).ToHaveValuesAsync()Select has options selected
Expect(Page).ToHaveTitleAsync()Page has a title
Expect(Page).ToHaveURLAsync()Page has a URL
Expect(Response).ToBeOKAsync()Response has an OK status

设置自定义超时时间

您可以为断言指定自定义超时时间,可以是全局设置或针对每个断言单独设置。默认超时时间为5秒。

全局超时

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 });