Playwright项目中如何让npm脚本链式执行不受前置脚本成败影响?
解决方案:无论Playwright测试结果如何都生成Allure报告
问题根源
你用;时出现No tests found错误,是因为Windows的cmd环境不把;当作命令分隔符,而是将其后面的内容作为参数传递给Playwright,导致Playwright误把npm当成测试文件的匹配规则,自然找不到测试用例。而&&仅当前置命令完全成功时才执行后续命令,不符合你的需求。
可行解决方案
方案1:用&&+||组合(跨平台兼容)
直接修改testChrome脚本,利用逻辑运算符确保无论前置测试成功或失败,都执行报告生成:
"scripts": { "testChrome": "npx playwright test --project=chrome && npm run generateReport || npm run generateReport", "generateReport": "allure generate output/allure-results -o output/allure-report --clean --single-file" }
- 逻辑:测试成功时走
&&分支执行报告;测试失败时触发||分支执行报告,覆盖所有场景。
方案2:强制用Bash执行(适合Unix-like环境或Windows下用Git Bash/WSL)
如果你的环境支持Bash,可通过bash -c强制解析命令分隔符;,确保前置命令无论结果如何都执行后续脚本:
"scripts": { "testChrome": "bash -c 'npx playwright test --project=chrome; npm run generateReport'", "generateReport": "allure generate output/allure-results -o output/allure-report --clean --single-file" }
方案3:用npm-run-all工具(更优雅的跨平台方案)
- 先安装依赖:
npm install --save-dev npm-run-all
- 修改脚本为串行执行并允许前置失败:
"scripts": { "testChrome:exec": "npx playwright test --project=chrome", "testChrome": "run-s --continue-on-error testChrome:exec generateReport", "generateReport": "allure generate output/allure-results -o output/allure-report --clean --single-file" }
run-s表示串行执行脚本;--continue-on-error参数确保即使testChrome:exec失败,仍继续执行generateReport。
内容的提问来源于stack exchange,提问作者Irshad Telsang
相关产品推荐
相关产品推荐

