【playwright】新一代自动化测试神器playwright+python系列课程48_playwright_expect断言

playwright_expect断言

playwright 提供了一个 expect方法 用于断言,该方法提供了丰富的断言场景,我们来看一下expect的使用。

expect 使用

断言描述
expect(locator).to_be_checked()Checkbox is checked
expect(locator).to_be_disabled()Element is disabled
expect(locator).to_be_editable()Element is enabled
expect(locator).to_be_empty()Container is empty
expect(locator).to_be_enabled()Element is enabled
expect(locator).to_be_focused()Element is focused
expect(locator).to_be_hidden()Element is not visible
expect(locator).to_be_visible()Element is visible
expect(locator).to_contain_text()Element contains text
expect(locator).to_have_attribute()Element has a DOM attribute
expect(locator).to_have_class()Element has a class property
expect(locator).to_have_count()List has exact number of children
expect(locator).to_have_css()Element has CSS property
expect(locator).to_have_id()Element has an ID
expect(locator).to_have_js_property()Element has a JavaScript property
expect(locator).to_have_text()Element matches text
expect(locator).to_have_value()Input has a value
expect(locator).to_have_values()Select has options selected
expect(page).to_have_title()Page has a title
expect(page).to_have_url()Page has a URL
expect(api_response).to_be_ok()Response has an OK status

Expect提供的断言场景非常多,我们选择一些常用的断言场景看一下使用的案例,工作中我们可以选择网页的标题和url来进行断言,我们先来看一下to_have_title()和to_have_url()两个方法的使用。
实践代码:

# '''
# author: 测试-老姜   交流微信/QQ:349940839
# 欢迎添加微信或QQ,加入学习群共同学习交流。
# QQ交流群号:877498247
# 西安的朋友欢迎当面交流。
# '''

from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False,slow_mo=2000)
    context = browser.new_context(record_video_dir="videos") #设置录屏保存目录
    page = context.new_page()
    page.goto("http://127.0.0.1/zentao/user-login.html") # 使用相对地址
    page.locator("#account").fill("admin")
    page.locator("input[name=\"password\"]").click()
    page.locator("input[name=\"password\"]").fill("Deshifuzhi01")
    page.get_by_role("button", name="登录").click()
    #脚本执行完成后断言网页的标题是不是'地盘 - 禅道'
    expect(page).to_have_title('地盘 - 禅道')
    #脚本执行完成后断言网页的标题是不是'http://127.0.0.1/zentao/my.html'
    expect(page).to_have_url('http://127.0.0.1/zentao/my.html')
    page.wait_for_timeout(10000)
    # ---------------------
    context.close() # 上下文关闭时保存录屏
    browser.close()

如果断言失败,会爆出unexpected value XXXX的异常信息。

我们再看一下to_have_count(),以元素的个数来断言。

实践代码:

# '''
# author: 测试-老姜   交流微信/QQ:349940839
# 欢迎添加微信或QQ,加入学习群共同学习交流。
# QQ交流群号:877498247
# 西安的朋友欢迎当面交流。
# '''

from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False,slow_mo=2000)
    context = browser.new_context(record_video_dir="videos") #设置录屏保存目录
    page = context.new_page()
    page.goto("http://127.0.0.1/zentao/user-login.html") # 使用相对地址
    page.locator("#account").fill("admin")
    page.locator("input[name=\"password\"]").click()
    page.locator("input[name=\"password\"]").fill("Deshifuzhi01")
    loc = page.locator('text=登录')
    #断言包含登录两个字的元素是否为2个
    expect(loc).to_have_count(2)

    # ---------------------
    context.close() # 上下文关闭时保存录屏
    browser.close()

with sync_playwright() as playwright:
    run(playwright)

to_have_text():以元素上的文本是否等于字符串来断言

实践代码:

# '''
# author: 测试-老姜   交流微信/QQ:349940839
# 欢迎添加微信或QQ,加入学习群共同学习交流。
# QQ交流群号:877498247
# 西安的朋友欢迎当面交流。
# '''

from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False,slow_mo=2000)
    context = browser.new_context(record_video_dir="videos") #设置录屏保存目录
    page = context.new_page()
    page.goto("http://127.0.0.1/zentao/user-login.html") # 使用相对地址
    page.locator("#account").fill("admin")
    page.locator("input[name=\"password\"]").click()
    page.locator("input[name=\"password\"]").fill("Deshifuzhi01")
    page.locator('text="登录"').click()
    loc = page.frame_locator('#appIframe-my').locator('.user-welcome')
    #断言元素上是否等于文本'admin,下午好!'
    expect(loc).to_have_text('admin,晚上好!')
    page.wait_for_timeout(10000)
    # ---------------------
    context.close() # 上下文关闭时保存录屏
    browser.close()

with sync_playwright() as playwright:
    run(playwright)


to_contain_text():以元素上的文本是否包含字符串来断言

# '''
# author: 测试-老姜   交流微信/QQ:349940839
# 欢迎添加微信或QQ,加入学习群共同学习交流。
# QQ交流群号:877498247
# 西安的朋友欢迎当面交流。
# '''

from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False,slow_mo=2000)
    context = browser.new_context(record_video_dir="videos") #设置录屏保存目录
    page = context.new_page()
    page.goto("http://127.0.0.1/zentao/user-login.html") # 使用相对地址
    page.locator("#account").fill("admin")
    page.locator("input[name=\"password\"]").click()
    page.locator("input[name=\"password\"]").fill("Deshifuzhi01")
    page.locator('text="登录"').click()
    loc = page.frame_locator('#appIframe-my').locator('.user-welcome')
    #断言元素上是否包含文本'admin'
    expect(loc).to_contain_text('admin')
    page.wait_for_timeout(10000)
    # ---------------------
    context.close() # 上下文关闭时保存录屏
    browser.close()
  • 21
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值