i18n-loading-success.cy.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { mockI18nSuccess, mockAllRequiredAPIs } from '../../support/mock/handlers/i18n';
  2. import LoginPage from '../../support/pageObjects/LoginPage';
  3. // 语言设置辅助函数
  4. const setNavigatorLanguage = (language: string) => (win: Window) => {
  5. Object.defineProperty(win.navigator, 'language', {
  6. value: language,
  7. writable: false
  8. });
  9. };
  10. describe('多语言资源正常加载测试', () => {
  11. const loginPage = new LoginPage();
  12. beforeEach(() => {
  13. // 清除所有拦截器
  14. cy.clearAllSessionStorage();
  15. cy.clearAllLocalStorage();
  16. // Mock所有必要的API,避免影响页面加载
  17. mockAllRequiredAPIs();
  18. });
  19. it('访问登录页面时成功加载中文多语言资源', () => {
  20. // 设置mock
  21. mockI18nSuccess('zh');
  22. // 设置浏览器语言为中文并访问登录页面
  23. loginPage.visit({
  24. onBeforeLoad: setNavigatorLanguage('zh-CN')
  25. });
  26. // 核心断言:等待用户名输入框真正可见
  27. // be.visible 会检查元素是否在DOM中存在、不被隐藏、且在视口中可见
  28. cy.get('[data-testid="login-username-input"]', { timeout: 10000 })
  29. .should('be.visible');
  30. // 验证API调用确实发生了
  31. cy.wait('@getI18nZHSuccess');
  32. // 验证其他关键UI元素也可见
  33. cy.get('[data-testid="login-password-input"]').should('be.visible');
  34. cy.get('[data-testid="login-submit-button"]').should('be.visible');
  35. });
  36. it('访问登录页面时成功加载英文多语言资源', () => {
  37. // 设置mock
  38. mockI18nSuccess('en');
  39. // 设置浏览器语言为英文并访问登录页面
  40. loginPage.visit({
  41. onBeforeLoad: setNavigatorLanguage('en-US')
  42. });
  43. // 核心断言:等待用户名输入框真正可见
  44. // be.visible 会检查元素是否在DOM中存在、不被隐藏、且在视口中可见
  45. cy.get('[data-testid="login-username-input"]', { timeout: 10000 })
  46. .should('be.visible');
  47. // 验证API调用确实发生了
  48. cy.wait('@getI18nENSuccess');
  49. // 验证其他关键UI元素也可见
  50. cy.get('[data-testid="login-password-input"]').should('be.visible');
  51. cy.get('[data-testid="login-submit-button"]').should('be.visible');
  52. });
  53. it('验证多语言资源请求参数正确', () => {
  54. mockI18nSuccess('zh');
  55. loginPage.visit({
  56. onBeforeLoad: setNavigatorLanguage('zh-CN')
  57. });
  58. // 等待用户名输入框可见,确保多语言加载成功
  59. cy.get('[data-testid="login-username-input"]', { timeout: 10000 })
  60. .should('be.visible');
  61. // 验证请求URL正确
  62. cy.wait('@getI18nZHSuccess').then((interception) => {
  63. expect(interception.request.url).to.include('/dr/api/v1/pub/trans/zh/zh.js');
  64. expect(interception.request.method).to.equal('GET');
  65. });
  66. });
  67. it('验证加载成功后Redux状态正确', () => {
  68. mockI18nSuccess('zh');
  69. loginPage.visit({
  70. onBeforeLoad: setNavigatorLanguage('zh-CN')
  71. });
  72. // 等待用户名输入框可见,确保多语言加载成功
  73. cy.get('[data-testid="login-username-input"]', { timeout: 10000 })
  74. .should('be.visible');
  75. cy.wait('@getI18nZHSuccess');
  76. // 验证Redux store中的i18n状态
  77. cy.window().its('store').invoke('getState').then((state) => {
  78. expect(state.i18n.loading).to.be.false;
  79. expect(state.i18n.error).to.be.null;
  80. expect(state.i18n.currentLocale).to.equal('zh');
  81. expect(state.i18n.messages).to.be.an('object');
  82. expect(Object.keys(state.i18n.messages).length).to.be.greaterThan(0);
  83. });
  84. });
  85. it('验证浏览器语言 zh-CN 自动检测为中文', () => {
  86. mockI18nSuccess('zh');
  87. loginPage.visit({
  88. onBeforeLoad: setNavigatorLanguage('zh-CN')
  89. });
  90. // 等待用户名输入框可见,确保多语言加载成功
  91. cy.get('[data-testid="login-username-input"]', { timeout: 10000 })
  92. .should('be.visible');
  93. cy.wait('@getI18nZHSuccess');
  94. // 验证其他关键UI元素也可见
  95. cy.get('[data-testid="login-password-input"]').should('be.visible');
  96. cy.get('[data-testid="login-submit-button"]').should('be.visible');
  97. });
  98. it('验证浏览器语言 zh-TW 自动检测为中文', () => {
  99. mockI18nSuccess('zh');
  100. loginPage.visit({
  101. onBeforeLoad: setNavigatorLanguage('zh-TW')
  102. });
  103. // 等待用户名输入框可见,确保多语言加载成功
  104. cy.get('[data-testid="login-username-input"]', { timeout: 10000 })
  105. .should('be.visible');
  106. cy.wait('@getI18nZHSuccess');
  107. // 验证其他关键UI元素也可见
  108. cy.get('[data-testid="login-password-input"]').should('be.visible');
  109. cy.get('[data-testid="login-submit-button"]').should('be.visible');
  110. });
  111. it('验证浏览器语言 en-US 自动检测为英文', () => {
  112. mockI18nSuccess('en');
  113. loginPage.visit({
  114. onBeforeLoad: setNavigatorLanguage('en-US')
  115. });
  116. // 等待用户名输入框可见,确保多语言加载成功
  117. cy.get('[data-testid="login-username-input"]', { timeout: 10000 })
  118. .should('be.visible');
  119. cy.wait('@getI18nENSuccess');
  120. // 验证其他关键UI元素也可见
  121. cy.get('[data-testid="login-password-input"]').should('be.visible');
  122. cy.get('[data-testid="login-submit-button"]').should('be.visible');
  123. });
  124. it('验证浏览器语言 en-GB 自动检测为英文', () => {
  125. mockI18nSuccess('en');
  126. loginPage.visit({
  127. onBeforeLoad: setNavigatorLanguage('en-GB')
  128. });
  129. // 等待用户名输入框可见,确保多语言加载成功
  130. cy.get('[data-testid="login-username-input"]', { timeout: 10000 })
  131. .should('be.visible');
  132. cy.wait('@getI18nENSuccess');
  133. // 验证其他关键UI元素也可见
  134. cy.get('[data-testid="login-password-input"]').should('be.visible');
  135. cy.get('[data-testid="login-submit-button"]').should('be.visible');
  136. });
  137. });