Skip to content

Instantly share code, notes, and snippets.

@DylanLacey
Created June 12, 2013 11:12
Show Gist options
  • Save DylanLacey/5764448 to your computer and use it in GitHub Desktop.
Save DylanLacey/5764448 to your computer and use it in GitHub Desktop.
Basic instructions for running Cucumber with Sauce, without using the sauce-cucumber gem. If you're not opposed to the sauce-cucumber gem, it gives you job status reporting, naming, and some other niceties.
## features/step_definitions/a_step_definition.rb
Given /$Anything$/
$driver.navigate.to "http://www.something.awesome.com"
end
Feature: Something
@selenium
Scenario: Batman could take Green Lantern in a Fight
Given Anything
## features/support/sauce.rb
## This version sets your job name and updates your job page with the job success status.
## This has to be done by your tests because Selenium doesn't understand what a "pass" or "fail" is
require "json"
require "selenium-webdriver"
require 'selenium/webdriver/remote/http/persistent'
Around('@selenium') do |scenario, block|
http_client = ::Selenium::WebDriver::Remote::Http::Persistent.new
http_client.timeout = 300 # Browser launch can take a while
# Find options for me at https://saucelabs.com/docs/platforms
desired_capabilities = {
:browserName => "FIREFOX",
:version => "21",
:platform => "WINDOWS",
:name => Utilities::Sauce.name_from_scenario(scenario)
}
user = ENV["SAUCE_USERNAME"]
access_key = ENV["SAUCE_ACCESS_KEY"]
webdriver_params = {
:url => "http://#{user}:#{access_key}@ondemand.saucelabs.com:80/wd/hub",
:desired_capabilities => desired_capabilities,
:http_client => http_client
}
$driver = ::Selenium::WebDriver.for(:remote, webdriver_params)
@job_id = $driver.session_id # The JobID, for updating the REST API
http_client.timeout = 90
block.call
$driver.quit #Save your minutes and make your tests finish nicely
job_success = !scenario.failed? # Whether this scenario should pass or fail
# Call our REST API with whatever client library makes you happiest
@uri = "https://#{user}:#{access_key}@saucelabs.com/rest/v1/#{user}/jobs/#{@job_id}"
@body -- {:passed => job_success}.to_json
@headers = {"Content-Length" => 15, "Content-Type" => "application/json"}
# Alternatively, use the SauceWhisk gem
require "sauce_whisk"
SauceWhisk::Jobs.change_status @job_id, job_success
end
module Utilities
class Sauce
def self.name_from_scenario(scenario)
# Special behavior to handle Scenario Outlines
if scenario.instance_of? ::Cucumber::Ast::OutlineTable::ExampleRow
table = scenario.instance_variable_get(:@table)
outline = table.instance_variable_get(:@scenario_outline)
return "#{outline.feature.file} - #{outline.title} - #{table.headers} -> #{scenario.name}"
end
scenario, feature = _scenario_and_feature_name(scenario)
return "#{feature} - #{scenario}"
end
def self.file_name_from_scenario(scenario)
if scenario.instance_of? ::Cucumber::Ast::OutlineTable::ExampleRow
table = scenario.instance_variable_get(:@table)
outline = table.instance_variable_get(:@scenario_outline)
return outline.feature.file
end
return scenario.location.file
end
def self._scenario_and_feature_name(scenario)
scenario_name = scenario.name.split("\n").first
feature_name = scenario.feature.short_name
return scenario_name, feature_name
end
end
end
## features/support/sauce.rb
## This is the basic webdriver setup, with none of the niceties like REST API integration. For that, see full_on_sauce.rb, below
require "selenium-webdriver"
require 'selenium/webdriver/remote/http/persistent'
Around('@selenium') do |scenario, block|
http_client = ::Selenium::WebDriver::Remote::Http::Persistent.new
http_client.timeout = 300 # Browser launch can take a while
# Find options for me at https://saucelabs.com/docs/platforms
desired_capabilities = {
:browserName => "FIREFOX",
:version => "21",
:platform => "WINDOWS"
}
user = ENV["SAUCE_USERNAME"]
access_key = ENV["SAUCE_ACCESS_KEY"]
webdriver_params = {
:url => "http://#{user}:#{access_key}@ondemand.saucelabs.com:80/wd/hub",
:desired_capabilities => desired_capabilities,
:http_client => http_client
}
$driver = ::Selenium::WebDriver.for(:remote, webdriver_params)
http_client.timeout = 90
block.call
$driver.quit #Save your minutes and make your tests finish nicely
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment