chromedp.Poll / PollFunction

I am evaluating JavaScript to determine if the page is fully loaded. I am presently using chromedp.Evaluate and saw that I could use less code by using the chromedp.Poll or chromedp.PollFunction functions.

However, I’m not having any such luck with either:

const (
pollFunction = `
function isInitialized(logMessage) {
if(typeof globalThis.logger === ‘undefined’) {
console.log(‘logger is undefined’);
return false;
}

  console.log('checking logs');
  return globalThis.logger.dump().some(log => typeof log === 'string' && log.includes(logMessage));

}
`

pollStatement = typeof globalThis.logger !== 'undefined' && globalThis.logger.dump().some(log => typeof log === 'string' && log.includes('%s'))
)

This times out:

chromedp.Run(ctx,
chromedp.Poll(fmt.Sprintf(pollStatement, logMessage), nil, chromedp.WithPollingArgs(logMessage), chromedp.WithPollingInterval(pollInterval), chromedp.WithPollingTimeout(pollTimeout)))

I ran this with both the function and the inline statement. With the function, I was hoping to see some statements printed to the console, but I got nothing.

While my evaluate version of this works, I am doing the polling myself and if there is a better way, I’d like to use that.

The Poll and PollFunction references: puppeteer/docs/api.md at v8.0.0 · puppeteer/puppeteer · GitHub and I’ve tried various iterations of syntax to see if it might have been a syntax error.

I think I resolved the issue, I passed in the wrong context. The above code is still correct, but that is in a function and that was being called incorrectly.

I was passing in the original context which was time limited to 5s, but had started earlier and the truthy value I’m waiting for takes about 10s to return, so that is why it never succeeded.