Rspec+Capybara optionally change JS driver Rspec+Capybara optionally change JS driver selenium selenium

Rspec+Capybara optionally change JS driver


Never ended up finding an answer for this, so here's the hacky solution I came up with:

The only thing I found that I could reliably change was the tagging system. So I call using -t visual tag and then take it away.

In spec/spec_helper.rb

Rspec.configure do |config|  if config.filter_manager.inclusions[:visual]    Capybara.javascript_driver = :selenium    config.filter_manager.inclusions.delete(:visual)  else    Capybara.javascript_driver = :poltergeist  end~rest of rspec config code~

Now you can run your tests with rspec (tests to run) -t visual
The main problem with this is that it will prevent you from running specific tests. You can still do a single file with rspec spec/features/signup_spec.rb -t visual but you cannot add a :54 to run at a specific line number.


You can set an environment variable from the command line that can be used in spec/spec_helper.rb:

DEBUG = ENV['DEBUG'] || falseif DEBUG  Capybara.default_driver = :seleniumelse  Capybara.default_driver = :rack_test  Capybara.javascript_driver = :poltergeistend

Which can then be run from the command line like so:

DEBUG=true rspec spec/features/my_spec.rb:35

This will allow you to specify a specific line number.

You may also have to change your cleanup strategy depending on the capybara driver being used (ie; with database cleaner):

RSpec.configure do |config|  config.before(:suite) do    if DEBUG      DatabaseCleaner.strategy = :truncation    else      DatabaseCleaner.strategy = :transaction    end    DatabaseCleaner.clean_with(:truncation)  endend

If you want to get fancy, you can combine it with this stackoverflow answer: https://stackoverflow.com/a/5150855/95683 to slow down the speed at which selenium runs specs when they're running in DEBUG mode:

config.before(:each) do |group|  set_speed :slow if DEBUGend


I have what I think might be a less hacky solution. I'm basically ripping off Jeff Perrin's solution but making it less complicated.

My DatabaseCleaner is just set to always use truncation, so no need to conditionally configure that part.

Set your javascript_driver this way:

# spec/spec_helper.rbCapybara.javascript_driver = ENV['USE_SELENIUM_FOR_CAPYBARA'] ? :selenium : :webkit

I don't see any need to set Capybara's default driver if we're always explicitly setting javascript_driver to something. (It's possible that Jeff knows something about this that I don't.)

This will use Webkit as the driver unless you have USE_SELENIUM_FOR_CAPYBARA set in your environment.

You should also of course have both the Selenium and Webkit driver gems in your Gemfile if you want RSpec to be able to function with either driver.