断言
断言列表
🌐 List of assertions
| 断言 | 描述 |
| :- | :- |
| 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).ToContainClassAsync() | 元素具有指定的 CSS 类 |
| Expect(Locator).ToContainTextAsync() | 元素包含文本 |
| Expect(Locator).ToHaveAccessibleDescriptionAsync() | 元素具有匹配的 可访问描述 |
| Expect(Locator).ToHaveAccessibleNameAsync() | 元素具有匹配的 可访问名称 |
| Expect(Locator).ToHaveAttributeAsync() | 元素具有 DOM 属性 |
| Expect(Locator).ToHaveClassAsync() | 元素具有一个 class 属性 |
| 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(Locator).ToMatchAriaSnapshotAsync() | 元素与提供的 Aria 快照匹配 |
| Expect(Page).ToHaveTitleAsync() | 页面有标题 |
| Expect(Page).ToHaveURLAsync() | 页面具有 URL |
| Expect(Response).ToBeOKAsync() | 响应状态为 OK |
设置自定义超时
🌐 Setting a custom timeout
你可以为断言指定自定义超时时间,既可以全局设置,也可以针对单个断言设置。默认超时时间为 5 秒。
🌐 You can specify a custom timeout for assertions either globally or per assertion. The default timeout is 5 seconds.
全局超时
🌐 Global timeout
- MSTest
- NUnit
- xUnit
- xUnit v3
using Microsoft.Playwright;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class Tests : PageTest
{
[OneTimeSetUp]
public void GlobalSetup()
{
SetDefaultExpectTimeout(10_000);
}
// ...
}
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);
}
// ...
}
using Microsoft.Playwright;
using Microsoft.Playwright.Xunit;
namespace PlaywrightTests;
public class UnitTest1: PageTest
{
UnitTest1()
{
SetDefaultExpectTimeout(10_000);
}
// ...
}
using Microsoft.Playwright;
using Microsoft.Playwright.Xunit.v3;
namespace PlaywrightTests;
public class UnitTest1: PageTest
{
UnitTest1()
{
SetDefaultExpectTimeout(10_000);
}
// ...
}
每个断言超时
🌐 Per assertion timeout
await Expect(Page.GetByText("Name")).ToBeVisibleAsync(new() { Timeout = 10_000 });