Playwright断言
Playwright 通过 expect 函数包含测试断言。要进行断言请调用 expect(value) 并选择一个反映预期的匹配器。Playwright 包含异步匹配器它们会一直等待直到满足预期条件。使用这些匹配器可以使测试保持稳定且具有弹性。例如此代码会一直等待页面标题包含“Playwright”为止。awaitexpect(page).toHaveTitle(/Playwright/);以下是常用的异步断言断言 描述 expect(locator).toBeChecked() 复选框已选中 expect(locator).toBeEnabled() 控件已启用 expect(locator).toBeVisible() 元素可见 expect(locator).toContainText() 元素包含文本 expect(locator).toHaveAttribute() 元素具有属性 expect(locator).toHaveCount() 元素列表具有给定长度 expect(locator).toHaveText() 元素匹配文本 expect(locator).toHaveValue() 输入元素具有值 expect(page).toHaveTitle() 页面具有标题 expect(page).toHaveURL() 页面具有 URLPlaywright 还包含通用的匹配器如toEqual、toContain、toBeTruthy可用于断言任何条件。这些断言不使用 await 关键字因为它们是对已有值进行即时的同步检查。expect(success).toBeTruthy();