取消Playwright配置webserver段后Windows下出现“No tests”问题
Angular + Playwright 测试异常问题(仅Windows系统)
仅Windows系统会触发该问题,Ubuntu系统可正常运行。
给Angular应用配置Playwright测试时遇到如下异常:
- 当
playwright.config.ts中默认注释的webserver配置段保持注释状态时,Playwright能正常识别并执行测试; - 取消该配置段注释,将URL修改为Angular默认的4200端口后,执行
npx playwright test --ui会卡顿约半分钟,最终显示“No tests”。
复现步骤
- 执行
ng new angular_e2e创建新Angular项目 - 执行
npm init playwright@latest初始化Playwright - 运行
npx playwright test --ui,可正常打开Playwright UI并显示示例测试 - 编辑
playwright.config.ts,取消webserver配置段的注释,将url字段改为http://localhost:4200 - 再次运行
npx playwright test --ui,卡顿半分钟后显示“No tests”
解决方法
- 修正webserver启动命令
默认的webserver配置中command可能未匹配Angular的启动命令,需将其设置为npm run start或直接ng serve,同时确保端口与URL一致:
webserver: { command: 'npm run start', url: 'http://localhost:4200', reuseExistingServer: !process.env.CI, }
排查端口占用
用命令netstat -ano | findstr :4200检查4200端口是否被其他进程占用,若有则终止对应进程,或修改Angular启动端口。延长webserver超时时间
Angular项目首次启动可能较慢,可在webserver配置中添加超时设置,避免因启动超时导致测试无法被识别:
webserver: { command: 'npm run start', url: 'http://localhost:4200', timeout: 120000, // 延长至120秒 reuseExistingServer: !process.env.CI, }
- 确认测试文件路径配置
检查playwright.config.ts中的testDir字段,确保其指向存放测试文件的目录(默认是tests/),若测试文件放在其他目录需同步修改该配置。
内容的提问来源于stack exchange,提问作者Ya.
相关产品推荐
相关产品推荐

