Skip to content

Instantly share code, notes, and snippets.

@mcornell
Created April 13, 2011 14:23
Show Gist options
  • Save mcornell/917632 to your computer and use it in GitHub Desktop.
Save mcornell/917632 to your computer and use it in GitHub Desktop.
Making Your Cukes More Awesomer With Screenshots
module ErrorWriter
def scenario_name(scenario)
if scenario.instance_of?(Cucumber::Ast::OutlineTable::ExampleRow)
scenario_name = scenario.scenario_outline.name.gsub(/[^\w\-]/, ' ')
scenario_name += "-Example#{scenario.name.gsub(/\s*\|\s*/, '-')}".chop
else
scenario_name = scenario.name.gsub(/[^\w\-]/, ' ')
end
scenario_name
end
def file_name(region, scenario_name)
time = Time.now.strftime("%Y-%m-%d-%H%M%S")
# Windows has file name limits
"#{region}-#{time}-#{scenario_name}".slice(0, 250).gsub(/[\,\/]/, '.')
end
def write_errors(region, scenario, browser)
Dir.mkdir("error_pages") unless File.directory?("error_pages")
scenario_name = scenario_name(scenario)
file_name = file_name(region, scenario_name)
File.open("error_pages/#{file_name}.txt", 'wb') { |fh|
fh.write browser.text.to_s
}
File.open("error_pages/#{file_name}.html", 'wb') { |fh|
fh.write browser.html.to_s
}
capture_screenshot(browser, file_name)
end
end
World(ErrorWriter)
After do |scenario|
write_errors([ENV['TEST_SERVER'] || 'devl'], scenario, @browser) if scenario.failed?
end
module Screenshots
if WEBDRIVER
def capture_screenshot(browser, file_name)
Dir.mkdir("screenshots") unless File.directory?("screenshots")
@browser.driver.save_screenshot("screenshots/#{file_name}.png")
end
else
def capture_screenshot(browser, file_name)
#puts "Sorry - no screenshots on your platform yet."
end
end
end
World(Screenshots)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment