Playwright断言
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为给定的 APIResponse 创建 APIResponseAssertions 对象。
用法
参数
-
response
APIResponse#用于断言的 APIResponse 对象。
返回值
Expect(locator)
添加于: v1.18为给定的 定位器 (Locator) 创建 LocatorAssertions 对象。
用法
await Expect(locator).ToBeVisibleAsync();
参数
-
locator
定位器 (Locator)#用于断言的 定位器 (Locator) 对象。
返回值
Expect(page)
添加于: v1.18为给定的 页面 (Page) 创建 PageAssertions 对象。
用法
await Expect(Page).ToHaveTitleAsync("News");
参数
返回值