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?
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
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 end
–
Cosmotron 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.
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:
- You still get RSpec's normal output for tests that
Pass
- You still get any error messages thrown by watir-webdriver for tests that
Fail
(e.g. timeout, element not found, etc.) - When a javascript error is thrown, it gets added to the RSpec test results output
Cons:
- The first two lines of
print_js_errors
are executed after each test
© 2022 - 2024 — McMap. All rights reserved.
get_log
in the documentation? – Naamana