4
votes

I want to output a screen shot of the page after a failed scenario.

I'm using capybara, rspec and launchy - my gemfile has

group :development, :test do
    gem 'rspec-rails'
    gem 'capybara'
    gem 'launchy'
end 

I've seen various suggestions which include the following code

After do |scenario|
  save_and_open_page if scenario.failed?
end

So, I've created a spec\support\env.rb file into which I've put this code (and that is all, there is nothing else in that file). Now when I run

bundle exec rspec

I get

C:/Working/x/spec/support/env.rb:1:in `<top (required)>': undefined method `After' for main:Object (NoMethodError)

If I comment out the lines in env.rb, then the tests run as I'd expect.

What am I missing?

Thanks

Versions:

  • rails 4
  • rspec 2.14.1
  • capybara 2.1.0
  • launchy 2.3.0
3

3 Answers

2
votes

Try wrapping it in a Rspec.configure block, something like:

RSpec.configure do |config|
  config.after { |example_group| save_and_open_page if example_group.example.exception }
end
2
votes

@ejosafat - extra "example." in you answer, so I used it as following:

RSpec.configure do |config|
  config.after { |example_group| save_and_open_page if example_group.exception }
end
1
votes

There is a gem, you just need to add this to your Gemfile:

gem 'capybara-screenshot', :group => :test

All the instructions can be found here:- https://github.com/mattheworiordan/capybara-screenshot