6 命令行運行Cypress
Cypress命令行的運行基本語法格式如下所示:
cypress <command> [options]
command代表運行的命令,是必選參數。支持的命令有:open、run、install、verify、cache、version、help。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
可被指定的瀏覽器有chrome、chromium、edge、electron、firefox。
- 添加配置項
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\*"
一次運行多個測試文件,使用逗號隔開
運行結果如下所示:
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是否正確安裝且能運行。如下所示:
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
原文地址:https://www.jianshu.com/p/a727f3905b60
本文同步在微信訂閱號上發佈,如各位小夥伴們喜歡我的文章,也可以關注我的微信訂閱號:woaitest,或掃描下面的二維碼添加關注: