capybara-webkit: automatically save a screenshot on an RSpec test failure
Found a gist that might help you https://gist.github.com/1156691
Screenshots not being auto generated with Ruby-Rspec
You should be able to do that in an RSpec after block. You can define that in your RSpec configuration, etc and should basically boil down to
after(:each) do |example|
if example.exception
# Do whatever you want to happen on failure
Capybara.current_session.save_and_open_screenshot
end
end
You'd need that block to run before you reset the session, so it needs to be defined after whatever block you have defined for doing the reset. Capybara: save image from loaded page (not screenshot)
Capybara doesn't have a built-in method for that, but you could execute javascript ( page.evaluate_script
) to drawImage(i) into a canvas element and then use getImageData to get the pixel values from the canvas and write to an external file.
save_and_open_page not working with capybara 2.0?
I was able to fix the problem with save_and_open_page after some blog browsing and digging. I have dropped the capybara_screenshot gem from my project. You can see my working code on my github test_capybara_screenshot repository. The solution that I figured out uses some pointers that I found on the capybara github site.
Assumptions:
- You are using rspec for testing.
- Your app is already configured for using the asset pipeline.
spec/spec_helper.rb
file within the RSpec.configure
loop:config.before (scope = :suite) do
%x[bundle exec rake assets:precompile]
end
I specify where the assets are pre-compiled to in the config/environments/test.rb
file:config.assets.prefix = "assets_test" # place test assets in public/assets_test directory
config.action_controller.asset_host = "file://#{::Rails.root}/public"
This makes it so that the test assets are independent of the development assets, and are only generated during a test suite run.If you do this, you will probably want to have git ignore the /public/assets*
directories.
`require': cannot load such file -- capybara/rspec (LoadError)
I hope you did install all required gems? Using bundle install
BTW did you change Gemfile, in that case you would be required to install Gems using bundle install.
You could check list of gems available by gem list
Regarding second part of the question, which testing API to use. It is matter of choice. You could stick with rpsec
and if you find it falls short of your expectation, then look out for change.
Capybara warning: ignoring the provided expectation [...] since it is not a string or a proc
Try to put the with: '0'
inside of the have_field
method:
is_expected.to have_field('ordered_quantity', with: '0')
Related Topics
Why Should @@Class_Variables Be Avoided in Ruby
Rails: Before Process_Action Callback: Authenticate_User! Has Not Been Defined
Ruby: Parse Excel 95-2003 Files
How to Directly Install a Gem from a Git Repository
Convert Durations in Ruby - Hh:Mm:Ss.Sss to Milliseconds and Vice Versa
Paperclip: Integration with Mailboxer Gem
How to Update All of My Products to a Specific User When Seeding
Can Sunspot Search Inside Array
Rvm Finding System Gems' Executables
No Such File to Load - Mechanize
Ruby Dynamic Arguments in Dynamically Created Methods
Need Help on Reading Emails with "Mail" Gem in Ruby
Where Are Keywords Defined in Ruby
How to Use (Ruby) Rgeo to Transform (Unproject) Coordinates
Best/Most Elegant Way to Share Objects Between a Stack of Rack Mounted Apps/Middlewares