Playwright UI 自动化示例

发布时间: 更新时间: 总字数:442 阅读时间:1m 作者: IP属地: 分享 复制网址

playwright为现代web应用提供可靠的端到端测试能力,是实现web自动化的重要工具。

介绍

  • 跨浏览器:playwright支持所有现代渲染引擎,包括Chromium、WebKit和Firefox
  • 跨平台的:在Windows、Linux和macOS上进行测试,本地或CI,无头或头部
  • 跨语言:支持在 TypeScript、JavaScript、Python、.net,、Java中使用 playwright API
  • 测试移动网络:原生移动模拟的谷歌Chrome为Android和移动Safari。同样的渲染引擎可以在桌面和云中工作

安装

  • download new browsers
playwright install
  • js
npm i -D playwright
pip install playwright==1.8.0a1
  • 代码自动生成
python -m playwright codegen --help

示例

  • 生成测试代码
python -m playwright codegen -b webkit https://www.xiexianbin.cn/search/

根据上面命令添加搜索和截图的代码示例:

from playwright.sync_api import Playwright, sync_playwright, expect


def run(playwright: Playwright) -> None:
    browser = playwright.webkit.launch(headless=False)
    context = browser.new_context()
    # Open new page
    page = context.new_page()
    # Go to https://www.www.xiexianbin.cn/search/
    page.goto("https://www.xiexianbin.cn/search/")

    # Fill an input with the name "search"
    page.fill("input[name=\"search\"]", "谢先斌")
    # page.locator('name=search').fill('value')
    # Click an element with data-test-id "submit"
    # page.click("a[target=\"_blank\"]")
    page.locator("[placeholder=\"Search\"]").click()
    page.locator("input[name=\"search\"]").press("Enter")

    page.wait_for_selector(selector="blockquote[id=\"search-result\"]", state="visible")

    # screen shot
    page.screenshot(path='search-谢先斌.png')

    # ---------------------
    context.close()
    browser.close()


if __name__ == '__main__':
    with sync_playwright() as playwright:
        run(playwright)

参考

  1. https://playwright.dev/
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数