From 2589f3479bee079aa45e191cb253951f2ec1ed03 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Wed, 5 Mar 2025 18:21:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/longi-ibp/check-page-normal.test.js | 3 + tests/pages/LongiMainPage.js | 120 ++++++---------------- 2 files changed, 34 insertions(+), 89 deletions(-) diff --git a/tests/longi-ibp/check-page-normal.test.js b/tests/longi-ibp/check-page-normal.test.js index 2dfaf18..b0af91a 100644 --- a/tests/longi-ibp/check-page-normal.test.js +++ b/tests/longi-ibp/check-page-normal.test.js @@ -3,6 +3,9 @@ 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); diff --git a/tests/pages/LongiMainPage.js b/tests/pages/LongiMainPage.js index 96eed3d..6de23da 100644 --- a/tests/pages/LongiMainPage.js +++ b/tests/pages/LongiMainPage.js @@ -565,6 +565,12 @@ class LongiMainPage extends BasePage { } } + /** + * 等待页面加载完成,带重试机制 + * @param {Object} menu 菜单对象 + * @param {string} subMenuText 子菜单文本(可选) + * @returns {Promise} 页面是否加载成功 + */ async waitForPageLoadWithRetry(menu, subMenuText = '') { const pageName = subMenuText ? `${menu.text} > ${subMenuText}` : menu.text; const pageStartTime = Date.now(); @@ -572,20 +578,14 @@ class LongiMainPage extends BasePage { const config = { maxRetries: 30, - retryInterval: 500 + retryInterval: 500, + stabilityDelay: 1000 }; let retryCount = 0; - const check = async () => { - try { - // 如果超过最大重试次数,记录失败并继续 - if (retryCount >= config.maxRetries) { - const reason = `页面加载超时 (${config.maxRetries} 次重试)`; - this.recordFailedPage(pageName, reason, pageStartTime); - return {success: false, error: reason}; - } - + try { + while (retryCount < config.maxRetries) { // 检查页面状态 const selectors = { loadingMask: '.el-loading-mask', @@ -598,71 +598,34 @@ class LongiMainPage extends BasePage { const elements = await this.page.locator(selector).all(); if (elements.length > 0 && key !== 'loadingMask') { const errorText = await elements[0].textContent(); - const reason = `页面加载出现错误: ${errorText}`; - this.recordFailedPage(pageName, reason, pageStartTime); - return {success: false, error: reason}; + console.error(`页面加载出现错误: ${pageName}, 错误信息: ${errorText}`); + return false; } } // 检查加载状态 const loadingMasks = await this.page.locator(selectors.loadingMask).all(); - if (loadingMasks.length > 0) { - retryCount++; - await this.page.waitForTimeout(config.retryInterval); - return await check(); + if (loadingMasks.length === 0) { + // 加载完成,等待页面稳定 + await this.page.waitForTimeout(config.stabilityDelay); + console.log(`✅ 页面 ${pageName} 加载完成`); + return true; } - // 加载完成,等待页面稳定 - await this.page.waitForTimeout(1000); - return {success: true, error: null}; - - } catch (error) { - // 如果是页面关闭错误,直接返回 - if (error.message.includes('Target page, context or browser has been closed')) { - return {success: false, error: '页面已关闭'}; - } - - // 其他错误继续重试 + // 继续等待 retryCount++; await this.page.waitForTimeout(config.retryInterval); - return await check(); } - }; - return await check(); - } + // 超时处理 + console.error(`页面加载超时: ${pageName}, 重试次数: ${config.maxRetries}`); + return false; -// 添加记录失败页面的方法 - recordFailedPage(pageName, reason, startTime) { - const duration = Date.now() - startTime; - console.error(`页面加载失败: ${pageName}, 原因: ${reason}, 耗时: ${duration}ms`); - // 这里可以添加将失败信息写入文件的逻辑 - } - - /** - * 获取二级菜单元素 - * @private - */ - async getSecondLevelMenu(menu) { - const menuElement = await this.page.locator(this.selectors.menuItems).nth(menu.index); - await menuElement.click(); - await this.page.waitForTimeout(500); // 减少等待时间 - return menuElement; - } - - /** - * 获取三级菜单项 - * @private - */ - async getThirdLevelMenuItems(menuElement) { - const thirdMenuItems = await this.page.locator(this.selectors.thirdLevelMenu).all(); - const thirdMenuTexts = await Promise.all( - thirdMenuItems.map(item => item.textContent()) - ); - return { - thirdMenuTexts: thirdMenuTexts.map(text => text.trim()), - totalItems: thirdMenuItems.length - }; + } catch (error) { + // 任何错误都记录并返回false + console.error(`页面加载出错: ${pageName}, 错误信息: ${error.message}`); + return false; + } } /** @@ -673,7 +636,6 @@ class LongiMainPage extends BasePage { async closeActiveTab(pageName) { try { console.log(`🗑️ 正在关闭页面 "${pageName}" 的tab...`); - // 检查是否存在活动的tab和关闭按钮 const activeTab = this.page.locator('.vab-tabs .el-tabs--card .el-tabs__item.is-active'); const closeButton = activeTab.locator('.el-icon.is-icon-close'); @@ -709,11 +671,9 @@ class LongiMainPage extends BasePage { try { // 检查三级菜单是否存在 const elements = await this.page.locator('.el-popper.is-light.el-popover .menuTitle.canClick').all(); - if (elements.length === 0) { return []; } - // 收集所有三级菜单项 const thirdMenus = []; for (let i = 0; i < elements.length; i++) { @@ -752,18 +712,7 @@ class LongiMainPage extends BasePage { // 获取三级菜单列表 const thirdMenus = await this.getThirdLevelMenus(menu); - if (thirdMenus.length === 0) { - console.log(`⚠️ ${menu.text} 没有找到三级菜单项`); - return; - } - - console.log(`✓ ${menu.text} 找到 ${thirdMenus.length} 个三级菜单项:`); - thirdMenus.forEach(item => { - console.log(` - ${item.text}`); - }); - // 这里可以根据需要处理三级菜单项 - // 比如点击每个三级菜单项并等待页面加载 for (const thirdMenu of thirdMenus) { await this.handleMenuClick(thirdMenu, menu.text); @@ -791,19 +740,12 @@ class LongiMainPage extends BasePage { const menuPath = parentText ? `${parentText} > ${menuInfo.text}` : menuInfo.text; console.log(`🔸 点击菜单: ${menuPath}`); - // 点击菜单项 - if (menuInfo.element) { - await menuInfo.element.click(); - } else { - // 如果没有element属性,尝试通过文本定位 - const menuLocator = this.page.locator(`text=${menuInfo.text}`).first(); - await menuLocator.click(); - } + await menuInfo.element.click(); // 等待页面加载 const loadResult = await this.waitForPageLoadWithRetry(menuInfo); - if (!loadResult.success) { - console.warn(loadResult.error); + if (!loadResult) { + console.warn('页面加载失败'); return false; } @@ -836,8 +778,8 @@ class LongiMainPage extends BasePage { // 等待页面加载 const loadResult = await this.waitForPageLoadWithRetry(parentMenu, tabInfo.text); - if (!loadResult.success) { - console.warn(`TAB页 ${tabInfo.text} 加载失败: ${loadResult.error}`); + if (!loadResult) { + console.warn('页面加载失败'); return false; }