第一次

This commit is contained in:
dengqichen 2025-03-05 18:21:48 +08:00
parent 1111e06aaa
commit 2589f3479b
2 changed files with 34 additions and 89 deletions

View File

@ -3,6 +3,9 @@ const {test, expect} = require('@playwright/test');
const LongiLoginPage = require('../pages/LongiLoginPage'); const LongiLoginPage = require('../pages/LongiLoginPage');
const LongiMainPage = require('../pages/LongiMainPage'); const LongiMainPage = require('../pages/LongiMainPage');
// 设置更长的超时时间1小时
test.setTimeout(3600000);
test('隆基登录', async ({page}) => { test('隆基登录', async ({page}) => {
// 1. 创建页面对象 // 1. 创建页面对象
const loginPage = new LongiLoginPage(page); const loginPage = new LongiLoginPage(page);

View File

@ -565,6 +565,12 @@ class LongiMainPage extends BasePage {
} }
} }
/**
* 等待页面加载完成带重试机制
* @param {Object} menu 菜单对象
* @param {string} subMenuText 子菜单文本可选
* @returns {Promise<boolean>} 页面是否加载成功
*/
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(); const pageStartTime = Date.now();
@ -572,20 +578,14 @@ class LongiMainPage extends BasePage {
const config = { const config = {
maxRetries: 30, maxRetries: 30,
retryInterval: 500 retryInterval: 500,
stabilityDelay: 1000
}; };
let retryCount = 0; let retryCount = 0;
const check = async () => { try {
try { while (retryCount < config.maxRetries) {
// 如果超过最大重试次数,记录失败并继续
if (retryCount >= config.maxRetries) {
const reason = `页面加载超时 (${config.maxRetries} 次重试)`;
this.recordFailedPage(pageName, reason, pageStartTime);
return {success: false, error: reason};
}
// 检查页面状态 // 检查页面状态
const selectors = { const selectors = {
loadingMask: '.el-loading-mask', loadingMask: '.el-loading-mask',
@ -598,71 +598,34 @@ class LongiMainPage extends BasePage {
const elements = await this.page.locator(selector).all(); const elements = await this.page.locator(selector).all();
if (elements.length > 0 && key !== 'loadingMask') { if (elements.length > 0 && key !== 'loadingMask') {
const errorText = await elements[0].textContent(); const errorText = await elements[0].textContent();
const reason = `页面加载出现错误: ${errorText}`; console.error(`页面加载出现错误: ${pageName}, 错误信息: ${errorText}`);
this.recordFailedPage(pageName, reason, pageStartTime); return false;
return {success: false, error: reason};
} }
} }
// 检查加载状态 // 检查加载状态
const loadingMasks = await this.page.locator(selectors.loadingMask).all(); const loadingMasks = await this.page.locator(selectors.loadingMask).all();
if (loadingMasks.length > 0) { if (loadingMasks.length === 0) {
retryCount++; // 加载完成,等待页面稳定
await this.page.waitForTimeout(config.retryInterval); await this.page.waitForTimeout(config.stabilityDelay);
return await check(); 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++; retryCount++;
await this.page.waitForTimeout(config.retryInterval); await this.page.waitForTimeout(config.retryInterval);
return await check();
} }
};
return await check(); // 超时处理
} console.error(`页面加载超时: ${pageName}, 重试次数: ${config.maxRetries}`);
return false;
// 添加记录失败页面的方法 } catch (error) {
recordFailedPage(pageName, reason, startTime) { // 任何错误都记录并返回false
const duration = Date.now() - startTime; console.error(`页面加载出错: ${pageName}, 错误信息: ${error.message}`);
console.error(`页面加载失败: ${pageName}, 原因: ${reason}, 耗时: ${duration}ms`); return false;
// 这里可以添加将失败信息写入文件的逻辑 }
}
/**
* 获取二级菜单元素
* @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
};
} }
/** /**
@ -673,7 +636,6 @@ class LongiMainPage extends BasePage {
async closeActiveTab(pageName) { async closeActiveTab(pageName) {
try { try {
console.log(`🗑️ 正在关闭页面 "${pageName}" 的tab...`); console.log(`🗑️ 正在关闭页面 "${pageName}" 的tab...`);
// 检查是否存在活动的tab和关闭按钮 // 检查是否存在活动的tab和关闭按钮
const activeTab = this.page.locator('.vab-tabs .el-tabs--card .el-tabs__item.is-active'); const activeTab = this.page.locator('.vab-tabs .el-tabs--card .el-tabs__item.is-active');
const closeButton = activeTab.locator('.el-icon.is-icon-close'); const closeButton = activeTab.locator('.el-icon.is-icon-close');
@ -709,11 +671,9 @@ class LongiMainPage extends BasePage {
try { try {
// 检查三级菜单是否存在 // 检查三级菜单是否存在
const elements = await this.page.locator('.el-popper.is-light.el-popover .menuTitle.canClick').all(); const elements = await this.page.locator('.el-popper.is-light.el-popover .menuTitle.canClick').all();
if (elements.length === 0) { if (elements.length === 0) {
return []; return [];
} }
// 收集所有三级菜单项 // 收集所有三级菜单项
const thirdMenus = []; const thirdMenus = [];
for (let i = 0; i < elements.length; i++) { for (let i = 0; i < elements.length; i++) {
@ -752,18 +712,7 @@ class LongiMainPage extends BasePage {
// 获取三级菜单列表 // 获取三级菜单列表
const thirdMenus = await this.getThirdLevelMenus(menu); 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) { for (const thirdMenu of thirdMenus) {
await this.handleMenuClick(thirdMenu, menu.text); await this.handleMenuClick(thirdMenu, menu.text);
@ -791,19 +740,12 @@ class LongiMainPage extends BasePage {
const menuPath = parentText ? `${parentText} > ${menuInfo.text}` : menuInfo.text; const menuPath = parentText ? `${parentText} > ${menuInfo.text}` : menuInfo.text;
console.log(`🔸 点击菜单: ${menuPath}`); console.log(`🔸 点击菜单: ${menuPath}`);
// 点击菜单项 await menuInfo.element.click();
if (menuInfo.element) {
await menuInfo.element.click();
} else {
// 如果没有element属性尝试通过文本定位
const menuLocator = this.page.locator(`text=${menuInfo.text}`).first();
await menuLocator.click();
}
// 等待页面加载 // 等待页面加载
const loadResult = await this.waitForPageLoadWithRetry(menuInfo); const loadResult = await this.waitForPageLoadWithRetry(menuInfo);
if (!loadResult.success) { if (!loadResult) {
console.warn(loadResult.error); console.warn('页面加载失败');
return false; return false;
} }
@ -836,8 +778,8 @@ class LongiMainPage extends BasePage {
// 等待页面加载 // 等待页面加载
const loadResult = await this.waitForPageLoadWithRetry(parentMenu, tabInfo.text); const loadResult = await this.waitForPageLoadWithRetry(parentMenu, tabInfo.text);
if (!loadResult.success) { if (!loadResult) {
console.warn(`TAB页 ${tabInfo.text} 加载失败: ${loadResult.error}`); console.warn('页面加载失败');
return false; return false;
} }