cypress icon indicating copy to clipboard operation
cypress copied to clipboard

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

Open xxluke opened this issue 1 year ago • 14 comments

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;
});

xxluke avatar Jan 16 '24 14:01 xxluke

facing same issue

msyniuk-reenbit avatar Feb 06 '24 14:02 msyniuk-reenbit

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

Sh1d0w avatar Feb 28 '24 07:02 Sh1d0w

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')

Elte156 avatar Mar 01 '24 22:03 Elte156

We're also have been experiencing same issues with 'postMessage' exception being thrown Screenshot 2024-04-04 at 11 18 29 AM

dfakhim avatar Apr 04 '24 15:04 dfakhim

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;
  });
});

yulianovdey avatar May 31 '24 18:05 yulianovdey

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.

crice88 avatar Jun 12 '24 17:06 crice88

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.

AdeZwart avatar Jul 01 '24 13:07 AdeZwart

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.

Jerryhu1 avatar Jul 03 '24 07:07 Jerryhu1

We also have had this problem recently...

tomasz-sobczyk-wttech avatar Jul 09 '24 10:07 tomasz-sobczyk-wttech

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

mcmartincerny avatar Sep 09 '24 13:09 mcmartincerny

@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

MKhasib avatar Sep 10 '24 12:09 MKhasib

I am seeing this issue on cy.reload(). Is there a fix please?

DancingCode avatar Oct 14 '24 08:10 DancingCode

I get this as well but I can't put a slash in front of my url as I use a full path, cy.visit(https://....)

Joshua-Hoban-visi-one avatar Jan 26 '25 22:01 Joshua-Hoban-visi-one

Not sure if this helps anyone else, but I had the exact same issue. Reading https://www.cypress.io/blog/mistake-when-using-cy-session-and-how-to-solve-it solved it for me.

TLDR: Our custom login command was not waiting for the redirect from keycloak before storing the session. Not sure if it was the session being corrupted somehow, or simply the act of switching during a redirect. But providing a clean assertion after clicking login, to validate we followed the login redirect, completely fixed things.

StefanVanDyck avatar Jun 02 '25 20:06 StefanVanDyck

any workaround for this issue I am having the same when one of my test redirects to another web page and then visits the same page again it throws the same error after the upgrade to latest version 14.2.7. and its random some times it passes.

achandansoni avatar Aug 11 '25 10:08 achandansoni

any workaround for this issue I am having the same when one of my test redirects to another web page and then visits the same page again it throws the same error after the upgrade to latest version 14.2.7. and its random some times it passes.

You can handle the exception

cy.on('uncaught:exception', ...)

I'm on vacation this week so I can't get you the full snippet but there's cypress docs that'll help

CSharpFiasco avatar Aug 11 '25 11:08 CSharpFiasco

any workaround for this issue I am having the same when one of my test redirects to another web page and then visits the same page again it throws the same error after the upgrade to latest version 14.2.7. and its random some times it passes.

You can handle the exception

cy.on('uncaught:exception', ...)

I'm on vacation this week so I can't get you the full snippet but there's cypress docs that'll help

I did the same workaround as the author and it worked thank you!

achandansoni avatar Aug 11 '25 11:08 achandansoni