import { mockI18nSuccess, mockAllRequiredAPIs } from '../../support/mock/handlers/i18n'; import LoginPage from '../../support/pageObjects/LoginPage'; // 语言设置辅助函数 const setNavigatorLanguage = (language: string) => (win: Window) => { Object.defineProperty(win.navigator, 'language', { value: language, writable: false }); }; describe('多语言资源正常加载测试', () => { const loginPage = new LoginPage(); beforeEach(() => { // 清除所有拦截器 cy.clearAllSessionStorage(); cy.clearAllLocalStorage(); // Mock所有必要的API,避免影响页面加载 mockAllRequiredAPIs(); }); it('访问登录页面时成功加载中文多语言资源', () => { // 设置mock mockI18nSuccess('zh'); // 设置浏览器语言为中文并访问登录页面 loginPage.visit({ onBeforeLoad: setNavigatorLanguage('zh-CN') }); // 核心断言:等待用户名输入框真正可见 // be.visible 会检查元素是否在DOM中存在、不被隐藏、且在视口中可见 cy.get('[data-testid="login-username-input"]', { timeout: 10000 }) .should('be.visible'); // 验证API调用确实发生了 cy.wait('@getI18nZHSuccess'); // 验证其他关键UI元素也可见 cy.get('[data-testid="login-password-input"]').should('be.visible'); cy.get('[data-testid="login-submit-button"]').should('be.visible'); }); it('访问登录页面时成功加载英文多语言资源', () => { // 设置mock mockI18nSuccess('en'); // 设置浏览器语言为英文并访问登录页面 loginPage.visit({ onBeforeLoad: setNavigatorLanguage('en-US') }); // 核心断言:等待用户名输入框真正可见 // be.visible 会检查元素是否在DOM中存在、不被隐藏、且在视口中可见 cy.get('[data-testid="login-username-input"]', { timeout: 10000 }) .should('be.visible'); // 验证API调用确实发生了 cy.wait('@getI18nENSuccess'); // 验证其他关键UI元素也可见 cy.get('[data-testid="login-password-input"]').should('be.visible'); cy.get('[data-testid="login-submit-button"]').should('be.visible'); }); it('验证多语言资源请求参数正确', () => { mockI18nSuccess('zh'); loginPage.visit({ onBeforeLoad: setNavigatorLanguage('zh-CN') }); // 等待用户名输入框可见,确保多语言加载成功 cy.get('[data-testid="login-username-input"]', { timeout: 10000 }) .should('be.visible'); // 验证请求URL正确 cy.wait('@getI18nZHSuccess').then((interception) => { expect(interception.request.url).to.include('/dr/api/v1/pub/trans/zh/zh.js'); expect(interception.request.method).to.equal('GET'); }); }); it('验证加载成功后Redux状态正确', () => { mockI18nSuccess('zh'); loginPage.visit({ onBeforeLoad: setNavigatorLanguage('zh-CN') }); // 等待用户名输入框可见,确保多语言加载成功 cy.get('[data-testid="login-username-input"]', { timeout: 10000 }) .should('be.visible'); cy.wait('@getI18nZHSuccess'); // 验证Redux store中的i18n状态 cy.window().its('store').invoke('getState').then((state) => { expect(state.i18n.loading).to.be.false; expect(state.i18n.error).to.be.null; expect(state.i18n.currentLocale).to.equal('zh'); expect(state.i18n.messages).to.be.an('object'); expect(Object.keys(state.i18n.messages).length).to.be.greaterThan(0); }); }); it('验证浏览器语言 zh-CN 自动检测为中文', () => { mockI18nSuccess('zh'); loginPage.visit({ onBeforeLoad: setNavigatorLanguage('zh-CN') }); // 等待用户名输入框可见,确保多语言加载成功 cy.get('[data-testid="login-username-input"]', { timeout: 10000 }) .should('be.visible'); cy.wait('@getI18nZHSuccess'); // 验证其他关键UI元素也可见 cy.get('[data-testid="login-password-input"]').should('be.visible'); cy.get('[data-testid="login-submit-button"]').should('be.visible'); }); it('验证浏览器语言 zh-TW 自动检测为中文', () => { mockI18nSuccess('zh'); loginPage.visit({ onBeforeLoad: setNavigatorLanguage('zh-TW') }); // 等待用户名输入框可见,确保多语言加载成功 cy.get('[data-testid="login-username-input"]', { timeout: 10000 }) .should('be.visible'); cy.wait('@getI18nZHSuccess'); // 验证其他关键UI元素也可见 cy.get('[data-testid="login-password-input"]').should('be.visible'); cy.get('[data-testid="login-submit-button"]').should('be.visible'); }); it('验证浏览器语言 en-US 自动检测为英文', () => { mockI18nSuccess('en'); loginPage.visit({ onBeforeLoad: setNavigatorLanguage('en-US') }); // 等待用户名输入框可见,确保多语言加载成功 cy.get('[data-testid="login-username-input"]', { timeout: 10000 }) .should('be.visible'); cy.wait('@getI18nENSuccess'); // 验证其他关键UI元素也可见 cy.get('[data-testid="login-password-input"]').should('be.visible'); cy.get('[data-testid="login-submit-button"]').should('be.visible'); }); it('验证浏览器语言 en-GB 自动检测为英文', () => { mockI18nSuccess('en'); loginPage.visit({ onBeforeLoad: setNavigatorLanguage('en-GB') }); // 等待用户名输入框可见,确保多语言加载成功 cy.get('[data-testid="login-username-input"]', { timeout: 10000 }) .should('be.visible'); cy.wait('@getI18nENSuccess'); // 验证其他关键UI元素也可见 cy.get('[data-testid="login-password-input"]').should('be.visible'); cy.get('[data-testid="login-submit-button"]').should('be.visible'); }); });