Captain_Meow_Meow
Captain_Meow_Meow

Reputation: 2541

In Puppeteer how to capture Chrome browser log in the console

I'm trying to collect Chrome browser logs: browser-issued warnings such as deprecation and interventions. For example, for site https://uriyaa.wixsite.com/corvid-cli2:

A cookie associated with a cross-site resource at http://wix.com/ was set without the `SameSite` attribute.
A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`.
You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.

I thought the following code would do the trick but it only catches logs generated by the page code.

(async ()=> {
    const browser = await puppeteer.launch({dumpio: true});
    const page = await browser.newPage();
    page.on('console', msg => {
        for (let i = 0; i < msg._args.length; ++i)
            console.log(`${i}: ${msg._args[i]}`);
    });
    await page.goto('https://uriyaa.wixsite.com/corvid-cli2', {waitUntil: 'networkidle2', timeout: 20000});
    await page.screenshot({path: 'screenshot.png'});
    await browser.close();
})();

bellow is not relevant as I thought as reportingobserver does not catch the chrome info on cookies without sameSite: Reading on the subject led me to https://developers.google.com/web/updates/2018/07/reportingobserver but I'm not sure how to use it, using the example int the browser console didn't work.

I'm not sure in which context the observer code should be used or if the browser need a flag to activate the reporting API. Or if this is the way to got about it.

help is welcomed.

Upvotes: 10

Views: 8656

Answers (2)

Javi Marz&#225;n
Javi Marz&#225;n

Reputation: 1340

When you launch Puppeteer browser, inside the options object, you should set dumpio to true:

await puppeteer.launch({ dumpio: true });

This will basically "pipe browser process stdout and stderr into process.stdout and process.stderr", which means it will redirect browser logs to whatever main process, server, etc. you are running.

You can see this and other launch options you can use when launching Puppeteer in here: https://www.puppeteersharp.com/api/PuppeteerSharp.LaunchOptions.html

Upvotes: 6

vsemozhebuty
vsemozhebuty

Reputation: 13812

Presumably, the 'console' event only catches console.log() and similar calls from the pages. But it seems you can catch warnings from the browser via CDPSession with Log Domain. Unfortunately, it works for me only with a headful browser:

'use strict';

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch({ headless: false });
    const [page] = await browser.pages();
    const cdp = await page.target().createCDPSession();

    await cdp.send('Log.enable');

    cdp.on('Log.entryAdded', async ({ entry }) => {
      console.log(entry);
    });

    await page.goto('https://uriyaa.wixsite.com/corvid-cli2');
  } catch (err) {
    console.error(err);
  }
})();

And one of the entries:

{
  source: 'other',
  level: 'warning',
  text: 'A cookie associated with a cross-site resource at http://www.wix.com/ was set without the `SameSite` attribute. It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.',
  timestamp: 1589058118372.802,
  url: 'https://uriyaa.wixsite.com/corvid-cli2'
}

Upvotes: 8

Related Questions