123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- // cypress/support/commands.js
- // ***********************************************
- // This example commands.js shows you how to
- // create various custom commands and overwrite
- // existing commands.
- //
- // For more comprehensive examples of custom
- // commands please read more here:
- // https://on.cypress.io/custom-commands
- // ***********************************************
- //
- //
- // -- This is a parent command --
- // Cypress.Commands.add('login', (email, password) => { ... })
- //
- //
- // -- This is a child command --
- // Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
- //
- //
- // -- This is a dual command --
- // Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
- //
- //
- // -- This will overwrite an existing command --
- // Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
- Cypress.Commands.add('logWithDate', (message, ...args) => {
- const timestamp = new Date().toISOString();
- const formattedMessage = `[${timestamp}] ${message}`;
-
- // 输出到控制台
- console.log(formattedMessage, ...args);
-
- // 也可以输出到 Cypress 的命令日志(可选)
- cy.log(formattedMessage);
-
- return null; // Cypress 命令需要返回 null 或 Promise
- });
- /**
- * 自定义命令:登录并完成初始化进入主页
- *
- * 这个命令封装了完整的登录流程:
- * 1. 清除存储
- * 2. Mock 所有必要的 API(i18n、quota、patient types、body parts)
- * 3. 执行登录
- * 4. 等待初始化完成
- *
- * @param {string} username - 用户名,默认 'admin'
- * @param {string} password - 密码,默认 '123456'
- *
- * @example
- * cy.loginAndInitialize();
- * cy.loginAndInitialize('testuser', 'testpass');
- */
- Cypress.Commands.add('loginAndInitialize', (username = 'admin', password = '123456') => {
- // 导入所需的 mock handlers
- const { mockAllRequiredAPIs, mockI18nSuccess } = require('./mock/handlers/i18n');
- const { mockGetQuotaSuccess } = require('./mock/handlers/quota');
- const { mockGetPatientTypeHuman, mockGetBodyPartHuman } = require('./mock/handlers/protocol');
- const { mockLoginSuccess } = require('./mock/handlers/user');
- const LoginPage = require('./pageObjects/LoginPage').default;
- // 清除存储
- cy.clearAllSessionStorage();
- cy.clearAllLocalStorage();
-
- // Mock 所有必要的 API
- mockAllRequiredAPIs(); // 软件信息、日志
- mockI18nSuccess('zh'); // 多语言资源
- mockGetQuotaSuccess(); // 配额检查
- mockGetPatientTypeHuman(); // 患者类型 - 必需!
- mockGetBodyPartHuman(); // 身体部位 - 必需!
- mockLoginSuccess(); // 登录
-
- // 使用 LoginPage Page Object 进行登录
- const loginPage = new LoginPage();
- loginPage.visit();
- loginPage.login(username, password);
-
- // 等待登录成功消息
- cy.wait('@loginSuccess');
- cy.contains('登录成功', { timeout: 5000 }).should('be.visible');
-
- // 等待初始化完成 - 退出按钮可见表示已成功进入主页
- cy.get('[data-testid="exit-button"]', { timeout: 10000 }).should('be.visible');
-
- cy.log('✅ 登录并初始化完成');
- });
- // Cypress.on('window:before:load', (win) => {
- // // 每打开/刷新一页都会触发
- // cy.then(() => {
- // console.log('[全局] 内存 href:', win.location.href);
- // const [, stamp] = win.location.href.match(/[?&]stamp=([^&]*)/) || [];
- // if (stamp) console.log('[全局] 当前 stamp:', stamp);
- // });
- // });
|