Here is how to do this with rails 4 integration test. Under the hood it uses minitest that have bunch of hooks we can use.
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
# test/test_helper.rb | |
class ActionDispatch::IntegrationTest | |
def after_teardown | |
if !passed? | |
timestamp = "#{Time.zone.now.strftime('%Y-%m-%d-%H:%M:%S')}" | |
screenshot_name = "screenshot-#{timestamp}.png" | |
# Handle CircleCi too | |
screenshot_path = "#{ENV.fetch('CIRCLE_ARTIFACTS', Rails.root.join('tmp/capybara'))}/#{screenshot_name}" | |
page.save_screenshot(screenshot_path) | |
end | |
super | |
end | |
end |
Most of code stolen from http://vumanhcuongit.github.io/testing/2016/01/26/take-screenshot-when-cucumber-test-failed/ and https://github.com/mattheworiordan/capybara-screenshot