WebdriverIO如何先运行单个测试再并行执行其余测试?
WebdriverIO 实现先单独运行指定测试文件再并行执行其余文件的方案
有两种可行的实现方式,具体如下:
方法一:命令行组合执行
直接通过终端命令串联两个执行步骤,先单独跑目标文件,再并行执行剩余文件:
- 单独运行
a.test.ts:
wdio run ./wdio.conf.ts --spec ./path/to/a.test.ts
- 并行执行
b.test.ts、c.test.ts、d.test.ts:
wdio run ./wdio.conf.ts --spec ./path/to/b.test.ts ./path/to/c.test.ts ./path/to/d.test.ts
可以将两个命令用&&串联,一次性完成整个流程:
wdio run ./wdio.conf.ts --spec ./path/to/a.test.ts && wdio run ./wdio.conf.ts --spec ./path/to/b.test.ts ./path/to/c.test.ts ./path/to/d.test.ts
注意:需要确保你的wdio.conf.ts配置文件中,maxInstances参数设置为3或更高,这样三个文件才能真正并行执行。
方法二:Node.js脚本封装执行逻辑
如果需要更灵活的流程控制,可以写一个自定义脚本调用WebdriverIO的CLI API,按顺序执行测试:
创建run-tests.js文件,内容如下:
const { run } = require('@wdio/cli'); async function runTests() { // 先执行单独的测试文件 console.log('开始执行a.test.ts...'); await run({ configPath: './wdio.conf.ts', specs: ['./path/to/a.test.ts'] }); // 再并行执行剩余文件 console.log('开始并行执行b.test.ts、c.test.ts、d.test.ts...'); await run({ configPath: './wdio.conf.ts', specs: ['./path/to/b.test.ts', './path/to/c.test.ts', './path/to/d.test.ts'], maxInstances: 3 // 临时指定并行实例数,确保三个文件同时运行 }); } runTests().catch(err => { console.error('测试执行失败:', err); process.exit(1); });
在终端运行该脚本即可:
node run-tests.js
如果使用TypeScript,可将脚本改为.ts格式,用ts-node运行:
ts-node run-tests.ts
内容的提问来源于stack exchange,提问作者Abhisar Tripathi
相关产品推荐
相关产品推荐

