使用Rails 4和selenium web驱动程序,在Travis CI上使用Sauce Labs,但不在本地使用

时间:2014-11-23 17:03:39

标签: ruby-on-rails-4 selenium-webdriver rspec-rails travis-ci saucelabs

我正在填写开源Rails项目的规范,需要在浏览器中运行应用程序以获取我的一些功能规范。我想在Travis CI上使用Sauce Labs,但没有不得不重写我的规范以在本地使用Sauce Labs,因为:

  1. 我不想在开发过程中连接到互联网来运行我的规格。
  2. 如果没有设置自己的Sauce Labs帐户和env vars,那么让规格依赖于Sauce Labs将使贡献者无法自行运行规范。
  3. 我找不到详细说明此方案的文档。实现这一目标的最佳方式是什么?

1 个答案:

答案 0 :(得分:4)

对于那些有类似需求的人来说,这就是我最终要做的事情:

<强> .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'

<强>特拉维斯/ Gemfile.travis

group :test, :development do
  gem 'sauce', '~> 3.1.1'
  gem 'sauce-connect'
  gem 'parallel_tests'
end

<强>特拉维斯/ 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

更新(2014/11/25):

我最终在最终解决方案中使用了略微不同的配置。我不喜欢插入行号的脆弱性。我没有在单独的文件中包含特殊的Sauce包含,而是在条件中嵌套特殊配置,具体取决于环境变量SAUCY是否设置为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

<强>规格/ 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

这样,如果我选择使用,我也可以在本地轻松使用Sauce:

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