Playwright-Cucumber-Javascript:NPX运行Feature文件报错求助
问题解决思路
核心原因
npx playwright tests:chrome报错是因为**tests:chrome不是Playwright CLI的内置命令**,它应该是你在package.json的scripts中自定义的脚本命令,必须通过npm run来调用,而不是直接用npx playwright执行。
两种解决方案
方案一:用npm run执行自定义脚本
- 先检查你的
package.json的scripts段,确认是否存在tests:chrome命令,示例如下:
"scripts": { "tests:cucumber": "cucumber-js --config cucumber.js", "tests:chrome": "playwright test --project=chrome" }
- 如果存在,直接用
npm run执行并传递参数:
npm run tests:chrome -- --grep @POC
注意:
--是用来将后续参数传递给脚本内部的命令,确保--grep @POC能被Playwright识别。
方案二:直接用Playwright CLI指定Chrome运行
如果你的package.json里没有定义tests:chrome脚本,想直接通过Playwright CLI指定Chrome浏览器运行Cucumber测试:
- 检查
playwright.config.ts(或.js)中是否配置了Chrome项目,示例如下:
export default defineConfig({ projects: [ { name: 'chrome', use: { ...devices['Desktop Chrome'] }, }, ], testMatch: ['**/*.feature'], // 确保匹配Cucumber的feature文件 });
- 直接执行Playwright CLI命令:
npx playwright test --project=chrome --grep @POC
验证逻辑
你能正常运行npm run tests:cucumber -- --tags "@MyTag",正是因为tests:cucumber是package.json中定义的自定义脚本,npm run会正确解析并执行脚本内容,而npx playwright只能识别Playwright官方内置的CLI命令(比如test、codegen等)。
内容的提问来源于stack exchange,提问作者nosequeweaponer
相关产品推荐
相关产品推荐

