chromiumoxide icon indicating copy to clipboard operation
chromiumoxide copied to clipboard

Getting entries from the browser console

Open terror opened this issue 2 years ago • 1 comments

Working on a project where it would be super useful to know what gets logged to the browser console, and we're handling it like so:

let mut events = page.event_listener::<EventEntryAdded>().await?;

eprintln!("Logging to console...");

page
  .evaluate("console.log('foo')")
  .await?;

while let Some(event) = events.next().await {
  eprintln!("{:?}", event);
}

The problem is that events.next() seems to be hanging forever.

Is this the correct way to get messages from the browser console?

terror avatar May 14 '22 01:05 terror

Same issue here — tried playing around with the enableLog and enableRuntime methods but no luck — other events come through fine.

It isn't much of a solution, but in case it helps I'm using this to work around the issue:

After loading the page:

let console_override = vec![
    "function() {",
    "const c = console; c.events = [];",
    "let l = [c.log, c.warn, c.error, c.debug].map(e => e.bind(c));",
    "let p = (m, a) => c.events.push(`${m}: ${Array.from(a).join(' ')}`)",
    "c.log = function(){ l[0].apply(c, arguments); p('LOG', arguments); }",
    "c.warn = function(){ l[1].apply(c, arguments); p('WRN', arguments); }",
    "c.error = function(){ l[2].apply(c, arguments); p('ERR', arguments); }",
    "c.debug = function(){ l[3].apply(c, arguments); p('DBG', arguments); }",
    "}",
]
.join("\n");

let _ = page.evaluate_function(console_override).await?;

Some time later:

let logs = page.evaluate_function("() => console.events").await?.into_value::<Vec<String>>();

Where logs comes out as ["LOG: Some log", "ERROR: Some console error"]

I really just need to know if things were logged during a test, so this is enough information for me. Having this fixed would be great, however, as the CDP event contains a lot more than just the text :)

bglw avatar May 24 '22 03:05 bglw