question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'message' event handler running in an iframe is not receiving the data sent by the `postMessage` call

See original GitHub issue

What is your Scenario?

The SUT is using postMessage from within a custom polymer web component to send data to a hidden iframe.

What is the Current behavior?

The message object that is received by the ‘message’ event handler has it’s data field set to undefined (as is it’s origin field)

What is the Expected behavior?

The ‘message’ event handler running within the iframe should receive a message object with it’s data field set to the object that was supplied by the postMessage call

Your complete app code (or attach your test files):

// polymer web component running in the parent top level window:
const configs = { configuration: 'some_important_value' };
this.$.auth.contentWindow.postMessage(configs, '*');

// (okta-auth.js) event handler for the posted message running in the iframe
window.addEventListener('message', function(e) {
  // this line fails as `data` field is undefined
  if(e.data.configuration) {
    // do stuff with the e.data.configuration
  }
});

// test file
import { Selector } from 'testcafe';

fixture`Test Welcome page`
  .page`http://localhost:53660`;


test('When I navigate to the welcome page do I see the Application Gateway bar?', async t => {
  await t
    .wait(10000)
    .typeText('#ClientID', 'P123456')
    .click('#GoButton')
    .typeText('#okta-signin-username', 'blah.blah@blah.com')
    .typeText('#okta-signin-password', 'xxxxxxxx')
    .click('#okta-signin-submit')
    .wait(15000)
    .expect(Selector('application-gateway-app').exists).ok();
});

Screenshots:

image

Your Environment details:

  • node.js version: v10.15.3
  • browser name and version: Chrome 75
  • platform and version: Windows 10 1803

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
miherlosevcommented, Apr 23, 2020

Hi @amal-architrave

To correctly raise an event you need to call 3 methods in sequence:

  • document.createEvent
  • event.initEvent
  • window.dispatchEvent

For more information, see the Creating and triggering events topic.

0reactions
lock[bot]commented, May 5, 2020

This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests. We recommend you ask TestCafe API, usage and configuration inquiries on StackOverflow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Window.postMessage() - Web APIs | MDN
The window.postMessage() method safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it ...
Read more >
window.postMessage not working from iframe to parent ...
I've had exactly the same problem and have solved it by moving the "script" section above the iframe declaration.
Read more >
Using JavaScript and window.postMessage() - In Plain English
window.postMessage() is a safe way to send messages between windows in different domains or origins. One can also post to an IFrame. The...
Read more >
Cross-window communication - The Modern JavaScript Tutorial
To receive a message, the target window should have a handler on the message event. It triggers when postMessage is called (and targetOrigin ......
Read more >
HTML5 window.postMessage - David Walsh Blog
If the destination window has changed, the message will not be sent. We've also created an event handler for receiving a message. It's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found