LoginPage.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // import 'cypress';
  2. import { fileIndex } from '../util';
  3. class LoginPage {
  4. visit(options?: { onBeforeLoad?: (win: Window) => void }) {
  5. // if (options?.onBeforeLoad) {
  6. // cy.visit(`${fileIndex}#/pages/index/index`, {
  7. // onBeforeLoad: options.onBeforeLoad
  8. // });
  9. // } else {
  10. // cy.visit(`${fileIndex}#/pages/index/index`);
  11. // }
  12. cy.visit(`${fileIndex}#/pages/index/index`, {
  13. onBeforeLoad(win) {
  14. win.addEventListener('beforeunload', () => console.trace('beforeunload'));
  15. win.addEventListener('unload', () => console.trace('unload'));
  16. // ===== 新增两行:钉时间戳 =====
  17. // cy.stub(win.Date, 'now').returns(1712345678901);
  18. // ========================================
  19. // 保持原透传逻辑
  20. options?.onBeforeLoad?.(win);
  21. }
  22. });
  23. }
  24. getUsernameInput() {
  25. return cy.get('[data-testid="login-username-input"]');
  26. }
  27. getPasswordInput() {
  28. return cy.get('[data-testid="login-password-input"]');
  29. }
  30. getSubmitButton() {
  31. return cy.get('[data-testid="login-submit-button"]');
  32. }
  33. login(username: string, password: string) {
  34. this.getUsernameInput().type(username);
  35. this.getPasswordInput().type(password);
  36. this.getSubmitButton().click();
  37. }
  38. // 获取用户名标签文本
  39. getUsernameLabel() {
  40. return cy.get('label').contains(/用户名|Username/);
  41. }
  42. // 获取密码标签文本
  43. getPasswordLabel() {
  44. return cy.get('label').contains(/密码|Password/);
  45. }
  46. // 获取登录按钮
  47. getLoginButton() {
  48. return cy.get('[data-testid="login-submit-button"]');
  49. }
  50. // 获取急诊按钮
  51. getEmergencyButton() {
  52. return cy.contains('button', /急诊|Emergency/);
  53. }
  54. // 验证页面是否为指定语言
  55. verifyLanguage(locale: 'zh' | 'en') {
  56. if (locale === 'zh') {
  57. this.getUsernameLabel().should('contain', '用户名');
  58. this.getPasswordLabel().should('contain', '密码');
  59. this.getLoginButton().should('contain', '登录');
  60. this.getEmergencyButton().should('contain', '急诊');
  61. } else {
  62. this.getUsernameLabel().should('contain', 'Username');
  63. this.getPasswordLabel().should('contain', 'Password');
  64. this.getLoginButton().should('contain', 'Login');
  65. this.getEmergencyButton().should('contain', 'Emergency');
  66. }
  67. }
  68. // 验证占位符文本
  69. verifyPlaceholders(locale: 'zh' | 'en') {
  70. if (locale === 'zh') {
  71. this.getUsernameInput().should('have.attr', 'placeholder', '请输入用户名');
  72. this.getPasswordInput().should('have.attr', 'placeholder', '请输入密码');
  73. } else {
  74. this.getUsernameInput().should('have.attr', 'placeholder', 'Enter username');
  75. this.getPasswordInput().should('have.attr', 'placeholder', 'Enter password');
  76. }
  77. }
  78. }
  79. export default LoginPage;