Playwright Python版本安装指南及入门使用教程(持续更新)

目录

  • Playwright初步安装使用教程(记录)(更新中)
  • 一、简介
  • 二、安装
  • 三、实例测试代码(官网)
  • 四、基本的action列表
  • 五、常用断言列表
  • 六、生成测试(Codegen)
  • 七、运行测试用例文件
  • 八、调试测试
  • Playwright初步安装使用教程(记录)(更新中)

    一、简介

    Playwright 是专门为满足端到端测试的需求而创建的。Playwright 支持所有现代渲染引擎,包括 Chromium、WebKit 和 Firefox。在 Windows、Linux 和 macOS 上、本地或 CI 上、无头或使用本机移动仿真进行 headed 测试。

    与selenium对比:

    二、安装

    python/conda环境安装命令:

    pip:
    pip install pytest-playwright
    
    conda:
    conda install pytest-playwright
    

    检查是否安装成功:

    pip:
    pip show pytest-playwright
    
    conda:
    conda list | findstr "playwright"
    
    
    三、实例测试代码(官网)

    下段代码从官网截取,约定测试用例文件开头与ptest相同,必须为test开头命令

    import re
    from playwright.sync_api import Page, expect
    
    def test_has_title(page: Page):
        page.goto("https://playwright.dev/")
    
        # Expect a title "to contain" a substring.
        expect(page).to_have_title(re.compile("Playwright"))
    
    def test_get_started_link(page: Page):
        page.goto("https://playwright.dev/")
    
        # Click the get started link.
        page.get_by_role("link", name="Get started").click()
    
        # Expects page to have a heading with the name of Installation.
        expect(page.get_by_role("heading", name="Installation")).to_be_visible()
    

    运行命令:

    pytest -s (文件名)
    

    默认模式下运行用例只会返回运行结果,不会打开浏览器,如果需要打开多个浏览器并且能够显示查看则用以下命令,此命令一次打开谷歌和火狐浏览器执行用例,–headed代表可以显示查看浏览器执行过程:

    pytest --browser chromium --browser firefox --headed
    
    四、基本的action列表
    action descrip
    locator.check() 选中input复选框
    locator.click() 单击元素
    locator.uncheck() 取消选中Input复选框
    locator.hover() 鼠标悬停在元素上
    locator.fill() 填写表单字段,输入文本
    locator.focus() 聚焦元素
    locator.press() 单次按键
    locator.set_input_files() 选择要上传的文件
    locator.select_option() 在下拉列表中选择选项
    五、常用断言列表
    Assertion Description
    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() Input 元素具有值
    expect(page).to_have_title() 页面有标题
    expect(page).to_have_url() 页面有 URL
    六、生成测试(Codegen)

    运行codegen可以对网页进行action自动生成,expect自动生成,对网页进行操作后复制对应代码微调即可。

    执行命令:

    playwright codegen
    

    操作项:

  • 'assert visibility'断言元素可见
  • 'assert text'断言元素包含特定文本
  • 'assert value'断言元素具有特定值
  • 页面实例:

    七、运行测试用例文件

    运行方式与pytest相同,下载的插件集成了pytest。

    运行单个文件:

    pytest test_testexample.py
    

    运行多个文件

    pytest test_testexample1.py test_testexample2.py
    

    运行特定测试用例,用***-k***去匹配用例名称:

    pytest -k test_example_item
    

    并行运行测试(用例过多时用分布式运行减少运行时间),建议使用一半的逻辑CPU内核

    pytest --numprocesses 2
    
    八、调试测试

    Playwright 附带 Playwright Inspector,它允许逐步执行 Playwright API 调用,查看其调试日志并浏览action。

    PowerShell版本:
    $env:PWDEBUG=1
    pytest -s
    

    对于此命令同样可以使用pytest -s-k等命令

    ——————————————————————————————————分界线后续继续更新

    作者:派大星的大摇裤

    物联沃分享整理
    物联沃-IOTWORD物联网 » Playwright Python版本安装指南及入门使用教程(持续更新)

    发表回复