Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure RSpec to use the Capybara.javascript_driver for all request specs

Is it possible globally configure RSpec to use Capybara's (default or custom) JavaScript driver for all request specs? We sometimes forget to manually add js: true to every request spec and it's kind of annoying.

like image 339
rubiii Avatar asked Oct 31 '12 20:10

rubiii


People also ask

What is RSpec capybara?

Capybara helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and comes with Rack::Test and Selenium support built in.

What is Capybara gem in rails?

Capybara is an integration testing tool for rack based web applications. It simulates how a user would interact with a website.

What is Capybara in Ruby?

Capybara is a web-based test automation software that simulates scenarios for user stories and automates web application testing for behavior-driven software development. It is written in the Ruby programming language.


2 Answers

In spec_helper.rb, set the following:

  config.before(:each) do
    if example.metadata[:type] == :request
      Capybara.current_driver = :selenium # or equivalent javascript driver you are using
    else
      Capybara.use_default_driver # presumed to be :rack_test
    end
  end
like image 106
prusswan Avatar answered Oct 02 '22 08:10

prusswan


For later versions of capybara and rspec, it's important to check for type being "feature"

config.before(:each) do
  if [:request, :feature].include? example.metadata[:type]
    Capybara.current_driver = :poltergeist # or equivalent javascript driver you are using
  else
    Capybara.use_default_driver # presumed to be :rack_test
  end
end

or for RSpec 3 (pass example into the block)

config.before(:each) do |example|
  if [:request, :feature].include? example.metadata[:type]
    Capybara.current_driver = :poltergeist # or equivalent javascript driver you are using
  else
    Capybara.use_default_driver # presumed to be :rack_test
  end
end
like image 30
justingordon Avatar answered Oct 02 '22 06:10

justingordon