-
-
Save rbarazi/337f470bf957cd16daf9b6b33bebef17 to your computer and use it in GitHub Desktop.
Capture javascript errors in Cucumber+Capybara+Webdriver tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in features/support/env.rb | |
require 'selenium/webdriver' | |
# we need a firefox extension to start intercepting javascript errors before the page | |
# scripts load | |
Capybara.register_driver :selenium do |app| | |
profile = Selenium::WebDriver::Firefox::Profile.new | |
# see https://github.com/mguillem/JSErrorCollector | |
profile.add_extension File.join(Rails.root, "features/support/extensions/JSErrorCollector.xpi") | |
Capybara::Selenium::Driver.new app, :profile => profile | |
end | |
After do |scenario| | |
if page.driver.to_s.match("Selenium") | |
errors = page.execute_script("return window.JSErrorCollector_errors.pump()") | |
if errors.any? | |
puts '-------------------------------------------------------------' | |
puts "Found #{errors.length} javascript #{pluralize(errors.length, 'error')}" | |
puts '-------------------------------------------------------------' | |
errors.each do |error| | |
puts " #{error["errorMessage"]} (#{error["sourceName"]}:#{error["lineNumber"]})" | |
end | |
raise "Javascript #{pluralize(errors.length, 'error')} detected, see above" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment