You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature request: Be able to see just console.log/console.error etc without all debug output #113
Indeed I just noticed this as I tried cuprite (coming from selenium + chrome headless). I would really like the ability to display (or get as string) the console output of the browser. Is there a way to get it using some method in an after(:each) maybe? I tried the logger option but it is slightly too verbose ^^.
Little suggestion: maybe when passing a ruby Logger object here you could send your debug with the "debug" level and the browser messages with their respective level so the user can then chose to set the logger level to "info" for example to not get those?
Otherwise a way to get them from a method would be perfect, as we could also filter them, mute them when we want, etc.. (like page.driver.browser.manage.logs.get("browser") from selenium-webdriver + chrome)
I think we need to split them, that would solve all the issues. If I remember correct we pass IO like STDOUT not Ruby logger, maybe it's time to revisit this part, I borrowed it from Poltergeist.
Hi, is there a workaround for this under than using grep to exclude debug lines? Thanks!
You can define a custom logger (which still needs to omit the debug log lines, but is a different approach to grepping stdout as outlined here).
There's an example at rubycdp/ferrum#55 (comment), I used this approach as a starting point. Happy to share more details about what I've done if that'd be helpful.
Hi @nfm , yeah that's what I have been doing with grep. Not sure of how to use the custom logger though. Can you help or should I ask in that thread? Thanks! :)
Happy to share my WIP. I expect you'll want to modify this a bit (we're trialing migrating from Selenium and I want to keep things backwards compatible-ish but haven't quite nailed that down yet):
# Create your own logger classclassFerrumLoggerattr_reader:logsdefinitialize@logs=[]end# Filter out the noise - I believe Runtime.exceptionThrown and Log.entryAdded are the interesting log methods but there might be others you needdefputs(log_str)_log_symbol,_log_time,log_body_str=log_str.strip.split(' ',3)log_body=JSON.parse(log_body_str)if%w[Runtime.exceptionThrownLog.entryAdded].include?(log_body['method'])selenium_compatible_log_message="#{log_body["params"]["entry"]["url"]} - #{log_body["params"]["entry"]["text"]}"@logs << {message: selenium_compatible_log_message,level: log_body["params"]["entry"]["level"]}endenddeftruncate@logs=[]endend# Here's how I'm setting up the cuprite driver with custom logger, you might have a different approach thoughCapybara.register_driver(:cuprite)do |app|
Capybara::Cuprite::Driver.new(app,
**{logger: FerrumLogger.new,# bunch of other options here that aren't relevant to custom logger})end# And then reading the logs from whatever context you're interested in them (eg. `after(:each)`)errors=page.driver.browser.logger.logs
...
Hi @nfm , thanks a lot! I made a couple of changes since I prefer to print the relevant log entries as they happen, plus I added Runtime.consoleAPICalled since that's what seems to be used when I use console.log from my code for debugging.
An update version to log also object, not just strings. Also removed log of Runtime.exceptionThrown since that's already caught by js_errors: true.
classFerrumLoggerdefputs(log_str)_log_symbol,_log_time,log_body_str=log_str.strip.split(' ',3)returniflog_body_str.nil?log_body=JSON.parse(log_body_str)caselog_body['method']when'Runtime.consoleAPICalled'log_body['params']['args'].eachdo |arg|
casearg['type']when'string'Kernel.putsarg['value']when'object'Kernel.putsarg['preview']['properties'].map{ |x| [x["name"],x["value"]]}.to_hendendwhen'Runtime.exceptionThrown'# noop, this is already logged because we have "js_errors: true" in cuprite.when'Log.entryAdded'Kernel.puts"#{log_body['params']['entry']['url']} - #{log_body['params']['entry']['text']}"endendend
Since the preview field used above is truncated and the CDP allows to experimentally get a deep serialized value I came up with this (I use a gem to draw a fancy box, but you get the idea).
Activity
jarthod commentedon Feb 19, 2020
Indeed I just noticed this as I tried
cuprite(coming from selenium + chrome headless). I would really like the ability to display (or get as string) the console output of the browser. Is there a way to get it using some method in anafter(:each)maybe? I tried theloggeroption but it is slightly too verbose ^^.Little suggestion: maybe when passing a ruby
Loggerobject here you could send your debug with the "debug" level and the browser messages with their respective level so the user can then chose to set the logger level to "info" for example to not get those?Otherwise a way to get them from a method would be perfect, as we could also filter them, mute them when we want, etc.. (like
page.driver.browser.manage.logs.get("browser")from selenium-webdriver + chrome)route commentedon Feb 20, 2020
I think we need to split them, that would solve all the issues. If I remember correct we pass IO like STDOUT not Ruby logger, maybe it's time to revisit this part, I borrowed it from Poltergeist.
vitobotta commentedon Jan 2, 2021
Hi, is there a workaround for this under than using grep to exclude debug lines? Thanks!
nfm commentedon Jan 2, 2021
You can define a custom logger (which still needs to omit the debug log lines, but is a different approach to grepping stdout as outlined here).
There's an example at rubycdp/ferrum#55 (comment), I used this approach as a starting point. Happy to share more details about what I've done if that'd be helpful.
vitobotta commentedon Jan 3, 2021
Hi @nfm , yeah that's what I have been doing with grep. Not sure of how to use the custom logger though. Can you help or should I ask in that thread? Thanks! :)
nfm commentedon Jan 3, 2021
Happy to share my WIP. I expect you'll want to modify this a bit (we're trialing migrating from Selenium and I want to keep things backwards compatible-ish but haven't quite nailed that down yet):
vitobotta commentedon Jan 3, 2021
Hi @nfm , thanks a lot! I made a couple of changes since I prefer to print the relevant log entries as they happen, plus I added
Runtime.consoleAPICalledsince that's what seems to be used when I useconsole.logfrom my code for debugging.Thanks again!
rogercampos commentedon Mar 17, 2021
An update version to log also object, not just strings. Also removed log of
Runtime.exceptionThrownsince that's already caught byjs_errors: true.phikes commentedon Aug 9, 2024
Since the preview field used above is truncated and the CDP allows to experimentally get a deep serialized value I came up with this (I use a gem to draw a fancy box, but you get the idea).
route commentedon Oct 19, 2026
going to be addressed by rubycdp/ferrum#658
feat(console): collect console messages, log entries and exceptions p…
feat: console messages API, a reworked logger and browser output kept…