问题
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?
回答1:
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.
回答2:
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://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 DEBUG
end
回答3:
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.
回答4:
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
来源:https://stackoverflow.com/questions/18501574/rspeccapybara-optionally-change-js-driver