Rspec+Capybara optionally change JS driver
Asked Answered
H

4

5

I use poltergeist/phantomjs for CI, but I want to be able to optionally change the JS driver to selenium to watch my test runs locally. Ideally I'd like to have a command line flag for this- default poltergeist, but be able to run rspec --driver=selenium (or something similar)

Any ideas?

Harleigh answered 29/8, 2013 at 2:53 Comment(0)
H
0

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.

Harleigh answered 17/12, 2013 at 15:46 Comment(1)
No, you actually can add a line number :54 to run specific test, at least in 2015, and 2016, but I can't remember if it was possible in 2014 as well.Periodontal
I
0

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

DEBUG = ENV['DEBUG'] || false
if DEBUG
  Capybara.default_driver = :selenium
else
  Capybara.default_driver = :rack_test
  Capybara.javascript_driver = :poltergeist
end

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)
  end
end

If you want to get fancy, you can combine it with this stackoverflow answer: https://mcmap.net/q/693789/-how-can-i-run-selenium-used-through-capybara-at-a-lower-speed 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 DEBUG
end
Isotone answered 11/2, 2014 at 16:47 Comment(0)
F
0

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.rb

Capybara.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.

Flicker answered 25/7, 2014 at 13:59 Comment(0)
S
0

This solution has perfectly worked for me. I set an environment variable and check for its value. Add this to your spec_helper.rb file

if ENV['DRIVER'] =='poltergeist' # use any driver you want to run on circlci
  Capybara.default_driver = :poltergeist
  URL = # use your own end point like http://www.localhost:8080
else
   Capybara.default_driver = :selenium
   URL = # Use your end point
end 
Capybara.configure do |config|
  config.app_host = URL
end

And then on the console run this command

DRIVER=poltergeist rspec //for running test on poltergeist

rspec //for running on selenium

Saguaro answered 13/11, 2015 at 1:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.