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.
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.
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
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
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.
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;