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,或掃描下面的二維碼添加關注:

作者: Surpassme
來源: http://www.jianshu.com/u/28161b7c9995/
http://www.cnblogs.com/surpassme/
宣告:本文著作權歸作者所有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出 原文鏈接 ,否則保留追究法律責任的權利,如有問題,可發送郵件 聯系,讓我們尊重原創者著作權,共同營造良好的IT朋友圈,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/508792.html
標籤:其他
