With Rails 4 and the selenium web driver use Sauce Labs on Travis CI, but not locally
I am filling out the specifications for an open source Rails project and have to run the application in a browser for some of my specifications. I'd like to use Sauce Labs on Travis CI, but without having to rewrite my specs, so I can also use Sauce Labs locally because:
- I dont want to be connected to the internet during development to run my specs.
- Making specs dependent on Sauce Labs would make it impossible for contributors to run specs themselves without setting up their own Sauce Labs account and env vars.
I could not find any documentation describing this scenario. What's the best way to achieve this?
source to share
For those with similar needs, this is what I ended up with:
.travis.yml
env:
global:
- secure: "encrypted sauce username"
- secure: "encrypted sauce secret key"
addons:
sauce_connect: true
before_install:
# install the ed text editor which we use to append
# file contents to a specific line of another file
- sudo apt-get install -y ed
# appends contents of travis/Gemfile.travis to Gemfile
- cat travis/Gemfile.travis >> Gemfile
# adds contents of travis/rails_helper.rb.travis to line 12 of spec/rails_helper.rb
- ed -s spec/rails_helper.rb <<< '12r travis/rails_helper.rb.travis'$'\nw'
Travis /Gemfile.travis
group :test, :development do
gem 'sauce', '~> 3.1.1'
gem 'sauce-connect'
gem 'parallel_tests'
end
Travis / rails _helper.rb.travis
require 'sauce'
require 'sauce/capybara'
# change to "Capybara.default_driver = :sauce" to use sauce
# for ALL feature specs, not just ones marked with "js: true"
Capybara.javascript_driver = :sauce
Sauce.config do |config|
config[:browsers] = [
['Linux', 'Chrome', nil],
# and other OS/browser combos you want to support...
]
end
UPDATE (2014/11/25):
I ended up using a slightly different configuration. I didn't like the fragility of inserting a line number. Instead of having Sauce special inclusions in separate files, I just nested the custom configuration in a conditional expression, depending on whether the environment variable is set SAUCY
to true.
.travis.yml
env:
global:
- secure: "encrypted sauce username"
- secure: "encrypted sauce secret key"
- SAUCY: true
addons:
sauce_connect: true
Gemfile
group :development, :test do
# other gems...
if ENV['SAUCY']
# gems for sauce
gem 'sauce', '~> 3.1.1'
gem 'sauce-connect'
gem 'parallel_tests'
end
end
specs / rails_helper.rb
# after other requires
if ENV['SAUCY']
require 'sauce'
require 'sauce/capybara'
# change to "Capybara.default_driver = :sauce" to use sauce
# for ALL feature specs, not just ones marked with "js: true"
Capybara.javascript_driver = :sauce
Sauce.config do |config|
config[:browsers] = [
['Linux', 'Chrome', nil],
# and other OS/browser combos you want to support...
]
end
end
This way I can also easily use Sauce locally if I choose:
SAUCY=true bundle install
SAUCY=true SAUCE_USERNAME=username SAUCE_ACCESS_KEY=access_key bundle exec rspec
source to share