Created
April 13, 2011 14:23
-
-
Save mcornell/917632 to your computer and use it in GitHub Desktop.
Making Your Cukes More Awesomer With Screenshots
This file contains 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 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) |
This file contains 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
After do |scenario| | |
write_errors([ENV['TEST_SERVER'] || 'devl'], scenario, @browser) if scenario.failed? | |
end |
This file contains 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 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