Cucumber and Capybara-Webkit automatic screenshots on failure

  • Download Gist
capybara_webkit_screenshot_env.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
def screen_shot_and_save_page
require 'capybara/util/save_and_open_page'
path = "/#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}"
Capybara.save_page body, "#{path}.html"
page.driver.render Rails.root.join "#{Capybara.save_and_open_page_path}" "#{path}.png"
end
 
begin
After do |scenario|
screen_shot_and_save_page if scenario.failed?
end
rescue Exception => e
puts "Snapshots not available for this environment.\n
Have you got gem 'capybara-webkit' in your Gemfile and have you enabled the javascript driver?"
end
shared_steps.rb
Ruby
1 2 3 4
Then /take a snapshot(| and show me the page)/ do |show_me|
page.driver.render Rails.root.join("tmp/capybara/#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.png")
Then %{show me the page} if show_me.present?
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.