diff --git a/cypress.config.js b/cypress.config.js index 82dffc6..09d4150 100644 --- a/cypress.config.js +++ b/cypress.config.js @@ -26,7 +26,7 @@ module.exports = { '--disable-backgrounding-occluded-windows', '--memory-pressure-off', '--disable-site-isolation-trials', - '--js-flags="--max-old-space-size=8192"', + '--js-flags="--max-old-space-size=4096"', '--disable-gpu', '--disable-notifications', '--disable-extensions', diff --git a/cypress/e2e/需求计划页面是否可用.cy.js b/cypress/e2e/需求计划页面是否可用.cy.js index f747c1a..6e393ec 100644 --- a/cypress/e2e/需求计划页面是否可用.cy.js +++ b/cypress/e2e/需求计划页面是否可用.cy.js @@ -7,6 +7,11 @@ describe('隆基系统全部页面验证', () => { }); }); + // 每个测试用例结束后清理内存 + afterEach(() => { + cleanupMemory(); + }); + // 格式化时间的辅助函数 const formatTime = (date) => { return date.toLocaleString('zh-CN', { @@ -154,12 +159,6 @@ describe('隆基系统全部页面验证', () => { } }; - // 每个测试用例前不再重新访问登录页 - // beforeEach(() => { - // // 只设置异常处理 - // Cypress.on('uncaught:exception', (err, runnable) => false); - // }); - // 添加内存清理的公共方法 const cleanupMemory = () => { // 执行垃圾回收 @@ -167,20 +166,22 @@ describe('隆基系统全部页面验证', () => { // 清理DOM快照 cy.get('body').then(() => { - Cypress.$(document).find('*').off(); - Cypress.$('.el-loading-mask').remove(); - Cypress.$('.el-message').remove(); - Cypress.$('.el-message-box').remove(); - - // 清理可能的内存泄漏 - Cypress.$('iframe').remove(); // 移除所有iframe - Cypress.$('img').remove(); // 移除所有图片 - Cypress.$('video').remove(); // 移除所有视频 - - // 清理事件监听器 - const win = Cypress.$(window); - win.off(); - win.find('*').off(); + try { + // 移除一些可能导致内存泄漏的元素 + Cypress.$('.el-loading-mask').remove(); + Cypress.$('.el-message').remove(); + Cypress.$('.el-message-box').remove(); + + // 清理事件监听器 - 只清理特定元素的事件 + Cypress.$('.el-dialog__wrapper').off(); + Cypress.$('.el-notification').off(); + Cypress.$('.el-drawer__wrapper').off(); + + // 清理控制台 + console.clear(); + } catch (e) { + console.error('清理DOM时出错:', e); + } }); // 手动清理一些可能的引用 @@ -213,20 +214,29 @@ describe('隆基系统全部页面验证', () => { // 添加报告写入方法 const writeReportBatch = () => { - const batchNumber = Math.floor(testReport.summary.totalPages / 20); // 每20个页面写入一次 - const fileName = `cypress/reports/${selectedEnvironment}-menu-test-report-batch-${batchNumber}.json`; + if (testReport.currentBatch.pages.length === 0) { + return; + } + const batchNumber = Math.floor(Math.random() * 10000); + cy.log(`📊 写入批次报告 ${batchNumber}:`); cy.log(`当前批次页面数: ${testReport.currentBatch.pages.length}`); cy.log(`当前批次失败页面数: ${testReport.currentBatch.failedPages.length}`); - - cy.writeFile(fileName, testReport.currentBatch).then(() => { - cy.log(`✅ 批次报告已写入: ${fileName}`); - // 清空当前批次数据,释放内存 + + cy.task('saveReport', { + data: testReport.currentBatch, + filename: `${selectedEnvironment}-menu-test-report-batch-${batchNumber}.json` + }).then(() => { + cy.log(`✅ 批次报告已写入`); + + // 清空当前批次 testReport.currentBatch = { pages: [], failedPages: [] }; + + // 清理内存 cleanupMemory(); }); }; @@ -681,6 +691,7 @@ describe('隆基系统全部页面验证', () => { // 开始测试的函数 function startTesting(selectedMenus) { let processedCount = 0; + const batchSize = 10; // 每批处理10个菜单项 const processMenuItems = (index = 0) => { if (index >= selectedMenus.length) { @@ -700,12 +711,12 @@ describe('隆基系统全部页面验证', () => { }).replace(/[\/\s:]/g, ''); // 创建以时间戳命名的报告目录 - const reportDir = `cypress/reports/report-${timestamp}`; + const reportDir = `cypress/reports/${selectedEnvironment}-report-${timestamp}`; // 创建报告目录并生成最终报告 cy.task('createReportDir', reportDir).then(() => { // 将摘要信息写入到时间戳目录中 - cy.writeFile(`${reportDir}/test-summary-${timestamp}.json`, testReport.summary).then(() => { + cy.writeFile(`${reportDir}/${selectedEnvironment}-test-summary-${timestamp}.json`, testReport.summary).then(() => { generateFinalReport(reportDir, timestamp, selectedEnvironment); }); }); @@ -716,6 +727,31 @@ describe('隆基系统全部页面验证', () => { writeReportBatch(); } + // 每处理一定数量的菜单项,重新加载页面以释放内存 + if (index > 0 && index % batchSize === 0) { + cy.log(`已处理 ${index} 个菜单项,重新加载页面以释放内存...`); + + // 保存当前进度 + writeReportBatch(); + + // 清理内存 + cleanupMemory(); + + // 重新加载页面 + cy.reload(); + + // 等待页面加载完成 + cy.get('.ly-side-nav', { timeout: 30000 }).should('be.visible'); + + // 确保菜单展开 + cy.get('.ly-side-nav').then($nav => { + if ($nav.css('left') !== '0px') { + cy.get('.vab-content .toggle-icon').click(); + } + }); + } + + // 每处理3个菜单项清理一次内存 if (index > 0 && index % 3 === 0) { cleanupMemory(); } @@ -756,11 +792,6 @@ describe('隆基系统全部页面验证', () => { checkTestStarted(); }); - // 在每个测试用例结束后清理内存 - afterEach(() => { - cleanupMemory(); - }); - // 修改生成HTML报告的方法,接收环境参数 const generateFinalReport = (reportDir, timestamp, environment) => { // 读取所有批次报告