import { execSync } from 'child_process'; import * as fs from 'fs'; interface SelectedTests { smoke: string[]; functional: string[]; edge: string[]; } export class TestExecutor { /** * 执行智能选择的测试 */ async runSelectedTests(testsFile: string): Promise { const selectedTests: SelectedTests = JSON.parse( fs.readFileSync(testsFile, 'utf-8') ); console.log('=== 开始执行智能测试 ===\n'); // 1. 执行冒烟测试(优先级最高) if (selectedTests.smoke.length > 0) { console.log('📦 执行冒烟测试...'); await this.runTests(selectedTests.smoke, 'smoke'); } // 2. 执行功能测试 if (selectedTests.functional.length > 0) { console.log('📦 执行功能测试...'); await this.runTests(selectedTests.functional, 'functional'); } // 3. 执行边缘场景测试(可选) if (selectedTests.edge.length > 0 && process.env.RUN_EDGE_TESTS === 'true') { console.log('📦 执行边缘场景测试...'); await this.runTests(selectedTests.edge, 'edge'); } console.log('\n✅ 智能测试执行完成'); } /** * 执行指定测试用例 */ private async runTests( testPatterns: string[], level: string ): Promise { for (const pattern of testPatterns) { try { console.log(` 执行: ${pattern}`); execSync( `npx playwright test "${pattern}" --project=chromium --reporter=html`, { stdio: 'inherit', env: { ...process.env, TEST_LEVEL: level, }, } ); } catch (error) { console.error(` ❌ 测试失败: ${pattern}`); // 继续执行其他测试 } } } /** * 执行全量测试 */ async runAllTests(): Promise { console.log('=== 开始执行全量测试 ===\n'); execSync('npm run test:e2e', { stdio: 'inherit', }); console.log('\n✅ 全量测试执行完成'); } } // 主函数 async function main() { const executor = new TestExecutor(); const testsFile = process.argv[2] || 'selected-tests.json'; if (fs.existsSync(testsFile)) { await executor.runSelectedTests(testsFile); } else { await executor.runAllTests(); } } main().catch(console.error);