优化分批执行点击菜单
This commit is contained in:
parent
15b08c65df
commit
c8bd981cc4
@ -1,12 +1,16 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* 菜单数据服务
|
||||
* 负责菜单数据的存储和检索
|
||||
*/
|
||||
class MenuDataService {
|
||||
constructor() {
|
||||
// 从环境变量获取路径配置
|
||||
this.dataDir = process.env.TEST_DATA_DIR || path.join(process.cwd(), 'test-data');
|
||||
this.menuDataPath = process.env.MENU_DATA_FILE_PATH || path.join(this.dataDir, 'menu-data.json');
|
||||
this.progressPath = process.env.TEST_PROGRESS_FILE_PATH || path.join(this.dataDir, 'test-progress.json');
|
||||
this.dataDir = process.env.TEST_DATA_DIR;
|
||||
this.menuDataPath = process.env.MENU_DATA_FILE_PATH;
|
||||
this.progressPath = process.env.TEST_PROGRESS_FILE_PATH;
|
||||
|
||||
// 确保数据目录存在
|
||||
if (!fs.existsSync(this.dataDir)) {
|
||||
14
package.json
14
package.json
@ -9,14 +9,12 @@
|
||||
"report": "playwright show-report",
|
||||
"codegen": "playwright codegen",
|
||||
"debug": "playwright test --debug",
|
||||
"test:longi:check-normal:dev": "cross-env NODE_ENV=dev node scripts/run-tests.js",
|
||||
"test:longi:check-clean": "cross-env NODE_ENV=dev node scripts/run-tests.js --clean",
|
||||
"test:longi:check-collect": "cross-env NODE_ENV=dev node scripts/run-tests.js --collect-only",
|
||||
"test:longi:check-clean-continue": "cross-env NODE_ENV=dev node scripts/run-tests.js --clean --continue",
|
||||
"test:menu": "cross-env NODE_ENV=dev playwright test menu.spec.js",
|
||||
"test:menu:ui": "cross-env NODE_ENV=dev playwright test menu.spec.js --ui",
|
||||
"test:menu:debug": "cross-env NODE_ENV=dev playwright test menu.spec.js --debug",
|
||||
"test:menu:clean": "cross-env NODE_ENV=dev rimraf test-data/* && npm run test:menu"
|
||||
"test:menu": "cross-env NODE_ENV=dev playwright test tests/e2e/menu.spec.js",
|
||||
"test:menu:ui": "cross-env NODE_ENV=dev playwright test tests/e2e/menu.spec.js --ui",
|
||||
"test:menu:debug": "cross-env NODE_ENV=dev playwright test tests/e2e/menu.spec.js --debug",
|
||||
"test:menu:clean": "cross-env NODE_ENV=dev rimraf test-data/* && npm run test:menu",
|
||||
"test:menu:collect": "cross-env NODE_ENV=dev playwright test tests/e2e/menu.spec.js --grep \"收集菜单数据\"",
|
||||
"test:menu:batch": "cross-env NODE_ENV=dev playwright test tests/e2e/menu.spec.js --grep \"批量测试菜单\""
|
||||
},
|
||||
"keywords": [
|
||||
"playwright",
|
||||
|
||||
@ -1,26 +0,0 @@
|
||||
// #!/usr/bin/env node
|
||||
//
|
||||
// /**
|
||||
// * 龙蛟IBP系统测试运行脚本
|
||||
// */
|
||||
// const {execSync} = require('child_process');
|
||||
// const chalk = require('chalk');
|
||||
//
|
||||
// console.log(chalk.blue('开始运行龙蛟IBP系统测试...'));
|
||||
//
|
||||
// // 默认运行最简单的测试
|
||||
// const testFile = process.argv[2] || 'tests/longi-ibp/simple-login.test.js';
|
||||
//
|
||||
// // 构建命令 - 使用--project而不是--browser
|
||||
// const command = `npx playwright test ${testFile} --headed --project=chromium`;
|
||||
//
|
||||
// console.log(chalk.yellow(`运行命令: ${command}`));
|
||||
//
|
||||
// try {
|
||||
// // 执行命令
|
||||
// execSync(command, {stdio: 'inherit'});
|
||||
// console.log(chalk.green('测试完成!'));
|
||||
// } catch (error) {
|
||||
// console.error(chalk.red('测试执行失败:'), error.message);
|
||||
// process.exit(1);
|
||||
// }
|
||||
@ -1,193 +0,0 @@
|
||||
// #!/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 <browser>', '指定浏览器 (chromium, firefox, webkit)', 'chromium')
|
||||
// .option('-t, --test <pattern>', '指定测试文件或目录')
|
||||
// .option('-h, --headless', '无头模式运行', false)
|
||||
// .option('-r, --reporter <reporter>', '指定报告格式 (list, html, json)', 'list')
|
||||
// .option('-p, --parallel <number>', '并行运行数量', '3')
|
||||
// .option('-s, --screenshot', '失败时截图', false)
|
||||
// .option('-v, --video', '录制视频', false)
|
||||
// .option('-d, --debug', '调试模式', false)
|
||||
// .option('-u, --ui', '使用UI模式', false)
|
||||
// .option('-c, --config <path>', '指定配置文件路径')
|
||||
// .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);
|
||||
});
|
||||
@ -8,9 +8,9 @@ const path = require('path');
|
||||
class MenuDataService {
|
||||
constructor() {
|
||||
// 从环境变量获取路径配置
|
||||
this.dataDir = process.env.TEST_DATA_DIR;
|
||||
this.menuDataPath = process.env.MENU_DATA_FILE_PATH;
|
||||
this.progressPath = process.env.TEST_PROGRESS_FILE_PATH;
|
||||
this.dataDir = path.join(process.cwd(), process.env.TEST_DATA_DIR || 'test-data');
|
||||
this.menuDataPath = path.join(process.cwd(), process.env.MENU_DATA_FILE_PATH || 'test-data/menu-data.json');
|
||||
this.progressPath = path.join(process.cwd(), process.env.TEST_PROGRESS_FILE_PATH || 'test-data/test-progress.json');
|
||||
|
||||
// 确保数据目录存在
|
||||
if (!fs.existsSync(this.dataDir)) {
|
||||
|
||||
@ -1,3 +1,6 @@
|
||||
// 加载环境变量
|
||||
require('../../config/env');
|
||||
|
||||
const { test } = require('@playwright/test');
|
||||
const TestController = require('../../src/controllers/TestController');
|
||||
const menuDataService = require('../../src/services/MenuDataService');
|
||||
@ -7,6 +10,8 @@ test.describe('菜单可访问性测试', () => {
|
||||
|
||||
test.beforeAll(async () => {
|
||||
controller = new TestController();
|
||||
// 打印环境变量,用于调试
|
||||
console.log('BASE_URL:', process.env.BASE_URL);
|
||||
});
|
||||
|
||||
test('收集菜单数据', async () => {
|
||||
|
||||
@ -1,30 +0,0 @@
|
||||
require('../../config/env');
|
||||
const {test, expect} = require('@playwright/test');
|
||||
const LongiLoginPage = require('../pages/LongiLoginPage');
|
||||
const LongiMainPage = require('../pages/LongiMainPage');
|
||||
|
||||
// 设置更长的超时时间:1小时
|
||||
test.setTimeout(3600000);
|
||||
|
||||
test('隆基登录', async ({page}) => {
|
||||
// 1. 创建页面对象
|
||||
const loginPage = new LongiLoginPage(page);
|
||||
const mainPage = new LongiMainPage(page);
|
||||
|
||||
// 2. 导航到登录页面
|
||||
await loginPage.navigateToLoginPage();
|
||||
|
||||
// 4. 点击登录按钮 - 使用页面对象模型
|
||||
const clickSuccess = await loginPage.clickLoginButton();
|
||||
|
||||
// 5. 验证点击是否成功
|
||||
expect(clickSuccess, '验证是否登录成功').toBeTruthy();
|
||||
|
||||
// 10. 检查菜单数据文件是否存在
|
||||
let menuItems = await mainPage.checkAndLoadMenuItems();
|
||||
|
||||
// 11. 使用菜单数据进行后续操作
|
||||
console.log(`共有 ${menuItems.length} 个菜单项可用于测试`);
|
||||
|
||||
await mainPage.handleAllMenuClicks(menuItems);
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user