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上产生标识


    常见用法示例如下所示:

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

    可被指定的浏览器有chromechromiumedgeelectronfirefox

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

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

  • 指定测试报告格式
cypress run --reporter json
cypress run --reporter junit --reporter-options mochaFile=result.xml,toConsole=true
  • 指定运行的测试文件
cypress run --spec "cypress\integration\3-Surpass-Test-Examples\testSelect\test.visit.local.file.js"
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"
cypress run --spec "cypress\integration\3-Surpass-Test-Examples\testSelect\*.js"
cypress run --spec "cypress\integration\3-Surpass-Test-Examples\testSelect\*"

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

    运行结果如下所示:

cypress调试 cypress run_测试工程师

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的运行环境,如下所示:

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

    运行的结果如下图所示:




cypress调试 cypress run_自动化测试_02


6.4 cypress verify

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

C:\Users\admin\Documents\CypressProjects>cypress verify

✔  Verified Cypress! C:\Users\admin\AppData\Local\Cypress\Cache\9.5.4\Cypress

6.5 cypress version

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

C:\Users\admin\Documents\CypressProjects>cypress verify

✔  Verified Cypress! C:\Users\admin\AppData\Local\Cypress\Cache\9.5.4\Cypress

C:\Users\admin\Documents\CypressProjects>cypress version
Cypress package version: 9.5.4
Cypress binary version: 9.5.4
Electron version: 15.3.5
Bundled Node version:
16.5.0

6.5 cypress help

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

C:\Users\admin\Documents\CypressProjects>cypress help
Usage: cypress <command> [options]

Options:
  -v, --version      prints Cypress version
  -h, --help         display help for command

Commands:
  help               Shows CLI help and exits
  version            prints Cypress version
  open [options]     Opens Cypress in the interactive GUI.
  run [options]      Runs Cypress tests from the CLI without the GUI
  open-ct [options]  Opens Cypress component testing interactive mode.
  run-ct [options]   Runs all Cypress Component Testing suites
  install [options]  Installs the Cypress executable matching this package's version
  verify [options]   Verifies that Cypress is installed correctly and executable
  cache [options]    Manages the Cypress binary cache
  info [options]     Prints Cypress and system information