segment-boneyard / nightmare

A high-level browser automation library.
https://open.segment.com
19.54k stars 1.08k forks source link

detect events fired on document load #1654

Open raquelhortab opened 1 year ago

raquelhortab commented 1 year ago

Hi, I am trying to detect events / function calls (more specifically an alert) that is fired directly when the document loads (it is placed directly on the script tag, not binded by any event listener:

<script language="JavaScript">
    alert("CODELEARN");  
</script>

but it does not seem to work

I got this on the preload script

window.alert = function(message) {
  send('page', 'alert', message)
}

and

beforeEach(() => {
  nightmare.on('page', (type, message) => {
  if (type == 'alert'){
     alert_messages.push(message);
  }
}

I then check in the test whether I got the expected alert_messages.

This works when the alert is triggered by a click or some other event but not with the example above.

Does anyone know a way to check this other than, for example, duplicating the script tag during the test?