// #!/usr/bin/env node // // /** // * 测试运行脚本 // * 提供命令行界面来运行测试 // */ // const {program} = require('commander'); // const {execSync} = require('child_process'); // const chalk = require('chalk'); // const path = require('path'); // const fs = require('fs'); // // // 确保在其他任何代码之前加载环境变量 // require('../config/env'); // // const TestController = require('../controllers/TestController'); // const menuDataService = require('../services/MenuDataService'); // // // 设置命令行选项 // program // .version('1.0.0') // .description('Playwright 自动化测试运行工具') // .option('-b, --browser ', '指定浏览器 (chromium, firefox, webkit)', 'chromium') // .option('-t, --test ', '指定测试文件或目录') // .option('-h, --headless', '无头模式运行', false) // .option('-r, --reporter ', '指定报告格式 (list, html, json)', 'list') // .option('-p, --parallel ', '并行运行数量', '3') // .option('-s, --screenshot', '失败时截图', false) // .option('-v, --video', '录制视频', false) // .option('-d, --debug', '调试模式', false) // .option('-u, --ui', '使用UI模式', false) // .option('-c, --config ', '指定配置文件路径') // .parse(process.argv); // // const options = program.opts(); // // // 构建命令 // let command = 'npx playwright test'; // // // 添加测试文件或目录 // if (options.test) { // command += ` "${options.test}"`; // } // // // 添加浏览器 // command += ` --project=${options.browser}`; // // // 添加报告格式 // if (options.reporter) { // command += ` --reporter=${options.reporter}`; // } // // // 添加并行数量 // if (options.parallel) { // command += ` --workers=${options.parallel}`; // } // // // 添加截图选项 // if (options.screenshot) { // command += ' --screenshot=on'; // } // // // 添加视频选项 // if (options.video) { // command += ' --video=on'; // } // // // 添加无头模式选项 // if (!options.headless) { // command += ' --headed'; // } // // // 添加调试模式 // if (options.debug) { // command += ' --debug'; // } // // // 添加UI模式 // if (options.ui) { // command = 'npx playwright test --ui'; // } // // // 添加配置文件 // if (options.config) { // command += ` --config="${options.config}"`; // } // // console.log(chalk.blue('运行命令:'), chalk.yellow(command)); // // try { // // 执行命令 // execSync(command, {stdio: 'inherit'}); // console.log(chalk.green('测试完成!')); // } catch (error) { // console.error(chalk.red('测试执行失败:'), error.message); // process.exit(1); // } // 确保在其他任何代码之前加载环境变量 require('../config/env'); const TestController = require('../controllers/TestController'); const menuDataService = require('../services/MenuDataService'); /** * 格式化进度信息 * @param {Object} progress - 进度对象 * @returns {string} - 格式化的进度字符串 */ function formatProgress(progress) { const percentage = ((progress.completed / progress.total) * 100).toFixed(2); return `进度: ${progress.completed}/${progress.total} (${percentage}%)`; } /** * 主执行函数 */ async function main() { console.log('环境变量:', { NODE_ENV: process.env.NODE_ENV, BASE_URL: process.env.BASE_URL, MENU_DATA_FILE_PATH: process.env.MENU_DATA_FILE_PATH }); const controller = new TestController(); // 处理命令行参数 const args = process.argv.slice(2); if (args.includes('--clean')) { menuDataService.clearAll(); console.log('已清理所有数据'); if (!args.includes('--continue')) { return; } } // 强制重新收集菜单数据 console.log('开始收集菜单数据...'); const menuData = await controller.collectMenuData(); console.log(`成功收集 ${menuData.length} 个菜单项`); // 清理之前的进度 menuDataService.saveProgress([]); // 显示初始进度 const initialProgress = controller.getTestProgress(); console.log('\n初始' + formatProgress(initialProgress)); if (args.includes('--collect-only')) { console.log('仅收集菜单数据,退出执行'); return; } // 执行测试 console.log('\n开始执行测试...\n'); let retryCount = 0; const maxRetries = 3; while (true) { const batch = controller.getNextBatch(); if (!batch || batch.length === 0) { const finalProgress = controller.getTestProgress(); // 检查是否所有菜单都测试完成 if (finalProgress.completed < finalProgress.total && retryCount < maxRetries) { console.log(`\n还有未完成的测试,尝试重试 (${retryCount + 1}/${maxRetries})...`); retryCount++; continue; } console.log('\n所有测试完成!'); console.log('最终' + formatProgress(finalProgress)); break; } // 显示当前进度 const currentProgress = controller.getTestProgress(); console.log('\n当前' + formatProgress(currentProgress)); // 执行当前批次 await controller.runBatchTest(batch); // 批次间暂停 console.log(`\n等待 ${controller.batchInterval/1000} 秒后继续下一批次...\n`); await new Promise(resolve => setTimeout(resolve, controller.batchInterval)); } } // 执行主函数 main().catch(error => { console.error('执行过程中出现错误:', error); process.exit(1); });