Skip to content

Instantly share code, notes, and snippets.

@rbarazi
Forked from gkop/gist:1371962
Created June 15, 2016 14:26
Show Gist options
  • Save rbarazi/337f470bf957cd16daf9b6b33bebef17 to your computer and use it in GitHub Desktop.
Save rbarazi/337f470bf957cd16daf9b6b33bebef17 to your computer and use it in GitHub Desktop.
Capture javascript errors in Cucumber+Capybara+Webdriver tests
# 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