playwright: Assertions断言

本文介绍了在Web自动化测试中使用的断言方法,如检查响应状态码、页面标题、URL,以及对DOM元素的各种属性(如可见性、启用/禁用状态、文本内容、属性值等)的验证,包括正则表达式匹配和多条件断言。
摘要由CSDN通过智能技术生成

response & page 断言

  • to_be_ok()
    判断response的status code是否在200-299之间
    expect(response).to_be_ok()
  • to_have_title(str | pattern)
    判断page的title是否和指定的title一致
    expect(page).to_have_title("title string")
  • to_have_url(str | pattern)
    判断page的URL是否和指定的URL一致
    expect(page).to_have_url("url string")
  • 相应的以上断言都有not_to_**

locator断言

  • to_be_attached(bool) :判断元素在dom节点中存在
  • to_be_checked( bool) :判断元素是否checked(只有支持checked属性的才能成段断言,否则会抛出错误)
  • to_be_disabled() :断言元素是否disabled(是否有disabled属性)
  • to_be_editable( bool) : 断言元素是否可以编辑
  • to_be_empty(): 断言元素值为空 或者dom节点下无内容
  • to_be_enabled(bool): to_be_enabled(enabled=False)等价于 to_be_disabled()
  • to_be_focused() : 断言元素是否focused
  • to_be_hidden() : 断言元素不存在dom中,或者不可见
  • to_be_in_viewport(float) :断言元素是否在当前页面可见, 可以用于断言页面锚点是否正确
  • to_be_visible(bool) :断言元素在dom中,并且可见
  • 使用示例
 <div id="ai-talk-container"></div>
 <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
 <input id="kw-2" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off" disable>
expect(page.locator("#ai-talk-container")).to_be_attached(timeout=100) 
expect(page.locator("#ai-talk-container")).to_be_empty() 
expect(page.locator("#kw")).to_be_editable() 
expect(page.locator("#kw")).to_be_enabled() 
expect(page.locator("#kw-2")).to_be_disabled() 
page.locator("#kw").click()
expect(page.locator("#kw")).to_be_focused() 
expect(page.get_by_role("heading", name="Reviews")).to_be_visible(timeout=100)
expect(page.get_by_role("heading", name="Reviews")).to_be_in_viewport(timeout=100)
  • to_contain_text(expected, ignore_case, timeout, use_inner_text ): 断言元素是否包含指定的值
    • expected 参数必须,可以是str, pattern, list
expect(locator).to_contain_text("substring") # 断言locator的文本是否包含substring
expect(locator).to_contain_text(re.compile("value1|value2")# 断言locator的文本是否包含value1或者value2
expect(page.locator("ul > li")).to_contain_text(["Text 1", "Text 3", "Text 4"]) # 断言li列表的值是否包含"Text 1", "Text 3", "Text 4",需要按顺序才能断言成功
expect(page.locator("ul > li")).to_contain_text(["Text 1", "Text 3", "Text 4"], ignore_case=True) # 忽略大小写
  • to_have_attribute(name, value, ignore_case, timeout): 断言元素是否有属性和值
    • name 参数必须,可以是str
    • value 参数必须,可以是str, pattern
expect(locator).to_have_attribute("src", re.compile(f".*/{img}\.png"))
  • to_have_class(expected , timeout ): 断言元素是否存在指定的class值
    • expected 参数必须,可以是str, pattern, list
expect(locator).to_have_class(re.compile("border-blue")) # 是否包含border-blue class
locator = page.locator("list > .component")
expect(locator).to_have_class(["component", "component selected", "component"])
  • to_have_count(int): 断言元素个数
  • to_have_css(str) :断言元素是否有指定的css演示
  • to_have_id(str | pattern): 断言元素的id
  • to_have_js_property((name, value, timeout): 断言元素是否有指定的JavaScript属性
    • name 和 value参数必须
  • to_have_text(expected, ignore_case, timeout, use_inner_text ): 断言元素是否有指定的文本
    - expected 参数必须,可以是str, pattern, list
expect(locator).to_have_js_property("loaded", True)
expect(locator).to_have_js_property("autocomplete", "off" )
expect(locator).to_have_text("substring") # 断言locator的文本是否等于substring
expect(locator).to_contain_text(re.compile("value1|value2")# 断言locator的文本是否等于value1或者value2
expect(page.locator("ul > li")).to_contain_text(["Text 1", "Text 3", "Text 4"]) # 断言li列表的值是否等于"Text 1", "Text 3", "Text 4",需要按顺序才能断言成功
expect(page.locator("ul > li")).to_contain_text(["Text 1", "Text 3", "Text 4"], ignore_case=True) # 忽略大小写
  • to_have_value(str| pattern):断言元素是否有指定的值
  • to_have_values(list | pattern) :断言元素是否有指定的一组值
expect(locator).to_have_value("200")
expect(locator).to_have_values([re.compile(r"R"), re.compile(r"G")])
  • 相应的以上断言都有not_to_**
  • 22
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值