Configure RSpec to use Capybara.javascript_driver for all query specifications

Is it possible to globally configure RSpec to use the Capybara (default or custom) JavaScript driver for all query specifications? We sometimes forget to manually add js: true to each request specification and this is annoying.

+8
ruby rspec capybara acceptance-testing
source share
3 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 
+10
source share

For later versions of capybara and rspec, it is important to check if the type is a "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 to 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 
+8
source share

Refer to this solution if you want to run all test cases right away.

Rspec + Capybara can change JS driver

0
source share

All Articles