Created
April 2, 2009 20:19
-
-
Save tomtt/89408 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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 visit_features(features) | |
| super | |
| puts "Screenshots created in %s" % @screenshots_directory | |
| end | |
| end | |
| end | |
| end | |
| World do |world| | |
| world.extend ResponseBodyHtmlWriter | |
| world | |
| end | |
| After do |scenario| | |
| if scenario.has_tags?(["screenshot"]) | |
| write_response_body_to_html_file(scenario) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment