javascriptnode.jschromiumpuppeteerbrowser-testing

puppeteer : console.log in evaluate triggers error "Unhandled promise rejection"


In puppeteer , when i want to use console.log from evaluate , it triggers me an error

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({headless: false});

  const page = await browser.newPage();
  page.on('console', msg => console.log('PAGE LOG:', ...msg.args));
  await page.goto('http://google.com',  {waitUntil: 'load'});
  await page.evaluate(async() => console.log('url is ${location.href}'));
  browser.close();
})();  

(node:70544) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: undefined is not iterable (node:70544) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Do you know how to handle this?


Solution

  • The msg.args value is undefined, so when you're trying to use the spread operator (...msg.args), it is failing.

    Either log msg.args, or wrap it in a null / undefined check