我正在填写一个开源 Rails 项目的规范,并且需要在浏览器中运行该应用程序以获得我的一些功能规范。我想在 Travis CI 上使用 Sauce Labs,但无需重写我的规范即可在本地使用 Sauce Labs,因为:
- 我不想在开发过程中必须连接到互联网才能运行我的规范。
- 使规范依赖于 Sauce Labs 将使贡献者无法在不设置自己的 Sauce Labs 帐户和环境变量的情况下自行运行规范。
我找不到详细说明这种情况的文档。实现这一目标的最佳方式是什么?
最佳答案
对于那些有类似需求的人,这就是我最终做的:
.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
更新(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
gem 文件:
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
这样,如果我选择的话,我也可以在本地轻松使用 Sauce:
SAUCY=true bundle install
SAUCY=true SAUCE_USERNAME=username SAUCE_ACCESS_KEY=access_key bundle exec rspec
关于ruby-on-rails-4 - 使用 Rails 4 和 selenium 网络驱动程序,在 Travis CI 上使用 Sauce Labs 但不是在本地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27091706/