We're hiring!

We're actively seeking developers and designers for our Ann Arbor & Detroit locations.

Easier Debugging with capybara-screenshot

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.
 

Matt Fletcher (74 Posts)

Matt is a software practitioner with Atomic Object.

This entry was posted in Ruby and tagged , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

3 Comments

  1. Posted December 30, 2012 at 6:41 pm

    Truly helpful. The tweak was nice also. Thanks!

  2. Posted December 31, 2012 at 9:35 am

    Nice !

    You should write a pull request for the gem with your code about your metadata, I think it should be accepted.

  3. Posted January 8, 2013 at 12:21 pm

    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

One Trackback

  1. [...] Easier debugging with capybara-screenshot [...]