8

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.

rubiii
  • 6,903
  • 2
  • 38
  • 51

3 Answers3

10

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
prusswan
  • 6,853
  • 4
  • 40
  • 61
  • That works, but I'd like to limit this to request specs. Of course I could put this line in every single request spec file, but I'd like to configure it globally. Kind of like you can include specific Modules for certain spec types. – rubiii Oct 31 '12 at 20:39
  • 1
    is there a similar workaround for rspec 3? "example" isn't defined now – noli Feb 11 '14 at 23:36
8

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
justingordon
  • 12,553
  • 12
  • 72
  • 116
  • If you are using RSpec >= 3, you will need to write the first line of this like `config.before(:each) do |example|` to get the same behavior. More via @joshua-muheim here: http://stackoverflow.com/a/24571607/2948832 – styger Dec 16 '14 at 20:52
0

Refer to this solution if you want to run all test cases at once.

Rspec+Capybara optionally change JS driver

hacker_1989
  • 303
  • 4
  • 15