10
votes

How can I automatically save the html and a screenshot when a test fails using capybara-webkit with Rspec? How can I execute a callback when an RSpec test fails.

Bonus points: how can I avoid getting the following error:

Capybara::Driver::Webkit::WebkitInvalidResponseError 

when executing this code:

require 'capybara/util/save_and_open_page'
path = "/#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}"
png = Capybara.save_and_open_page_path + "#{path}.png"
page.driver.render Rails.root.join(png)
3

3 Answers

17
votes

I have written a gem Capybara-Screenshot specifically for this, check out https://github.com/mattheworiordan/capybara-screenshot

It will automatically create screen shots on failed RSpec or Cucumber steps.

4
votes

Capybara provides a function for saving and opening a screenshoot during the testing. You just need to call anywhere in your test:

save_and_open_screenshot

and it will open a picture how the test looks like at that point. No need for any additional gems.

Capybara::save_and_open_screenshot

2
votes