cypress icon indicating copy to clipboard operation
cypress copied to clipboard

`window.Cypress` is `undefined` after redirect back to application from `cy.origin`

Open AaronMcCloskey opened this issue 2 years ago • 11 comments

Current behavior

Attempting to log in to my application via google. When the page loads, window.Cypress has the expected value.

But after I get redirected back to my application from Google sign in, window.Cypress in my application is undefined

Desired behavior

That when I am redirected back from cy.origin window.Cypress value is persisted/not undefined.

Test code to reproduce

This is my test.

Should not, I even tried to assert that window.Cypress is an object, which it seems to be in the context of the test, but not the application. When I have a console.log(window.Cypress) in my application it's undefined.

login.cy.ts

export default describe('Login Page', () => {
  beforeEach(() => {
    cy.visit(`/login`);
    cy.clearCookies();
    cy.clearLocalStorage();
  });

  it('logs in via Google', () => {
    cy.awaitPageLoad();

    // click on google login button
    cy.clickBtn('google-login');

    cy.origin('https://accounts.google.com/', () => {
      cy.get('#identifierId').type(Cypress.env('GOOGLE_LOGIN_EMAIL'));
      cy.get('span').contains('Next').click();
      Cypress.on('uncaught:exception', (err) => !err.message.includes('ResizeObserver loop limit exceeded'));
      cy.get("[name='password']").type(Cypress.env('SOCIAL_LOGIN_PASSWORD'), { log: false, timeout: 20000, force: true });
      cy.get('span').contains('Next').click();
      cy.window().its('Cypress').should('be.an', 'object');
    });
    cy.window().its('Cypress').should('be.an', 'object');

    cy.awaitPageLoad();
  });
});

My tests fail as I am relying on window.Cypress to have a value so certain functions don't get triggered. i.e analytics.

I even tried

Cypress.on('window:before:load', (win) => {
  win.CypressRunning = true;
});

And did a check for window.CypressRunning but it also becomes undefined even though I never explicitly set it to anything else rather than true

Here is my cypress.config.ts

cypress.config.ts

import { defineConfig } from 'cypress';
import flags from './cypress/fixtures/flags.json';
import testOrder from './cypress/fixtures/test-order.json';

require('dotenv').config({ path: `.env` });

const order = testOrder.map((t) => `**/${t}.{cy,spec}.{js,jsx,ts,tsx}`);

export default defineConfig({
  e2e: {
    specPattern: [...order, '**/*.{cy,spec}.{js,jsx,ts,tsx}'],
    setupNodeEvents(on, config) {
      config.env.DEV_URL = process.env.CYPRESS_DEV_URL;
      config.env.GOOGLE_LOGIN_EMAIL = process.env.GOOGLE_LOGIN_EMAIL;
      config.env.SOCIAL_LOGIN_PASSWORD = process.env.SOCIAL_LOGIN_PASSWORD;

      config.video = false;
      config.screenshotOnRunFailure = false;

      config.experimentalModifyObstructiveThirdPartyCode = true;

      const excludeSpecPattern = ['**/1-getting-started/*', '**/2-advanced-examples/*'];

      if (config.isTextTerminal) {
        excludeSpecPattern.push('**/all.{cy,spec}.{js,jsx,ts,tsx}');
      }

      config.excludeSpecPattern = excludeSpecPattern;

      on('before:browser:launch', (browser, launchOptions) => {
        if (browser && (browser?.name === 'chromium' || browser?.name === 'chrome')) {
          flags.forEach((flag) => {
            if (!launchOptions.args.includes(flag)) launchOptions.args.push(flag);
          });
        }
        return launchOptions;
      });

      return config;
    },
  },
});

Cypress Version

12.3.0

Node version

16.14.2

Operating System

macOS 12.6

Debug Logs

No response

Other

No response

AaronMcCloskey avatar Jan 09 '23 00:01 AaronMcCloskey

Sorry to see you're encountering an issue. In order to properly investigate this, we'll need a minimal reproduction that reliably demonstrates the issue. The code provided includes app routes (e.g. /login) and custom commands (e.g. awaitPageLoad) that we cannot reproduce.

chrisbreiding avatar Jan 10 '23 14:01 chrisbreiding

Sorry to see you're encountering an issue. In order to properly investigate this, we'll need a minimal reproduction that reliably demonstrates the issue. The code provided includes app routes (e.g. /login) and custom commands (e.g. awaitPageLoad) that we cannot reproduce.

Not a problem, I'll look into doing that this evening.

AaronMcCloskey avatar Jan 10 '23 15:01 AaronMcCloskey

@chrisbreiding Here is my minimal reproduction of issue

I've left instructions in the README.md on the steps needed to run the application and produce the issue.

If you have any issues, give me a shout.

My apologies, I know it's a bit involved having to create a Google OAuth project to test this issue.

AaronMcCloskey avatar Jan 10 '23 17:01 AaronMcCloskey

Thanks for the reproduction! That will help a lot with diagnosing this issue.

I was able to verify the issue by running the reproduction. I'm going to route this to my team for further investigation.

chrisbreiding avatar Jan 13 '23 16:01 chrisbreiding

Notes for further investigation:

  • Set up a project under the [email protected] account for reproducing this called Issue 25394 Repro. Use the client ID and secret from that.
  • Used the [email protected] user and manually entered the OTP after increasing the timeout for the step after the login to give time for that.

chrisbreiding avatar Jan 13 '23 16:01 chrisbreiding

hey @AaronMcCloskey, @chrisbreiding! 👋 I have the same issue, but I was able to workaround it with an additional check if the window.parent.Cypress is set.

LetItRock avatar Jan 23 '23 22:01 LetItRock

hey @AaronMcCloskey, @chrisbreiding! 👋 I have the same issue, but I was able to workaround it with an additional check if the window.parent.Cypress is set.

This worked great! Thanks for the suggestion.

AaronMcCloskey avatar Jan 23 '23 23:01 AaronMcCloskey

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

cypress-app-bot avatar Jul 23 '23 01:07 cypress-app-bot

I just ran into this today on v12.17.2, still appears to be a problem. Messed up my session management entirely as I change my session storage based on if cypress is running or not.

Thanks for the workaround above.

Hawxy avatar Jul 31 '23 12:07 Hawxy

This is still an issue in Cypress for us (13.3.2), we have our react app login setup with B2C which is supposed to redirect back to our application, on initial component load I can see the window.Cypress object, but when I login back using re-direct window.Cypress is undefined causing login to fail

Siddharth-Ashri avatar Nov 02 '23 21:11 Siddharth-Ashri

hey @AaronMcCloskey, @chrisbreiding! 👋 I have the same issue, but I was able to workaround it with an additional check if the window.parent.Cypress is set.

thanks @LetItRock for this. this has fixed the issue!

Siddharth-Ashri avatar Nov 02 '23 22:11 Siddharth-Ashri

hey @AaronMcCloskey, @chrisbreiding! 👋 I have the same issue, but I was able to workaround it with an additional check if the window.parent.Cypress is set.

Thanks @LetItRock!!

This should be included in the documentation as a suggestion if you do navigate away from the app being tested for some reason, like authentication. In our case we use ADB2C, but it's the same issue, because we need to navigate away from the app to authenticate.

elkinjosetm avatar Jun 14 '24 20:06 elkinjosetm