第一次

This commit is contained in:
dengqichen 2025-03-06 09:17:58 +08:00
parent 2589f3479b
commit 67149da27a
2 changed files with 48 additions and 32 deletions

View File

@ -32,5 +32,5 @@ test('隆基登录', async ({page}) => {
// 11. 使用菜单数据进行后续操作 // 11. 使用菜单数据进行后续操作
console.log(`共有 ${menuItems.length} 个菜单项可用于测试`); console.log(`共有 ${menuItems.length} 个菜单项可用于测试`);
await mainPage.handleAllMenus(menuItems); await mainPage.handleAllMenuClicks(menuItems);
}); });

View File

@ -481,9 +481,9 @@ class LongiMainPage extends BasePage {
} }
} }
async handleAllMenus(menuItems) { async handleAllMenuClicks(menuItems) {
for (let i = 0; i < menuItems.length; i++) { for (let i = 0; i < menuItems.length; i++) {
await this.handleSingleMenu(menuItems[i]); await this.handleSingleMenuClick(menuItems[i]);
} }
} }
@ -547,21 +547,43 @@ class LongiMainPage extends BasePage {
} }
} }
async handleSingleMenu(menu) { async handleSingleMenuClick(menu) {
await this.expandSideMenu(); try {
// 在处理菜单前重新获取最新的element await this.expandSideMenu();
menu = await this.restoreMenuElement(menu); // 在处理菜单前重新获取最新的element
menu = await this.restoreMenuElement(menu);
if (!menu.element) { if (!menu.element) {
console.error(`无法找到菜单项 "${menu.text}" 的element跳过处理`); console.error(`无法找到菜单项 "${menu.text}" 的element跳过处理`);
return; return;
} }
if (menu.hasThirdMenu) { if (menu.hasThirdMenu) {
await this.handleThreeLevelMenu(menu); await this.handleThreeLevelMenu(menu);
} else { } else {
// 处理二级菜单点击 // 处理二级菜单点击
await this.handleMenuClick(menu); await this.handleMenuClick(menu);
}
// 执行内存回收
await this.page.evaluate(() => {
// 清理DOM中的临时元素
const cleanup = () => {
const elements = document.querySelectorAll('.el-loading-mask, .el-message, .el-message-box');
elements.forEach(el => el.remove());
// 如果浏览器支持手动垃圾回收,则执行
if (window.gc) {
window.gc();
}
};
cleanup();
});
console.log(`🧹 执行内存回收 - ${menu.text}`);
} catch (error) {
console.error(`处理菜单失败 [${menu.text}]:`, error.message);
throw error;
} }
} }
@ -573,7 +595,6 @@ class LongiMainPage extends BasePage {
*/ */
async waitForPageLoadWithRetry(menu, subMenuText = '') { async waitForPageLoadWithRetry(menu, subMenuText = '') {
const pageName = subMenuText ? `${menu.text} > ${subMenuText}` : menu.text; const pageName = subMenuText ? `${menu.text} > ${subMenuText}` : menu.text;
const pageStartTime = Date.now();
console.log(`等待页面 ${pageName} 数据加载...`); console.log(`等待页面 ${pageName} 数据加载...`);
const config = { const config = {
@ -714,7 +735,7 @@ class LongiMainPage extends BasePage {
// 这里可以根据需要处理三级菜单项 // 这里可以根据需要处理三级菜单项
for (const thirdMenu of thirdMenus) { for (const thirdMenu of thirdMenus) {
await this.handleMenuClick(thirdMenu, menu.text); await this.handleMenuClick(thirdMenu, menu);
// 如果不是最后一个菜单项,需要重新点击父菜单展开三级菜单 // 如果不是最后一个菜单项,需要重新点击父菜单展开三级菜单
if (thirdMenu !== thirdMenus[thirdMenus.length - 1]) { if (thirdMenu !== thirdMenus[thirdMenus.length - 1]) {
@ -732,28 +753,23 @@ class LongiMainPage extends BasePage {
/** /**
* 通用的菜单点击和页面加载处理方法 * 通用的菜单点击和页面加载处理方法
* @param {Object} menuInfo 菜单信息对象包含text属性 * @param {Object} menuInfo 菜单信息对象包含text属性
* @param {string} parentText 父级菜单文本可选 * @param {Object} parentMenu 父级菜单可选
* @returns {Promise<boolean>} 处理是否成功 * @returns {Promise<boolean>} 处理是否成功
*/ */
async handleMenuClick(menuInfo, parentText = '') { async handleMenuClick(menuInfo, parentMenu) {
try { try {
const menuPath = parentText ? `${parentText} > ${menuInfo.text}` : menuInfo.text; const menuPath = parentMenu.text ? `${parentMenu.text} > ${menuInfo.text}` : menuInfo.text;
console.log(`🔸 点击菜单: ${menuPath}`); console.log(`🔸 点击菜单: ${menuPath}`);
await menuInfo.element.click(); await menuInfo.element.click();
// 等待页面加载 // 等待页面加载
const loadResult = await this.waitForPageLoadWithRetry(menuInfo); const loadResult = await this.waitForPageLoadWithRetry(menuInfo);
if (!loadResult) { if (loadResult) {
console.warn('页面加载失败'); // 处理所有TAB页
return false; await this.handleAllTabs(menuInfo);
// 关闭标签页
await this.closeActiveTab(menuPath);
} }
// 处理所有TAB页
await this.handleAllTabs(menuInfo);
// 关闭标签页
await this.closeActiveTab(menuPath);
return true; return true;
} catch (error) { } catch (error) {
console.error(`处理菜单点击失败 [${menuInfo.text}]:`, error.message); console.error(`处理菜单点击失败 [${menuInfo.text}]:`, error.message);