Capybara-Webkit: Automatically Save a Screenshot on an Rspec Test Failure

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.

The first thing that I do is set it up so that assets are precompiled into a test directory. I do this by adding the following code into the 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



Leave a reply



Submit