cypress-longi/cypress/e2e/需求计划页面是否可用1.cy.js
2025-03-03 14:04:22 +08:00

177 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

describe('隆基系统页面验证', () => {
// 环境配置
const ENV_CONFIG = {
DEV: {
url: 'https://ibp-dev.longi.com/main/#/login',
needCredentials: false
},
UAT: {
url: 'https://ibp-test.longi.com/main/#/login?debug=ly',
needCredentials: true,
username: 'qichenadmin',
password: '123456'
}
};
// 登录处理函数
const handleLogin = (environment) => {
const envConfig = ENV_CONFIG[environment];
if (!envConfig) {
throw new Error(`未知的环境: ${environment}`);
}
cy.log(`正在登录 ${environment} 环境: ${envConfig.url}`);
cy.visit(envConfig.url);
// 忽略未捕获的异常
Cypress.on('uncaught:exception', (err, runnable) => false);
cy.get('#app', {timeout: 10000}).should('exist');
if (envConfig.needCredentials) {
// 清空输入框
cy.get('input[name="username"]').clear();
cy.get('input[name="passWord"]').clear();
// 输入账号密码
cy.get('input[name="username"]').type(envConfig.username);
cy.get('input[name="passWord"]').type(envConfig.password);
}
// 执行登录
cy.get('.container-button').click();
// 等待登录完成
cy.wait(3000);
};
// 清理内存
const cleanupMemory = () => {
cy.task('clearMemory');
};
// 创建常用元素别名
const createAliases = () => {
cy.log('创建常用元素别名');
// 页面主体
cy.get('body').as('pageBody');
// 侧边栏导航
cy.get('.ly-side-nav').as('sideNav');
// 主菜单
cy.get('.el-scrollbar__view > .el-menu').as('mainMenu');
// 菜单项
cy.get('.el-menu-item').as('menuItems');
// 子菜单标题
cy.get('.el-sub-menu__title').as('subMenuTitles');
// 内容区域
cy.get('.vab-content').as('contentArea');
// 标签页
cy.get('.vab-tabs').as('tabs');
// 加载遮罩
cy.get('.el-loading-mask').as('loadingMask');
// 菜单切换按钮
cy.get('.vab-content .toggle-icon').as('menuToggle');
cy.log('别名创建完成');
};
// 确保菜单展开
const ensureMenuExpanded = () => {
cy.log('确保菜单处于展开状态');
cy.get('@sideNav').then(($el) => {
cy.log(`菜单现在的偏移量是:${$el.css('left')}`);
const leftValue = $el.css('left');
if (leftValue !== '0px') {
cy.log('菜单未展开,点击展开');
cy.get('@menuToggle').click();
cy.wait(500); // 等待菜单展开动画
cy.log('已点击展开菜单');
} else {
cy.log('菜单已经处于展开状态');
}
});
};
// 获取菜单数据
const getMenuItems = () => {
cy.log('开始获取菜单结构');
return cy.get('@mainMenu').then(($elMenu) => {
const menuItems = [];
// 获取所有菜单项
const allMenuItems = $elMenu.find('.el-sub-menu__title, .el-menu-item');
// 过滤并整理菜单项
allMenuItems.each((index, el) => {
const $item = Cypress.$(el);
const menuText = $item.find('.titleSpan').text().trim() || $item.text().trim();
const isFirstLevel = $item.find('.menuIcon').length > 0;
// 只添加二级菜单项
if (!isFirstLevel) {
menuItems.push({
index,
text: menuText,
element: el,
hasThirdMenu: false // 默认设置为没有三级菜单
});
}
});
cy.log(`🔍 找到 ${menuItems.length} 个可测试的菜单项`);
// 使用cy.wrap()将同步值转换为Cypress命令
return cy.wrap(menuItems);
});
};
// 测试入口
it('验证系统页面是否可用', () => {
// 从环境变量获取环境参数默认为DEV
const environment = Cypress.env('environment') || 'DEV';
// 验证环境参数
if (!ENV_CONFIG[environment]) {
cy.log(`❌ 无效的环境参数: ${environment}`);
cy.log('有效的环境参数: DEV, UAT');
throw new Error(`无效的环境参数: ${environment}`);
}
cy.log(`🚀 开始在 ${environment} 环境测试页面`);
// 登录系统
handleLogin(environment);
// 创建常用元素别名
createAliases();
// 确保菜单展开
ensureMenuExpanded();
// 获取菜单数据
getMenuItems().then(menuItems => {
cy.log(`获取到 ${menuItems.length} 个菜单项`);
// 这里可以添加更多的测试逻辑使用menuItems
menuItems.forEach((item, index) => {
cy.log(`菜单项 ${index + 1}: ${item.text}`);
});
});
// 使用别名测试菜单是否加载成功
cy.get('@menuItems', { timeout: 10000 }).should('be.visible');
cy.log('✅ 系统菜单加载成功');
cy.log(`🏁 测试完成,环境: ${environment}`);
});
});