123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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');
- });
- });
|