Ok, cracked it, so that a single or some multiple events are detected by Puppeteer. A couple things weren't apparent to me about how puppeteer works:
- puppeteer page ≈ a browser tab
- that js can be attached to that 'tab'
- puppeteer must browse the test url after setup of the 'tab' (timing is critical)
So now combining previous answers, in Node or on try-puppeteer:
// in node wrap with an asynch iife
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Expose a handler to the page
await page.exposeFunction('onCustomEvent', ({ type, detail }) => {
console.log(`Event fired: ${type}, detail: ${detail}`);
});
// listen for events of type 'status' and
// pass 'type' and 'detail' attributes to our exposed function
await page.evaluateOnNewDocument(() => {
window.addEventListener('status', ({ type, detail }) => {
window.onCustomEvent({ type, detail });
});
});
await page.goto('https://puppet.azurewebsites.net/puppeteer/soEvntSingle.html');
// await page.goto('https://puppet.azurewebsites.net/puppeteer/soEvnt.html');
// await page.waitFor(3000);
await browser.close();
Detects the events fired from that webpage. Where with devTools you'll see the event is fired by a CustomEvent.
<script>
var event = new CustomEvent('status', { detail: 'ok' });
window.addEventListener('status', function(e) {
console.log('status: ', e.detail);
});
window.dispatchEvent(event);
// setTimeout(window.dispatchEvent, 1000, event);
</script>
Switching on commented lines and commenting out respective lines gets Puppeteer to monitor for repeated firing of an event. Then page soEvnt.html fires the CustomEvent 'status' event every second ad nauseam. The Puppeteer test has to terminate at some stage, to report to the terminal (or test infrastructure like Jest) so monitoring is set for 3 seconds.
!Google you can come to me for help any time!