6

How can I check a browser console for errors?

For example, I have raised browser(chrome of firefox) with Selenium and I have done some actions with Selenium WebDriver. After than I want to know is there any error in the web console.

Maxim Banaev
  • 886
  • 1
  • 9
  • 22

4 Answers4

1

I tried to take console error like:

def check_console_log
    console_log = @browser.driver.manage.get_log(:browser)
    if console_log != nil
      raise(console_log)
    end
end

and also log it to file

def write_log(file_path)
    work_log = @browser.driver.manage.get_log(:browser)
    messages = ""
    work_log.each { | item | messages += item.message + "\n"}
    File.write( "#{file_path}.log", messages ) unless messages == ""
end
Kirikami
  • 321
  • 4
  • 11
1

I used this code to capture console errors and display to logs

log = Logger.new(STDOUT)

console_log = $driver.manage.logs.get(:browser)
  if console_log != []
      begin
        log.info console_log
          raise Exception.new('Console Errors Found!')
      end    
  else
    puts "No Console errors found!"
  end 
Viraj Pai
  • 61
  • 4
0

I would recommend adding rescue blocks like @Arup recommended. In addition, you can add a debug statement so that you can step through the errors in console

begin 
  puts "Your code here"
rescue => e
  debugger
end

Be sure to add the ruby-debug to your Gemfile or install it with:

$ sudo gem install ruby-debug

Now, when you have any errors, your code will halt and you can view/step through the exceptions in console.

I highly recommend the Better Errors Gem. Check out this episode on RailsCasts about it. This gem allows you to view and interact with exceptions in your web browser. It is a life saver.

Another cool project I can across is rack-webconsole. This gem allows you to literally open up a command line at anytime in your web browser and interact with your Rails app. Not sure if this gem is still supported or no though.

Finally, be sure to get RailsPanel for chrome. This add on gives you some awesome information about your Rails app in a developer tool addon.

Clayton Selby
  • 1,225
  • 1
  • 11
  • 26
0

Version 2.38 of the selenium ruby gem exposes the logging API.

http://selenium.googlecode.com/git/rb/CHANGES

This allows you to access the chrome console logs.

https://code.google.com/p/selenium/wiki/Logging

Here is an example in Java but you should be able to translate easily to ruby;

java example

Community
  • 1
  • 1
Robbie Wareham
  • 3,380
  • 1
  • 20
  • 38
  • You can still access the logs with earlier versions of the Ruby bindings if you are using RemoteWebDriver but you have to monkey patch the code to add the ability to call the API methods – Robbie Wareham Jan 13 '14 at 08:50
  • @KickButtowski This answer is over 3 years old. Apologies for not keeping all links in my answers up to date. Perhaps you could find the new link and submit an edit? – Robbie Wareham Aug 17 '17 at 10:42