Search code examples
ruby-on-rails-4selenium-webdriverrspec-railstravis-cisaucelabs

With Rails 4 and selenium web driver, use Sauce Labs on Travis CI but not locally


I'm filling out specs for an open source Rails project and need to run the app in a browser for some of my feature specs. I'd like to use Sauce Labs on Travis CI, but without having to rewrite my specs to also use Sauce Labs locally, because:

  1. I don't want to have to be connected to the Internet during development to run my specs.
  2. Making the specs reliant on Sauce Labs would make it impossible for contributors to run the specs themselves without setting up their own Sauce Labs account and env vars.

I couldn't find documentation detailing this scenario. What's the best way of achieving this?


Solution

  • For those with similar needs, this is what I ended up doing:

    .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 in my final solution. I didn't like the brittleness of inserting at a line number. Instead of having special Sauce inclusions in separate files, I just nested special configuration in a conditional, depending on whether an environment variable SAUCY is set 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
    

    spec/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 to with:

    SAUCY=true bundle install
    SAUCY=true SAUCE_USERNAME=username SAUCE_ACCESS_KEY=access_key bundle exec rspec