This is one of those Rubygems I wish I’d known about a long time ago: capybara-screenshot.
As the name suggests, when a capybara test fails, the gem will automatically take a screenshot of what the browser rendered. I don’t normally need this level of information, as the error is usually fairly obvious. But a handful of times now, the extra information has helped to work through those times where I’m scratching my head and can’t figure out why a test is failing.
I did change the stock behavior of the gem a bit — I don’t have it automatically take a screenshot. Since I don’t normally need that, I didn’t want it cluttering up my filesystem. Instead, I tag scenarios with a little piece of metadata when I want a screenshot. Here’s how I did that:
First, I updated my spec_helper.rb
and told capybara-screenshot not to automatically take screenshots.
Capybara::Screenshot.autosave_on_failure = false
Then I told RSpec to use the scenario’s metadata to decide whether screenshots should be enabled or not.
RSpec.configure do |c|
c.before(:each, type: :feature) do
Capybara::Screenshot.autosave_on_failure = example.metadata[:screenshot]
end
end
Finally, whenever I need a screenshot, I put a piece of metadata on the scenario. Once I’m done debugging, I remove the metadata.
scenario 'closing the map and looking at it later', screenshot: true do
# ...
end
I hope you find this gem as helpful as I have. Thank you to Matthew O’Riordan for the great work.
Truly helpful. The tweak was nice also. Thanks!
Nice !
You should write a pull request for the gem with your code about your metadata, I think it should be accepted.
Glad you find it useful. I am happy to incorporate pull requests if they make sense for other users. Feel free to do a pull request and I’d be happy to consider integrating it.
Thanks
Matthew O’Riordan
[…] Easier debugging with capybara-screenshot […]