Skip to content

Instantly share code, notes, and snippets.

@DylanLacey
Created May 10, 2018 04:51
Show Gist options
  • Select an option

  • Save DylanLacey/65e7fb020876d7b3d028c096f21762f4 to your computer and use it in GitHub Desktop.

Select an option

Save DylanLacey/65e7fb020876d7b3d028c096f21762f4 to your computer and use it in GitHub Desktop.
Capybara for Sauce Labs
module SauceCapybara
class Driver < Capybara::Selenium::Driver
# No need to clean up after sessions & no need to wait until all
# processes have ended to call close (and doing so can cause timeout
# problems on Sauce) so just quit now.
def reset!
quit
end
end
end
require 'selenium/webdriver'
require 'capybara/rails'
require 'capybara/rspec'
require 'sauce_whisk'
require_relative 'sauce_capybara'
RSpec.configure do |config|
config.before :each, js: true do |scenario|
Capybara.register_driver :remote do |app|
capabilities = # {Put your Sauce Labs Desired Capabilities here as a hash}
capabilities[:name] = scenario.full_description # Give the Sauce Labs test a decent name
client = Selenium::WebDriver::Remote::Http::Default.new
client.open_timeout = 1500 # Optional; Deals with long session start times
server_url = "https://#{ENV['SAUCE_USERNAME']}:#{ENV['SAUCE_ACCESS_KEY']}" +'@ondemand.saucelabs.com:443/wd/hub'
SauceCapybara::Driver.new (
app,
browser: :remote,
url: server_url,
desired_capabilities: capabilities,
http_client: client
)
end
Capybara.current_driver = :remote
Capybara.default_driver = :remote
jobname = scenario.full_description
Capybara.session_name = "#{jobname} - #{ENV['platform']} - " +
"#{ENV['browserName']} - #{ENV['version']}"
session = Capybara.current_session
# Capture the Sauce Labs job ID
begin
@session_id = session.driver.browser.session_id
rescue Selenium::WebDriver::Error::UnknownError, URI::InvalidURIError
sleep(1)
@session_id = session.driver.browser.session_id
end
puts "SauceOnDemandSessionID=#{@session_id} job-name=#{jobname}" # Optional; Used for the Jenkins plugin
end
# Use the Sauce Labs REST API to mark jobs passed or failed
config.after :each, js: true do |scenario|
if scenario.exception
SauceWhisk::Jobs.fail_job @session_id
else
SauceWhisk::Jobs.pass_job @session_id
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment