Skip to content

Instantly share code, notes, and snippets.

@alvarogarcia7
Created September 27, 2017 09:50
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 alvarogarcia7/d07795e1fc0d2e5686486a85bbe08e68 to your computer and use it in GitHub Desktop.
Save alvarogarcia7/d07795e1fc0d2e5686486a85bbe08e68 to your computer and use it in GitHub Desktop.
Take a screenshot after a failure in Ruby, using Capybara
After do |scenario|
diff = (Time.new - time)
timings << { scenario: scenario.name, timing: diff }
if scenario.failed?
begin
Capybara.using_session(Capybara::Screenshot.final_session_name) do
filename_prefix = Capybara::Screenshot.filename_prefix_for(:cucumber, scenario)
saver = Capybara::Screenshot::Saver.new(Capybara, Capybara.page, true, filename_prefix)
saver.save
saver.output_screenshot_path
# Trying to embed the screenshot into our output."
if File.exist?(saver.screenshot_path)
require "base64"
#encode the image into it's base64 representation
image = open(saver.screenshot_path, 'rb') {|io|io.read}
encoded_img = Base64.encode64(image)
#this will embed the image in the HTML report, embed() is defined in cucumber
embed(encoded_img, 'image/png;base64', "Screenshot of the error")
end
end
rescue Exception => e
puts "Failed to save screenshot"
puts "#{e.message}\n #{e.backtrace.join("\n ")}"
end
end
if ($config.get('FAIL_ON_FIRST_ERROR') == 'true')
Cucumber.wants_to_quit = true if scenario.failed?
end
unless $config.get('TEST_MODE') == 'acceptance'
CookieRemover.delete_all($config.get('LOGIN_URL'))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment