You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Cypress测试Azure AAD登录遇阻问题求助

问题描述

我正尝试使用Cypress测试Azure AAD登录功能,已按照Cypress官方文档的步骤操作。
当我通过以下代码访问首页时:

describe('Azure Active Directory Authentication', () => {
  it('should verify the user logged in has the correct name', () => {
    cy.visit('/home')
  })
})

Cypress会卡在Azure登录页面。

而当我使用cy.origin方法时:

describe('Azure Active Directory Authentication', () => {
    it('should verify the user logged in has the correct name', () => {
      cy.visit('/home')
      const username = 'juanito'

      cy.origin(
          'login.microsoftonline.com',
          {args: {username}},
          ({ username }) => {
            console.log("HELELLOOO: " + username)
          }
      )
    })
})

登录页弹出后几秒直接跳转到认证后的页面,疑似浏览器或Cypress使用了缓存跳过了Azure登录。我已尝试清除浏览器缓存、重装npm包、调用Cypress.session.clearAllSavedSessions()和Cypress.session.clearCurrentSessionData(),但均无效。请问如何解决该问题,确保Cypress先完成Azure AAD认证再进入后续页面?

解决方案

以下是几个可尝试的解决方法:

1. 完善cy.origin内的登录逻辑

你当前的cy.origin块仅执行了日志输出,没有实际完成登录流程,导致缓存会话直接生效。需要在origin块内添加完整的用户名密码输入操作:

describe('Azure Active Directory Authentication', () => {
  it('should verify the user logged in has the correct name', () => {
    cy.visit('/home')
    
    cy.origin('login.microsoftonline.com', { args: { username: 'juanito', password: '你的密码' } }, 
      ({ username, password }) => {
        // 等待登录元素加载并完成输入
        cy.get('#i0116').should('be.visible').type(username)
        cy.get('#idSIButton9').click()
        cy.get('#i0118').should('be.visible').type(password)
        cy.get('#idSIButton9').click()
        // 处理保持登录弹窗(若存在)
        cy.get('#idBtn_Back').click()
      }
    )

    // 验证登录后的页面状态
    cy.get('#user-profile').should('exist')
  })
})

2. 配置会话策略强制清除缓存

在测试中明确设置会话缓存策略,确保每次测试都从头执行认证流程:

describe('Azure Active Directory Authentication', () => {
  beforeEach(() => {
    cy.session('azure-login', () => {
      cy.visit('/home')
      cy.origin('login.microsoftonline.com', () => {
        // 完整登录逻辑
        cy.get('#i0116').type('你的用户名')
        cy.get('#idSIButton9').click()
        cy.get('#i0118').type('你的密码')
        cy.get('#idSIButton9').click()
        cy.get('#idBtn_Back').click()
      })
    }, {
      cacheAcrossSpecs: false,
      validate: () => {
        // 验证是否成功登录
        cy.get('#user-name').should('be.visible')
      }
    })
  })

  it('should verify the user logged in has the correct name', () => {
    cy.visit('/home')
    cy.get('#user-name').should('contain', 'juanito')
  })
})

3. 启动Cypress时强制清除浏览器缓存

在cypress.config.js中添加浏览器启动参数,禁用缓存相关功能:

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {},
    browserLaunchOptions: {
      args: [
        '--disable-application-cache',
        '--disable-cache',
        '--disk-cache-size=0',
        '--disable-offline-load-stale-cache'
      ]
    }
  }
})

4. 强制Azure AD重新认证

在访问首页时添加prompt=login参数,强制Azure AD跳过SSO缓存,显示登录界面:

cy.visit('/home?prompt=login')

内容的提问来源于stack exchange,提问作者msuzuki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 22:22:25