Skip to content

Instantly share code, notes, and snippets.

@tomtt
Created April 2, 2009 20:41
Show Gist options
  • Select an option

  • Save tomtt/89423 to your computer and use it in GitHub Desktop.

Select an option

Save tomtt/89423 to your computer and use it in GitHub Desktop.
module ResponseBodyHtmlWriter
def create_directory_for_html_files
@time ||= Time.now.to_s(:number)
unless @screenshots_directory
@screenshots_directory = File.expand_path(File.join(RAILS_ROOT,
'public',
'screenshots',
@time))
FileUtils.mkdir_p(@screenshots_directory)
end
@screenshots_directory
end
def file_name_for_html_file(scenario)
scenario_name = scenario.instance_eval("@name")
scenario_name_slugged = scenario_name.strip.downcase.tr(' ', '_').squeeze('_')
feature_filename = scenario.instance_eval("@feature").file
feature_file_basename = File.basename(feature_filename, '.feature')
screen_shot_name = "%s__%s.html" % [feature_file_basename,
scenario_name_slugged]
file_name = File.expand_path(File.join(create_directory_for_html_files,
screen_shot_name))
file_name
end
def write_response_body_to_html_file(scenario)
file_name = file_name_for_html_file(scenario)
if File.exist?(file_name)
raise "There was already a screenshot created called \"%s\"" % file_name
end
File.open(file_name, 'w') do |file|
file << response.body
end
puts "Screenshot: %s" % file_name
end
end
module Cucumber
module Formatter
class ScreenShots < Ast::Visitor
def initialize(step_mother, io, options)
super(step_mother)
end
def included_by_tags?(node)
node.has_tags?(["screenshot"])
end
end
end
end
World do |world|
world.extend ResponseBodyHtmlWriter
world
end
After do |scenario|
write_response_body_to_html_file(scenario)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment