Can watir-webdriver capture console errors?
Asked Answered
C

3

16

I am wondering if watir-webdriver as the ability to log the output of any console errors? This would be equivalent to manually opening the console in a browser and watching for JS errors as a page loads. Can I capture this through watir-webdriver and log/error on?

Corfu answered 7/9, 2011 at 20:50 Comment(0)
C
3

If it will be helpful to someone - this solution should work:

def check_console_log
    console_log = @browser.driver.manage.logs.get(:browser)
    if console_log != nil
      raise(console_log)
    end
end
Cosmotron answered 8/12, 2015 at 2:18 Comment(2)
Are you sure this is right? I do not see get_log in the documentation?Naamana
Kind of sure. For now I'm using this functionality in my project, after step failed I'm checking console log and writing log file. This method described in module Selenium module WebDriver module Remote def getLog(type) data = execute :getLog, {}, :type => type.to_s Array(data).map do |l| LogEntry.new l.fetch('level'), l.fetch('timestamp'), l.fetch('message') end endCosmotron
C
1

When using watir-webdriver in combination with Cucumber, the errors, if any, output to an html file that is very well formatted and includes watir-webdriver errors.

This can be accomplished by adding the following flags to your default profile in cucumber.yml:

--color --format pretty --format html -o results.html More info on this file here. Here's some background on Cucumber.

However, if you're using only watir-webdriver from the console, you can redirect watir-webdriver errors to a file by doing the following:

$ ruby your_watir_script.rb 2> watir_debug.log #watir outputs errors as stderr

In most cases, if something in watir fails (e.g an element can't be found) then everything after that will also fail, which is why its useful to have something like Cucumber drive your automation on a scenario basis.

Candler answered 29/5, 2012 at 3:30 Comment(1)
So these errors are the errors that watir itself throws correct? Does watir already capture Javascript Errors from the page and log them?Corfu
C
1

I based my solution on Kirikami's answer, no Cucumber necessary. This approach prints javascript console Errors only (no warnings, info, logs, or debug).

def print_js_errors
  log = @browser.driver.manage.logs.get(:browser)
  errors = log.select{ |entry| entry.level.eql? 'SEVERE' }
  if errors.count > 0
    javascript_errors = errors.map(&:message).join("\n")
    raise javascript_errors
  end
end

Then, if you're using rspec, you can do this:

RSpec.configure do |config|
  config.after :each do
    print_js_errors
  end
end

Pros:

  1. You still get RSpec's normal output for tests that Pass
  2. You still get any error messages thrown by watir-webdriver for tests that Fail (e.g. timeout, element not found, etc.)
  3. When a javascript error is thrown, it gets added to the RSpec test results output

Cons:

  1. The first two lines of print_js_errors are executed after each test
Communard answered 30/6, 2016 at 6:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.