14

I need to capture the console logs (category: info) of a browser using Ruby & Capybara. Until now I have tried using driver.manage.logs.get(:browser) or (:client) but, using this, the result is not what I want. It gives out the interaction results between selenium and browser where I can see my javascript statements sent for execution, but the resulting output fails to get captured.

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
Vivek Khurana
  • 221
  • 1
  • 3
  • 7

3 Answers3

27

Whether or not logs are available when using selenium depends on what browser you are using with Selenium. If you were using Firefox you'd be out of luck since it doesn't support the log retrieval API, however since you're using Chrome they are accessible. The issue you're having is that, by default, only WARN or ERROR level logs are captured. You can change this in the driver registration through the loggingPrefs capability

Capybara.register_driver :logging_selenium_chrome do |app|
  caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs:{browser: 'ALL'})
  browser_options = ::Selenium::WebDriver::Chrome::Options.new()
  # browser_options.args << '--some_option' # add whatever browser args and other options you need (--headless, etc)
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options, desired_capabilities: caps)
end

and then specify to use :logging_selenium_chrome as your driver

 Capybara.javascript_driver = :logging_selenium_chrome # or however else you're specifying which driver to use

which should then allow you to get the logs in your tests with

page.driver.browser.manage.logs.get(:browser)
Thomas Walpole
  • 45,510
  • 5
  • 59
  • 73
  • Thanks for the reply @Thomas Walpole. I tried to implement your solution but I am getting **uninitialized constant Selenium::WebDriver::Chrome::Options (NameError)** constantly. Do you have any solution so I can implement it more fastly. – Vivek Khurana Sep 19 '17 at 12:18
  • @VivekKhurana what version of selenium are you using? – Thomas Walpole Sep 19 '17 at 14:34
  • 1
    @VivekKhurana Update your `selenium-webdriver` gem - The `Chrome::Options` class was added in 3.4.1. The current release is 3.5.2, upgrade to that. – Thomas Walpole Sep 19 '17 at 16:31
  • You saved my day. – Pedro Adame Vergara Mar 27 '18 at 11:19
  • 2
    Does not work for me at `Google Chrome 85.0.4183.83` & `ChromeDriver 85.0.4183.83` `NoMethodError: private method 'log' called for #` – milushov Sep 04 '20 at 06:28
  • See https://stackoverflow.com/q/56522499/120136 for info about `NoMethodError: private method 'log' called for # – L.R. Apr 08 '21 at 19:02
8

Thomas Walpole answer is correct but it seems that nowadays if you are using chrome as your driver you should use

Selenium::WebDriver::Remote::Capabilities.chrome( "goog:loggingPrefs": { browser: 'ALL' } )

Notice goog:loggingPrefs instead of loggingPrefs only with this solution i was able to get console.log printed in the log.

Took me a while and got it from here https://intellipaat.com/community/5478/getting-console-log-output-from-chrome-with-selenium-python-api-bindings after several frustrating attempts.

0Ds0
  • 578
  • 5
  • 18
  • I believe this is the case for Chrome 75+, the original config works for up to Chrome 74. – boylec1986 Mar 24 '20 at 17:20
  • Does not work for me at `Google Chrome 85.0.4183.83` & `ChromeDriver 85.0.4183.83` `NoMethodError: private method 'log' called for #` – milushov Sep 04 '20 at 06:31
0

Not sure that this is what you want, but take a look at https://github.com/dbalatero/capybara-chromedriver-logger.

It helps me identify the problem with dynamic modules import(''). Works both locally and in Github Actions / Circle CI by displaying failed loads of assets (which i believe outputs as console.error).

woto
  • 2,636
  • 1
  • 28
  • 22