• WEB自动化测试(6)—— 命令行运行Cypress


    6 命令行运行Cypress

        Cypress命令行的运行基本语法格式如下所示:

    cypress <command> [options]
    

        command代表运行的命令,是必选参数。支持的命令有:openruninstallverifycacheversionhelp。options是代表各command支持的参数,是可选参数。

    在日常项目,用得较多的是run和open两个命令。

    6.1 cypress run

    6.1.1 概述

        cypress run 主要用于在命令行模式下运行测试用例,直到结束。默认情况下,cypress run 使用无头模式运行测试。其基本语法格式如下所示:

    cypress run [options]
    

    6.1.2 常用参数

        cypress run在运行时,可以指定多个参数,其指定的参数将应用于本次测试阶段且会覆盖cypress.json中相同的参数。常用的参数如下所示:

    参数功能描述
    --browser, -b配置运行浏览器
    --ci-build-id用于分组运行或并行运行
    --config, -c运行时的配置项
    --config-file, -C运行时所使用的配置文件
    --env, -e设置环境变量
    --key, -k指定录制视频的秘钥
    --headed使用有头模式运行测试
    --no-exit运行完成后不退出Test Runner
    --parallel在多台机器上并行运行测试
    --port,-p指定运行时的端口
    --project, -P指定运行的项目
    --record在运行录制视频
    --reporter, -r使用Mocha样式的测试报告
    --reporter-options, -o指定Mocha报告的配置项
    --spec, -s指定本次要运行文件目录或文件
    --tag, -t给正在运行的测试程序打tag或tags,主要用于在Dashboard上产生标识

        常见用法示例如下所示:

    • 指定运行浏览器
    1. cypress run --browser chrome
    2. // 或指定浏览器安装路径
    3. cypress run --browser /usr/bin/chromium

        可被指定的浏览器有chromechromiumedgeelectronfirefox

    • 添加配置项
    cypress run --config pageLoadTimeout=100000,watchForFileChanges=false
    
    • 添加配置文件
    cypress run --config-file tests/cypress-config.json
    
    • 添加环境变量
    1. cypress run --env host=test.surpass.com
    2. // 多个环境变量,使用逗号隔开
    3. cypress run --env host=test.surpass.com,port=20149
    4. // 使用JSON字符串
    5. cypress run --env flags={"host":"test.surpass.com","port":20149}

    多个环境变量,使用逗号隔开或使用JSON字符串

    • 指定测试报告格式
    1. cypress run --reporter json
    2. cypress run --reporter junit --reporter-options mochaFile=result.xml,toConsole=true
    • 指定运行的测试文件
    1. cypress run --spec "cypress\integration\3-Surpass-Test-Examples\testSelect\test.visit.local.file.js"
    2. cypress run --spec "cypress\integration\3-Surpass-Test-Examples\testSelect\test.visit.local.file.js","cypress\integration\3-Surpass-Test-Examples\testPost\test.post.spec.js"
    3. cypress run --spec "cypress\integration\3-Surpass-Test-Examples\testSelect\*.js"
    4. cypress run --spec "cypress\integration\3-Surpass-Test-Examples\testSelect\*"

    一次运行多个测试文件,使用逗号隔开

        运行结果如下所示:

    6.2 cypress open

    6.2.1 概述

        cypress open 主要用于打开交互式的Test Runner,其基本语法如下所示:

    cypress open [options]
    

    6.2.2 常用参数

        常用的参数如下所示:

    参数功能描述
    --browser, -b配置运行浏览器
    --config, -c运行时的配置项
    --config-file, -C运行时所使用的配置文件
    --env, -e设置环境变量
    --port,-p指定运行时的端口
    --project, -P指定运行的项目

        cypress open用法同cypress run跳过。

    6.3 cypress info

        cypress info用于显示当前Cypress的运行环境,如下所示:

    • 运行机器上安装的浏览器
    • 运行环境变更,比如说代理设置等
    • 运行时的数据存储路径
    • 操作系统和内存信息待

        运行的结果如下图所示:

    6.4 cypress verify

        cypress verify主要用于验证Cypress是否正确安装且能运行。如下所示:

    1. C:\Users\admin\Documents\CypressProjects>cypress verify
    2. ✔ Verified Cypress! C:\Users\admin\AppData\Local\Cypress\Cache\9.5.4\Cypress

    6.5 cypress version

        cypress version主要用于查看安装的cypress版本信息。如下所示:

    1. C:\Users\admin\Documents\CypressProjects>cypress verify
    2. ✔ Verified Cypress! C:\Users\admin\AppData\Local\Cypress\Cache\9.5.4\Cypress
    3. C:\Users\admin\Documents\CypressProjects>cypress version
    4. Cypress package version: 9.5.4
    5. Cypress binary version: 9.5.4
    6. Electron version: 15.3.5
    7. Bundled Node version:
    8. 16.5.0

    6.5 cypress help

        cypress help主要用于查看cypress提供的帮助信息,如下所示:

    1. C:\Users\admin\Documents\CypressProjects>cypress help
    2. Usage: cypress [options]
    3. Options:
    4. -v, --version prints Cypress version
    5. -h, --help display help for command
    6. Commands:
    7. help Shows CLI help and exits
    8. version prints Cypress version
    9. open [options] Opens Cypress in the interactive GUI.
    10. run [options] Runs Cypress tests from the CLI without the GUI
    11. open-ct [options] Opens Cypress component testing interactive mode.
    12. run-ct [options] Runs all Cypress Component Testing suites
    13. install [options] Installs the Cypress executable matching this package's version
    14. verify [options] Verifies that Cypress is installed correctly and executable
    15. cache [options] Manages the Cypress binary cache
    16. info [options] Prints Cypress and system information

    最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

    这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….

    如果你不想再体验一次自学时找不到资料,没人解答问题,坚持几天便放弃的感受的话,可以加入下方我的qq群大家一起讨论交流,里面也有各种软件测试资料和技术交流。

  • 相关阅读:
    小程序内容管理系统设计
    限制价格只能输入数字,且最多两个小数
    【Kubernetes系列】Container(容器)
    AI房产户型图识别3DRender
    数据类型(面向对象)
    Spring refresh 方法分析之一
    linux安装MySql之错误
    国考省考行测:继续讲结构分析法的分总、总分、分分、分总分、总分总的真题例题讲解,错项的特点规律
    git的实际操作
    github知识小结(2022/08/07)
  • 原文地址:https://blog.csdn.net/m0_58026506/article/details/127105539