在Docker中运行Cypress且使用多命令脚本时如何传递参数
1. 默认传参的作用范围
你直接执行docker run <镜像名> test -- <参数>时,默认传入的参数只会作用于test脚本对应的顶层shell命令,不会自动透传给内部调用的npm run scripts,更不会作用到npm run posttest。
2. 可行的传参实现方案
步骤1:修改package.json脚本配置
首先调整两处配置:
- 把原Windows格式的清理命令换成Linux兼容版本(Docker容器一般为Linux环境,原
rmdir命令会执行失败) - 给
test脚本中的npm run scripts增加透传参数的--标记
修改后的scripts字段参考:
"scripts": { "clean:reports": "rm -rf cypress/reports && mkdir -p cypress/reports/mochareports", "pretest": "npm run clean:reports", "scripts": "cypress run", "combine-reports": "mochawesome-merge cypress/reports/mocha/*.json > cypress/reports/mochareports/report.json", "generate-report": "marge cypress/reports/mochareports/report.json -f report -o cypress/reports/mochareports", "posttest": "npm run combine-reports && npm run generate-report", "test" : "npm run scripts -- $* || npm run posttest" },
提示:如果传入的参数包含空格(比如测试文件路径带空格),可以把test脚本中的
$*替换为"$@",避免参数被截断。
步骤2:执行docker run时传参
因为你的ENTRYPOINT已经固定为["npm","run"],所以执行命令时直接在test后面加--后跟要传给Cypress的参数即可,示例:
docker run <你的镜像名称> test -- --spec cypress/e2e/example.cy.js
3. 参数作用范围说明
上述配置下,你传入的--spec等参数只会作用于npm run scripts对应的cypress run命令,不会传递给后面的npm run posttest,不会影响报告合并生成逻辑。
内容的提问来源于stack exchange,提问作者jprealini
相关产品推荐
相关产品推荐

