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

Authentication Cypress:登录身份验证重定向到另一个域:解决方法?,authentication,redirect,cypress,Authentication,Redirect,Cypress,我知道Cypress不允许从一个域切换到另一个域,因为它会出现以下错误: chrome-error://chromewebdata/ 但是,我需要一个解决办法。我为多个环境提供了一个测试集:阶段、演示、产品 使用DEMO和PROD,在身份验证阶段(用户名/密码),保持在同一域内: 访问:https://[demo|www.foo.com AUTH:https://account.foo.com/auth>用户名>>密码 同意:https://[demo| www.foo.com/action

我知道Cypress不允许从一个域切换到另一个域,因为它会出现以下错误:

chrome-error://chromewebdata/
但是,我需要一个解决办法。我为多个环境提供了一个测试集:阶段、演示、产品

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

  • 访问
    https://[demo|www.foo.com
  • AUTH
    https://account.foo.com/auth
    >用户名>>密码
  • 同意
    https://[demo| www.foo.com/action…
使用阶段,身份验证阶段切换到另一个域:

  • 访问
    https://[stage].foo.com
  • AUTH
    https://account.bar.com/auth
    >用户名>>密码
  • 同意
    https://[stage].foo.com/action…
因此,由于域翻转,Cypress无法从VISIT重定向到AUTH。这是阶段的阻塞测试

推荐的解决方法是什么

  • 木偶演员
  • 使用
    cy.request()
参考:


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

我采用的方法与柏树食谱相似

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

首先需要的是来自帐户登录页面的令牌,其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)
        })
})
然后在提供给执行登录的
cy.request()
requestOptions的
body
中,提供一次性CSRF令牌:

    const body: { [key: string]: string } = {
        email: $envCypress.account_username,
        password: $envCypress.account_password,
        __RequestVerificationToken: $csrfToken
    };

我采用的方法与柏树食谱相似

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

首先需要的是来自帐户登录页面的令牌,其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)
        })
})
然后在提供给执行登录的
cy.request()
requestOptions的
body
中,提供一次性CSRF令牌:

    const body: { [key: string]: string } = {
        email: $envCypress.account_username,
        password: $envCypress.account_password,
        __RequestVerificationToken: $csrfToken
    };