Skip to content

Instantly share code, notes, and snippets.

@danielpuglisi
Forked from senny/save_failed_scenarios.rb
Last active December 11, 2015 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielpuglisi/4642977 to your computer and use it in GitHub Desktop.
Save danielpuglisi/4642977 to your computer and use it in GitHub Desktop.
# Hook to save the html page of every failed features into the temp
# # file directory taht it can be checked after cucumber has finished running.
require 'fileutils'
FAILED_FEATURES_PAGES_PATH = File.join Rails.root, 'tmp', 'failed_features'
FAILED_FEATURES_IMAGES_PATH = File.join Rails.root, 'tmp', 'failed_features_images'
FileUtils.rm_rf FAILED_FEATURES_PAGES_PATH
FileUtils.rm_rf FAILED_FEATURES_IMAGES_PATH
module SaveFailedFeatures
def self.included(base)
# p (base.methods - Object.methods).sort
base.after do
if example.exception
begin
FileUtils.makedirs(FAILED_FEATURES_PAGES_PATH) unless File.exists?(FAILED_FEATURES_PAGES_PATH)
FileUtils.makedirs(FAILED_FEATURES_IMAGES_PATH) unless File.exists?(FAILED_FEATURES_IMAGES_PATH)
name = example.description
filename = "#{name.parameterize}.html"
save_page File.join(FAILED_FEATURES_PAGES_PATH, filename)
# if page.driver.respond_to?(:render)
# filename = "#{name.parameterize}.png"
# page.driver.render(File.join(FAILED_FEATURES_IMAGES_PATH, filename))
# end
rescue Rack::Test::Error
end
end
end
end
end
# Hook to save the html page of every failed scenario into the temp
# file directory taht it can be checked after cucumber has finished running.
require 'fileutils'
require 'capybara/util/save_and_open_page'
FAILED_SCENARIO_PAGES_PATH = File.join Rails.root, 'tmp', 'failed_scenarios'
FAILED_SCENARIO_IMAGES_PATH = File.join Rails.root, 'tmp', 'failed_scenarios_images'
FileUtils.rm_rf FAILED_SCENARIO_PAGES_PATH
FileUtils.rm_rf FAILED_SCENARIO_IMAGES_PATH
After do |scenario|
if scenario.failed?
begin
FileUtils.makedirs(FAILED_SCENARIO_PAGES_PATH) unless File.exists?(FAILED_SCENARIO_PAGES_PATH)
FileUtils.makedirs(FAILED_SCENARIO_IMAGES_PATH) unless File.exists?(FAILED_SCENARIO_IMAGES_PATH)
name = scenario.name
name = "#{scenario.scenario_outline.name}_00_#{name}" if scenario.respond_to? :scenario_outline
filename = "#{name.parameterize}.html"
File.open File.join(FAILED_SCENARIO_PAGES_PATH, filename), 'w+' do |f|
f.write Capybara.send(:rewrite_css_and_image_references, page.body)
end
if page.driver.respond_to?(:render)
filename = "#{name.parameterize}.png"
page.driver.render(File.join(FAILED_SCENARIO_IMAGES_PATH, filename))
end
rescue Rack::Test::Error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment