Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console event is not triggered by console errors #1512

Closed
carlosesilva opened this issue Dec 1, 2017 · 5 comments
Closed

Console event is not triggered by console errors #1512

carlosesilva opened this issue Dec 1, 2017 · 5 comments

Comments

@carlosesilva
Copy link

Steps to reproduce

Tell us about your environment:

What steps will reproduce the problem?

Please include code that reproduces the issue.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  page.on('console', msg => console.log('PAGE LOG:', msg.text));
  await page.goto('http://do.carlosesilva.com/puppeteer/');
  await page.waitFor(5000);
  await browser.close();
})();

What is the expected result?
I expect the console event to be triggered for all 4 of the console items shown in the screenshot below:
puppeteer-browser-console

What happens instead?
It only gets triggered by the first console item which was from a console.log message.
puppeteer-node-console

@mwhitaker
Copy link

This works for me:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  page.on('console', msg => console.log('PAGE LOG:', msg.text));
  page.on('pageerror', error => {
    console.log(error.message);
   });
  page.on('response', response => {
    console.log(response.status, response.url);
  });
  page.on('requestfailed', request => {
    console.log(request.failure().errorText, request.url);
  });
  await page.goto('http://do.carlosesilva.com/puppeteer/');
  await page.waitFor(5000);
  await browser.close();
})();
200 'http://do.carlosesilva.com/puppeteer/'
PAGE LOG: This is a standard console.log message
Error: This is an error we are forcibly throwing
    at http://do.carlosesilva.com/puppeteer/:22:11
net::ERR_CONNECTION_REFUSED https://do.carlosesilva.com/http-only/sample.png
404 'http://do.carlosesilva.com/puppeteer/this-image-does-not-exist.png'

I think the section on the console event doesn't make it super clear

Also emitted if the page throws an error or a warning.

that you have to listen to different events, e.g. pageerror, to capture the various messages the console shows.

Cheers,
Michael

@carlosesilva
Copy link
Author

That did the trick @mwhitaker, thanks!

@softaria-roman
Copy link

page.on('console', msg => console.log('PAGE LOG:', msg.text));

Should be

page.on('console', msg => console.log('PAGE LOG:', msg.text()));

@joshuaaguilar20
Copy link

joshuaaguilar20 commented Jan 3, 2020

Any one having issues with this its missing the function call
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.on('console', msg => console.log('PAGE LOG:', msg.text()));
page.on('pageerror', error => {
console.log(error.message);
});
page.on('response', response => {
console.log(response.status(), response.url();
});
page.on('requestfailed', request => {
console.log(request.failure().errorText, request.url();
});
})()

@aditodkar
Copy link

@mwhitaker @joshuaaguilar20 @softaria-roman Any suggestions how can we print stack trace for browser errors? error.stack did not work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants