Skip to content

Instantly share code, notes, and snippets.

@abersager
Created June 6, 2013 16:28
Show Gist options
  • Save abersager/5722867 to your computer and use it in GitHub Desktop.
Save abersager/5722867 to your computer and use it in GitHub Desktop.
Rails project setup for running Cucumber tests through Selenium and Sauce Labs

cucumber-rails + Selenium / Sauce Labs

Cucumber tests through Selenium

Download Selenium Server (2.33.0 seems to work nicely). Run it on the host (physical or vm) with the browser under test with:

java -jar selenium-server-standalone-2.33.0.jar

Run the tests:

  • BROWSER_NAME: eg. firefox, ie, chrome, safari

  • DRIVER_HOST: The IP address of the host running the selenium server.

    BROWSER_NAME=firefox DRIVER_HOST=10.0.2.2 bundle exec cucumber -p selenium

Cucumber tests through Sauce

Register with Saucelabs and download their Sauce Connect tool. Run it locally with

java -jar Sauce-Connect.jar <sauce-user> <sauce-key>

Run the tests against a specific browser:

BROWSER="windows xp,firefox" SAUCE_USER_NAME=<sauce_user> SAUCE_API_KEY=<sauce_api_key> bundle exec cucumber -p sauce

Run the tests against a pre-defined matrix of browsers (defined in config/sauce_browser_matrix.json)

bundle exec rake sauce:cucumber
# config/cucumber.yml
<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
%>
default: <%= std_opts %> features
wip: --tags @wip:3 --wip features
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
selenium: --tags ~@no-selenium features CUCUMBER_PROFILE=selenium
sauce: --tags ~@no-selenium features CUCUMBER_PROFILE=sauce
group :test, :development do
gem 'cucumber-rails'
gem "sauce-cucumber"
gem 'parallel_tests'
gem 'sauce-connect'
end
# lib/tasks/sauce.rake
require 'cucumber/rake/task'
require 'parallel'
namespace :sauce do
desc "Run all features against all browsers in parallel"
task :cucumber => :environment do
report_dir = "reports"
FileUtils::mkdir_p(report_dir)
path = Rails.root.join("config", "sauce_browser_matrix.json")
@browsers = JSON.parse(IO.read(path))
Parallel.map(@browsers, :in_threads => @browsers.size) do |browser|
begin
options = browser.pop if browser.length >= 4
report_file = "#{browser.join('_').gsub(' ', '_')}.json"
ENV["CUCUMBER_OPTS"] = [
"--profile sauce",
"--format #{ENV["FORMAT"] || "json"}",
"--out '#{report_dir}/#{report_file}'",
options,
"BROWSER='#{browser.join(',')}'"
].join(' ')
Rake::Task[ "sauce:run_browser_tests" ].execute
puts "Success: #{browser.join('_')}"
rescue Exception => e
puts "Failed: #{browser.join('_')}"
end
end
end
Cucumber::Rake::Task.new(:'run_browser_tests')
end
# features/support/sauce.rb
if ENV["CUCUMBER_PROFILE"] == 'sauce'
require 'sauce/cucumber'
require 'sauce/parallel'
Capybara.default_driver = :sauce
if ENV["APP_HOST"]
# Use provided application location
Capybara.app_host = ENV["APP_HOST"]
else
# Use Capybara server. To work with sauce, Capybara has to run with a
# recognised port. List of ports recognised by Sauce can be found at
# https://saucelabs.com/ruby
Capybara.server_port = 49221
end
Sauce.config do |c|
if ENV['USE_TUNNEL']
start_tunnel_for_parallel_tests(c)
end
platform, name, version = ENV["BROWSER"].split(',')
c[:browsers] = [[platform, name, version]]
end
Around do |scenario, block|
Sauce::Capybara::Cucumber.around_hook scenario, block
end
end
// config/sauce_browser_matrix.json
// see https://saucelabs.com/docs/platforms
[
// [platform browser version options ]
["Windows 7", "Internet Explorer", "9", "--tags ~@no-svg" ],
["Windows XP", "Internet Explorer", "8", "--tags ~@svg" ],
["Windows 7", "Firefox", "21", "--tags ~@no-svg" ]
]
# features/support/selenium.rb
if ENV["CUCUMBER_PROFILE"] == 'selenium'
driver_host = ENV["DRIVER_HOST"]
if !driver_host
raise "No driver host defined"
end
driver_port = ENV["DRIVER_PORT"] || '4444'
browser_name = ENV["BROWSER_NAME"] || 'chrome'
browser_platform = ENV["BROWSER_PLATFORM"]
browser_version = ENV["BROWSER_VERSION"]
app_host = ENV["APP_HOST"] || 'http://limelight.dev.gov.uk'
Capybara.app_host = app_host
Capybara.run_server = false
capabilities = {
:browser_name => browser_name
}
capabilities[:platform] = browser_platform if browser_platform
capabilities[:version] = browser_version if browser_version
require 'selenium-webdriver'
Capybara.register_driver :remote do |app|
Capybara::Selenium::Driver.new(app,
:browser => :remote,
:url => "http://#{driver_host}:#{driver_port}/wd/hub",
:desired_capabilities => Selenium::WebDriver::Remote::Capabilities.new(
capabilities
)
)
end
Capybara.default_driver = :remote
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment