Playwright断言03,如何成?

摘要:Playwright 断言 import re from playwright.sync_api import expect expect(page).to_have_title(re.compile("Playw
Playwright 断言 import re from playwright.sync_api import expect expect(page).to_have_title(re.compile("Playwright")) # 等待页面标题包含“Playwright”: 断言 描述 expect(locator).to_be_checked() 复选框已选中 expect(locator).to_be_enabled() 控制功能已启用 expect(locator).to_be_visible() 元素可见 expect(locator).to_contain_text() 元素包含文本 expect(locator).to_have_attribute() 元素具有属性 expect(locator).to_have_count() 元素列表的长度已给出 expect(locator).to_have_text() 元素与文本匹配 expect(locator).to_have_value() 输入元素有值 expect(page).to_have_title() 页面标题 expect(page).to_have_url() 页面包含 URL expect(locator).to_be_checked() Playwright 中用于断言检查元素选中状态的方法。 功能说明 断言用途:验证复选框(checkbox)或单选按钮(radio)是否处于选中状态 返回值:如果元素被选中则断言通过,否则失败 # 检查复选框是否被选中 checkbox = page.get_by_role("checkbox", name="同意条款") checkbox.check() expect(checkbox).to_be_checked() # 检查单选按钮是否被选中 radio_button = page.get_by_role("radio", name="男") radio_button.click() expect(radio_button).to_be_checked() expect(locator).to_be_visible() 用于断言元素可见性的方法 断言用途:验证指定的locator元素在页面上是否可见 检查内容:元素不仅存在于DOM中,而且在视觉上对用户可见 返回结果:如果元素可见则断言通过,否则失败 # 检查按钮是否可见 button = page.get_by_role("button", name="提交") expect(button).to_be_visible() # 检查文本是否显示 message = page.get_by_text("操作成功") expect(message).to_be_visible() expect(locator).to_contain_text() Playwright 中用于断言元素文本内容包含特定字符串的方法。 断言用途:验证指定 locator 元素的文本内容是否包含预期的文本 匹配方式:部分文本匹配,不需要完全相等 大小写敏感:默认情况下区分大小写 # 检查元素文本是否包含特定内容 title = page.get_by_role("heading", name="产品") expect(title).to_contain_text("产品") # 检查列表项是否包含指定文本 list_item = page.get_by_role("listitem") expect(list_item).to_contain_text("重要信息") expect(page).to_have_title() Playwright 中用于断言页面标题的 expect 方法。 断言用途:验证当前页面的标题是否符合预期 匹配方式:完全匹配页面的 title 标签内容 使用场景:常用于页面导航后的验证 断言用途:验证当前页面的标题是否符合预期 匹配方式:完全匹配页面的 title 标签内容 使用场景:常用于页面导航后的验证 # 断言页面标题 expect(page).to_have_title("首页 - 我的网站") # 结合其他操作使用 page.goto("https://example.com") expect(page).to_have_title("Example Domain")