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.

3
  • What problem with Ruby's own begin..rescue..end ? Commented Jan 10, 2014 at 11:42
  • 2
    Nothing. My app not failed. I want check web console. Commented Jan 10, 2014 at 11:56
  • Okay... if you get your answer,,notify me.. I would like to know if it possible or not. Commented Jan 10, 2014 at 12:04

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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 

Comments

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

2 Comments

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
@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?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.