cypress
cypress copied to clipboard
"Cannot read properties of null (reading 'postMessage')" while switching origin
Current behavior
I get this error in about half of all attempts:
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:
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;
});
facing same issue
Experiencing the same issue when trying to login in our app with Keycloak
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')
We're also have been experiencing same issues with 'postMessage' exception being thrown
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;
});
});
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.
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.
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.
We also have had this problem recently...
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 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
I am seeing this issue on cy.reload(). Is there a fix please?
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://....)
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.
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.
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
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!