Cypress: Login Authentication redirects to another domain: Workaround?

Viewed 6720

I understand that Cypress does not allow flipping from one domain to another domain because it will error with:

chrome-error://chromewebdata/

However, I need a workaround. I am providing a test set for multiple environments: STAGE, DEMO, PROD.

With DEMO and PROD, during the authentication phase (username/password), stay within the same domain:

  • VISIT: https://[demo|www].foo.com
  • AUTH: https://account.foo.com/auth >> username >> password
  • CONSENT: https://[demo|www].foo.com/action...

With STAGE, the authentication phase flips to another domain:

  • VISIT: https://[stage].foo.com
  • AUTH: https://account.bar.com/auth >> username >> password
  • CONSENT: https://[stage].foo.com/action...

Thereby, Cypress fails to redirect from VISIT to AUTH because of domain flip. This is blocking testing of STAGE.

What recommended workaround approaches?

  • Puppeteer?
  • Native Cypress using cy.request()?

Referenced:

Thank you, much appreciate the assistance.

2 Answers

The approach I took is similar to the Cypress Recipe Login with CSRF token

As mentioned, each deployment has its own website URL followed by account login URL.

Needed first is the CSRF token from account login page, which its URL is referred here as $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)
        })
})

Then within body of requestOptions provided to cy.request() which will performing the login, provide one-time CSRF token:

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