0

When I try the custom-event.js on https://try-puppeteer.appspot.com/ no response is displayed on the log. Surely this demo can be improved by showing some result of the code run ?!?

I've seen the popular answers on Puppeteer: How to listen to object events and the much less appreciated https://stackoverflow.com/a/66713946/2455159 (0 votes!).

NONE OF THE FIRST EXAMPLES work on try-puppeteer and the SECOND ONE DOES !

I get the idea,

First, you have to expose a function that can be called from within the page. Second, you listen for the event and call the exposed function and pass on the event data.

-- but that snippet didn't work (for me ) applied to the https://www.chromestatus.com/features custom event.

What's the bottom line for observing custom events with puppeteer?

Stephan Luis
  • 911
  • 1
  • 9
  • 24
  • can you clarify what is the importance of using puppeteer via try-puppeteer.appspot.com? that page is using the obsolete puppeteer 1.4.0 (from 2018), while 10.4.0 is the latest (some of the answers may use newer API etc., there can be several reasons why they don't work there). it is not clear if you want _try-puppeteer_ to do what you expect (in that case you should raise this in [its Github repo](https://github.com/ebidel/try-puppeteer)) or you want a general answer for - any specific version of - puppeteer? – theDavidBarton Sep 28 '21 at 19:07
  • I thought I'd ask here first for a quick response. My point of using try-puppeteer is to have an easy to access demo. Guess that was intent of that site, shame it's not up to date or even useful for that now. I will manage to get over to their repo, maybe update it with a working demo for events. Can you help? – Stephan Luis Sep 29 '21 at 14:30
  • I see. Eric Bidelman (the author) left Google some years ago but his try-puppeteer demo site [mentioned in the official docs](https://github.com/puppeteer/puppeteer#what-can-i-do) which might be misleading (as it is an abandoned open source project). that can be addressed by (1) proposing a note to this reference (it is using obsolete puppeteer, examples may not work anymore); (2) contributing to Eric Bidelman's project with improved example scripts and/or updating the puppeteer version. I suppose vsemozhebuty's solution could be added to the example script.... – theDavidBarton Sep 29 '21 at 16:39
  • ... anyway, I am happy to contribute with the above-mentioned Github chores. – theDavidBarton Sep 29 '21 at 16:41

2 Answers2

2

There is a difference between the first two examples and the last one: the first ones just illustrate the subscription on an event, but they do not fire this event, so there is no observable reaction there (this can be confusing as a reader would expect the sites to fire the events but they do not). The third example does fire the event.

You can get the first two examples observable even on https://try-puppeteer.appspot.com/ if you add this lines accordingly.

await page.evaluate(() => { document.dispatchEvent(new Event('app-ready')); });
await page.evaluate(() => { document.dispatchEvent(new Event('status')); });
vsemozhebuty
  • 12,992
  • 1
  • 26
  • 26
  • Thanks for answering! Thing I don't understand is that I _am_ able to get puppeteer to respond to the actual page events firing. See [puppet demo](https://puppet.azurewebsites.net/custEvnt.html), by repeat firing of the event and monitoring over time with puppeteer. More to my needs than only checking if the event is there as your code does. Will I face problems doing it this way? (Paste my code into try-puppeteer. ) – Stephan Luis Sep 29 '21 at 14:22
  • Your puppeteer code need not firing the event inside `page.evaluate()` because the site document fires it by itself. It seems OK for me, if I understand correctly. – vsemozhebuty Sep 29 '21 at 14:51
  • Follow up question, how do I get puppeteer to detect a single event, ex. without the repeated firing. Once I remove the setInterval, my puppeteer code does not detect the event firing. I've created a new page [custEvntSingle.html](https://puppet.azurewebsites.net/custEvntSingle.html) – Stephan Luis Sep 29 '21 at 19:44
  • @StephanLuis The link seems broken. – vsemozhebuty Sep 29 '21 at 22:52
  • I've updated to directory, but see my answer below. Let me know if its a good explanation of how this works? – Stephan Luis Sep 30 '21 at 08:58
  • @StephanLuis The explanation seems good) – vsemozhebuty Oct 01 '21 at 05:46
1

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!

Stephan Luis
  • 911
  • 1
  • 9
  • 24
  • Yes, I realise that the code that is on [try-puppeteer](https://try-puppeteer.appspot.com/) is almost identical to mine. I'll try to figure out why it doesn't 'work'. My guess is that the custom event 'name' has changed. And just because of that I've learned soo much. – Stephan Luis Sep 30 '21 at 09:46