PageAssertions
PageAssertions 类提供了断言方法,可用于在测试中对 Page 状态进行断言。
🌐 The PageAssertions class provides assertion methods that can be used to make assertions about the Page state in the tests.
using System.Text.RegularExpressions;
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;
namespace PlaywrightTests;
[TestClass]
public class ExampleTests : PageTest
{
[TestMethod]
public async Task NavigateToLoginPage()
{
await Page.GetByRole(AriaRole.Button, new() { Name = "Sign In" }).ClickAsync();
await Expect(Page).ToHaveURLAsync(new Regex(".*/login"));
}
}
方法
🌐 Methods
ToHaveTitleAsync
Added in: v1.20确保页面具有给定的标题。
🌐 Ensures the page has the given title.
用法
await Expect(Page).ToHaveTitleAsync("Playwright");
参数
-
titleOrRegExpstring | Regex Added in: v1.18#预期的标题或正则表达式。
-
optionsPageAssertionsToHaveTitleOptions?(optional)-
Timeout[float]? (optional) Added in: v1.18#重试断言的时间(以毫秒为单位)。默认值为
5000。
-
返回
ToHaveURLAsync
Added in: v1.20确保页面导航到给定的 URL。
🌐 Ensures the page is navigated to the given URL.
用法
await Expect(Page).ToHaveURLAsync(new Regex(".*checkout"));
参数
-
urlOrRegExpstring | Regex Added in: v1.18#需要 URL 字符串或正则表达式。
-
optionsPageAssertionsToHaveURLOptions?(optional)-
IgnoreCasebool? (optional) Added in: v1.44#是否执行不区分大小写的匹配。如果指定了 IgnoreCase 选项,则优先于相应的正则表达式参数。提供的谓词会忽略此标志。
-
Timeout[float]? (optional) Added in: v1.18#重试断言的时间(以毫秒为单位)。默认值为
5000。
-
返回
ToMatchAriaSnapshotAsync
Added in: v1.60断言页面主体与给定的 可访问性快照 匹配。
🌐 Asserts that the page body matches the given accessibility snapshot.
用法
await page.GotoAsync("https://demo.playwright.dev/todomvc/");
await Expect(page).ToMatchAriaSnapshotAsync(@"
- heading ""todos""
- textbox ""What needs to be done?""
");
参数
expectedstring#optionsPageAssertionsToMatchAriaSnapshotOptions?(optional)-
Timeout[float]? (optional)#重试断言的时间(以毫秒为单位)。默认值为
5000。
-
返回
属性
🌐 Properties
Not
Added in: v1.20使断言检查相反的条件。
🌐 Makes the assertion check for the opposite condition.
用法
例如,这段代码测试页面 URL 不包含 "error":
🌐 For example, this code tests that the page URL doesn't contain "error":
await Expect(Page).Not.ToHaveURLAsync("error");
类型