commands.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // cypress/support/commands.js
  2. // ***********************************************
  3. // This example commands.js shows you how to
  4. // create various custom commands and overwrite
  5. // existing commands.
  6. //
  7. // For more comprehensive examples of custom
  8. // commands please read more here:
  9. // https://on.cypress.io/custom-commands
  10. // ***********************************************
  11. //
  12. //
  13. // -- This is a parent command --
  14. // Cypress.Commands.add('login', (email, password) => { ... })
  15. //
  16. //
  17. // -- This is a child command --
  18. // Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
  19. //
  20. //
  21. // -- This is a dual command --
  22. // Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
  23. //
  24. //
  25. // -- This will overwrite an existing command --
  26. // Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
  27. Cypress.Commands.add('logWithDate', (message, ...args) => {
  28. const timestamp = new Date().toISOString();
  29. const formattedMessage = `[${timestamp}] ${message}`;
  30. // 输出到控制台
  31. console.log(formattedMessage, ...args);
  32. // 也可以输出到 Cypress 的命令日志(可选)
  33. cy.log(formattedMessage);
  34. return null; // Cypress 命令需要返回 null 或 Promise
  35. });
  36. /**
  37. * 自定义命令:登录并完成初始化进入主页
  38. *
  39. * 这个命令封装了完整的登录流程:
  40. * 1. 清除存储
  41. * 2. Mock 所有必要的 API(i18n、quota、patient types、body parts)
  42. * 3. 执行登录
  43. * 4. 等待初始化完成
  44. *
  45. * @param {string} username - 用户名,默认 'admin'
  46. * @param {string} password - 密码,默认 '123456'
  47. *
  48. * @example
  49. * cy.loginAndInitialize();
  50. * cy.loginAndInitialize('testuser', 'testpass');
  51. */
  52. Cypress.Commands.add('loginAndInitialize', (username = 'admin', password = '123456') => {
  53. // 导入所需的 mock handlers
  54. const { mockAllRequiredAPIs, mockI18nSuccess } = require('./mock/handlers/i18n');
  55. const { mockGetQuotaSuccess } = require('./mock/handlers/quota');
  56. const { mockGetPatientTypeHuman, mockGetBodyPartHuman } = require('./mock/handlers/protocol');
  57. const { mockLoginSuccess } = require('./mock/handlers/user');
  58. const LoginPage = require('./pageObjects/LoginPage').default;
  59. // 清除存储
  60. cy.clearAllSessionStorage();
  61. cy.clearAllLocalStorage();
  62. // Mock 所有必要的 API
  63. mockAllRequiredAPIs(); // 软件信息、日志
  64. mockI18nSuccess('zh'); // 多语言资源
  65. mockGetQuotaSuccess(); // 配额检查
  66. mockGetPatientTypeHuman(); // 患者类型 - 必需!
  67. mockGetBodyPartHuman(); // 身体部位 - 必需!
  68. mockLoginSuccess(); // 登录
  69. // 使用 LoginPage Page Object 进行登录
  70. const loginPage = new LoginPage();
  71. loginPage.visit();
  72. loginPage.login(username, password);
  73. // 等待登录成功消息
  74. cy.wait('@loginSuccess');
  75. cy.contains('登录成功', { timeout: 5000 }).should('be.visible');
  76. // 等待初始化完成 - 退出按钮可见表示已成功进入主页
  77. cy.get('[data-testid="exit-button"]', { timeout: 10000 }).should('be.visible');
  78. cy.log('✅ 登录并初始化完成');
  79. });
  80. // Cypress.on('window:before:load', (win) => {
  81. // // 每打开/刷新一页都会触发
  82. // cy.then(() => {
  83. // console.log('[全局] 内存 href:', win.location.href);
  84. // const [, stamp] = win.location.href.match(/[?&]stamp=([^&]*)/) || [];
  85. // if (stamp) console.log('[全局] 当前 stamp:', stamp);
  86. // });
  87. // });