cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.

Home Page:https://cypress.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`cy.origin` not working with Auth0 login

wongk opened this issue · comments

Current behavior

We are using Cypress to test a SPA (Angular) that integrates with Auth0 for login. I have added a login command to our cypress suite that is a copy & paste of the command from the real world app. The command fails when cy.origin is called.

Screen Shot 2023-01-24 at 9 30 08 AM

Our app redirects to our Auth0 login page when "/" is visited, but Cypress does not detect this. This login command succeeds without the call to cy.origin, but the session is then restored for the wrong domain.

Desired behavior

No response

Test code to reproduce

Cypress.Commands.add("login", (username: string, password: string) => {
  const log = Cypress.log({
    displayName: "AUTH0 LOGIN",
    message: [`🔐 Authenticating | ${username}`],
    autoEnd: false,
  });
  log.snapshot("before");

  const args = { username, password };
  cy.session(
    args,
    () => {
      cy.visit("/");

      cy.origin("auth.our.domain.com", { args }, ({ username, password }) => {
        cy.get(loginPageLocators.emailInput).type(username);
        cy.get(loginPageLocators.passwordInput).type(password);
        cy.get(loginPageLocators.continueButton).click();
      });
      cy.url().should("contain", Cypress.config().baseUrl);
    },
    {
      cacheAcrossSpecs: true,
    }
  );
  cy.visit("/");

  log.snapshot("after");
  log.end();
});

Cypress Version

12.3.0

Node version

v16.15.0

Operating System

macOS 12.5

Debug Logs

No response

Other

No response

Hi @wongk. Thank you for opening an issue. I am having some trouble reproducing the issue. Are you able to show the error or the replay on the session? I could be possible that the session is saving too early before the auth flow is fully completely, which you might be able to determine with some type of timeout before the validation

  cy.origin("auth.our.domain.com", { args }, ({ username, password }) => {
        cy.get(loginPageLocators.emailInput).type(username);
        cy.get(loginPageLocators.passwordInput).type(password);
        cy.get(loginPageLocators.continueButton).click();
      });
      // does this change anything?
      cy.wait(5000)
      cy.url().should("contain", Cypress.config().baseUrl);

It turns out you can't use locator variables in cy.origin, even if they're global. This was causing the issue, but for some reason, yesterday Cypress was not showing me the full error.

Now I am having an issue with cy.session. When the session is restored and the command visits "/", the Auth0 guard is still redirecting to the login page. It works fine the first time, when the session is created.

It turns out you can't use locator variables in cy.origin, even if they're global. This was causing the issue, but for some reason, yesterday Cypress was not showing me the full error.

What do you mean by locator variables? Can you share a sample selector?

Now I am having an issue with cy.session. When the session is restored and the command visits "/", the Auth0 guard is still redirecting to the login page. It works fine the first time, when the session is created.

would you be able to share your session code? We might be able to help you get unblocked here. My guess is there may be issues with rehydrating or saving the initial state too early.