Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capybara-webkit: automatically save a screenshot on an RSpec test failure

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)
like image 755
Greg Weber Avatar asked Oct 18 '11 04:10

Greg Weber


3 Answers

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.

like image 152
Matthew O'Riordan Avatar answered Nov 11 '22 00:11

Matthew O'Riordan


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

like image 5
Aleks Avatar answered Nov 11 '22 02:11

Aleks


Found a gist that might help you https://gist.github.com/1156691

like image 2
Mario Visic Avatar answered Nov 11 '22 01:11

Mario Visic