playwright的一些断言

from playwright.sync_api import sync_playwright, expect

def run(playwright):
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()

    # 打开新页面
    page = context.new_page()

    # 导航到示例页面
    page.goto("https://example.com")

    # 断言页面标题
    expect(page).to_have_title("Example Domain")

    # 断言 h1 文本内容
    expect(page.locator('h1')).to_have_text('Example Domain')

    # 断言元素可见
    expect(page.locator('text="More information..."')).to_be_visible()

    # 断言元素不可见
    # 假设页面上有一个隐藏的元素,ID为hidden-element
    # expect(page.locator('#hidden-element')).not_to_be_visible()

    # 断言元素属性
    # 假设页面上有一个按钮,ID为submit-button,其属性为disabled
    # expect(page.locator('#submit-button')).to_have_attribute('disabled', 'true')

    # 断言元素数量
    # 假设页面上有三个div元素,类名为item
    # expect(page.locator('div.item')).to_have_count(3)

    # 断言 URL
    expect(page).to_have_url('https://example.com')

    # 断言输入框的值
    # 假设页面上有一个输入框,名称为username,其值为testuser
    # expect(page.locator('input[name="username"]')).to_have_value('testuser')

    # 断言元素的 CSS 属性
    # 假设页面上有一个按钮,其背景颜色为rgb(0, 123, 255)
    # expect(page.locator('button')).to_have_css('background-color', 'rgb(0, 123, 255)')

    # 断言元素可编辑
    # 假设页面上有一个输入框
    # expect(page.locator('input')).to_be_editable()

    # 断言复选框已选中
    # 假设页面上有一个复选框
    # expect(page.locator('input[type="checkbox"]')).to_be_checked()

    # 关闭上下文和浏览器
    context.close()
    browser.close()

with sync_playwright() as playwright:
    run(playwright)

 

posted @ 2024-06-26 10:55  苦逼小李  阅读(615)  评论(0)    收藏  举报