Cypress:登录身份验证重定向到另一个域:解决方法?

Jef*_*ner 7 authentication redirect cypress

据我所知,Cypress 不允许从一个域翻转到另一个域,因为它会出错:

chrome-error://chromewebdata/
Run Code Online (Sandbox Code Playgroud)

但是,我需要一个解决方法。我正在为多种环境提供测试集:STAGE、DEMO、PROD。

使用DEMOPROD,在身份验证阶段(用户名/密码),保持在同一域中:

  • 访问https://[demo|www].foo.com
  • 授权https://account.foo.com/auth>>用户名>>密码
  • 同意https://[demo|www].foo.com/action...

使用STAGE,身份验证阶段翻转到另一个域:

  • 访问https://[stage].foo.com
  • 授权https://account.bar.com/auth>>用户名>>密码
  • 同意https://[stage].foo.com/action...

因此,由于域翻转, Cypress 无法从VISIT重定向到AUTH 。这阻碍了STAGE的测试的阻塞测试。

有哪些推荐的解决方法?

  • 傀儡师?
  • 原生赛普拉斯使用cy.request()

参考:

谢谢,非常感谢您的帮助。

Jef*_*ner 3

我采取的方法类似于使用 CSRF 令牌的 Cypress Recipe Login

如前所述,每个部署都有自己的网站 URL,后跟帐户登录 URL

首先需要来自帐户登录页面的CSRF令牌,其 URL 在这里被称为:$baseUrl

Cypress.Commands.add('cmdGetCSRF', ($baseUrl) => {
    cy.log('COMMAND cmdGetCSRF');

    expect($baseUrl)
        .to.be.a('string')
        .not.empty

    cy.request($baseUrl)
        .its('body')
        .then(($body) => {
            const $html = Cypress.$($body)

            cy.log('html', $html)

            const csrf = $html.find('input[name="__RequestVerificationToken"]').val()

            expect(csrf)
                .to.be.a('string')
                .not.empty

            return cy.wrap(csrf)
        })
})
Run Code Online (Sandbox Code Playgroud)

然后在body执行登录requestOptions时提供cy.request()一次性 CSRF 令牌:

    const body: { [key: string]: string } = {
        email: $envCypress.account_username,
        password: $envCypress.account_password,
        __RequestVerificationToken: $csrfToken
    };
Run Code Online (Sandbox Code Playgroud)