cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.68k stars 3.16k forks source link

"Cannot read properties of null (reading 'postMessage')" while switching origin #28728

Open xxluke opened 8 months ago

xxluke commented 8 months ago

Current behavior

I get this error in about half of all attempts:

grafik

In console:

Uncaught (in promise) TypeError: Cannot read properties of null (reading 'postMessage') at PrimaryOriginCommunicator.toSource (cypress_runner.js:169306:12) at PrimaryOriginCommunicator. (index-f74a2fdb.js:146501:43)

While Cypress states that the "error originated from your application code, not from Cypress", it actually does originate from Cypress, here: grafik

Desired behavior

There should be no error. Also it's misleading that the UI says the error doesn't originate from Cypress when it does.

Test code to reproduce

I couldn't make a reproduction in time and I don't want to publish my company's internal url. However, the test code is very simple:

cy.visit('http://localhost:4200/'); // Used to set the global origin to localhost instead of the login provider, which would otherwise be the first visit. Not sure if relevant: localhost would js-redirect to company home page if given enough time.
cy.visit('https://sso-provider.example.com/login');
// Error
cy.origin('https://sso-provider.example.com', { args: loginData }, ({ user, password }) => {
  // ...
});

As you can see on my first screenshot, a XHR request of the first website is still marked as pending, so I guess the ending event was the one that couldn't get through anymore.

Cypress Version

13.6.2

Node version

v18.17.0

Operating System

Windows 11 22H2

Debug Logs

No response

Other

As a workaround, a global event listener seems to work:

Cypress.on('uncaught:exception', (error: Error) => {
  // returning false here prevents Cypress from failing the test
  console.error('Caught error', error);
  if (error.stack?.includes('PrimaryOriginCommunicator.toSource')) {
    return false;
  }
  return true;
});
msyniuk-reenbit commented 7 months ago

facing same issue

Sh1d0w commented 6 months ago

Experiencing the same issue when trying to login in our app with Keycloak

Elte156 commented 6 months ago

We are seeing the same issue as well. Some of our tests end with asserting that the page redirection (to another origin) worked. Then when the next test runs, we get this error intermittently.

TypeError: Cannot read properties of null (reading 'postMessage')

dfakhim commented 5 months ago

We're also have been experiencing same issues with 'postMessage' exception being thrown

Screenshot 2024-04-04 at 11 18 29 AM
yulianovdey commented 3 months ago

In our case listening for uncaught exceptions inside cy.origin stopped Cypress from running into the postMessage issue:

cy.origin(Cypress.env('app_url'), () => {
  cy.on('uncaught:exception', (e) => {
    console.log(e);
    return false;
  });
});
crice88 commented 3 months ago

So I had a similar issue but with our cy.visit() method. Up until recently we had had routes such as cy.visit('logout') or cy.visit('login'). After a minor upgrade to 13.4.0 (and an upgrade of Node 16 to 20), these methods began throwing the same errors that OP was receiving. Our solution was to just include leading forward slashes in our visit param i/e cy.visit('/login')

This doesn't solve OPs issue, but may help someone struggling with this error.

AdeZwart commented 2 months ago

We also have this issue. It's with only 1 very specific test.

The weirdest thing is that it seems like it started randomly. No code changes on the repo, same versions of Node (18.19), Cypress(12.17) and Chrome (120) used.

Jerryhu1 commented 2 months ago

We also have this issue. It's with only 1 very specific test.

The weirdest thing is that it seems like it started randomly. No code changes on the repo, same versions of Node (18.19), Cypress(12.17) and Chrome (120) used.

Exact same issue here, Cypress 12.17, node 18.12. 1 specific test started failing suddenly without changes. Running the test in isolation passes without problems.

tomasz-sobczyk-wttech commented 2 months ago

We also have had this problem recently...

mcmartincerny commented 3 days ago

I ran into the same problem today. Do we know what does the "postMessage" even do? Is it important? Is the workaround mentioned viable?

MKhasib commented 2 days ago

@mcmartincerny My issue was clicking a link that opened a different origin(A different url than cypress). I fixed it by preventing such behavior

I was using this: cy.visit('/...') So changing the origin will mean you can't visit the same docs for your app