title: how can I get playwright to listen to message events
body: <p>I have a website that communicates through postMessage commands</p>
  <pre><code> window.parent.postMessage(communication, this.origin);
  </code></pre>
  <p>When using playwright, I'm trying to do an &quot;on message&quot; handler</p>
  <pre><code>function eventListener(returnEvent: any): any {
    window.addEventListener('message', function(event: MessageEvent) {
      console.log('Im here');
      if (event.data.messageId === 'payment-method') {
        console.log('setting return event', event);
        returnEvent = event;
        return returnEvent;
      }
    });
  }
  
  ...
  
    let returnEvent: any = {};
    await page.evaluate(eventListener, returnEvent);
    await creditCardDetailsPage.fillFormValid();
    await page.waitForTimeout(5000); //give time for message event to fire
    console.log('event was', returnEvent);
    await expect(returnEvent).toEqual(&lt;MY DATA OBJECT HERE&gt;)
  </code></pre>
  <p>console output</p>
  <pre><code>Im here
  setting return event MessageEvent
  event was {}
  </code></pre>
  <p>I cannot put my expects in the <code>page.evaluate()</code> code, because then it's executing within the context of the javascript  tag that it's injecting, and not within the context of the spec.</p>