Skip to content

Instantly share code, notes, and snippets.

@DylanLacey
Last active January 4, 2016 06:00
Show Gist options
  • Save DylanLacey/437906605a3f792e4443 to your computer and use it in GitHub Desktop.
Save DylanLacey/437906605a3f792e4443 to your computer and use it in GitHub Desktop.
Using Capybara and Minitest with Sauce Labs
# test/support/capybara.rb
# Assuming Capybara needs to be set up from scratch; Otherwise check the
# Capybara integration gem you're using for help.
# The sauce.rb file is in the same directory here, otherwise update the
# require_relative to fit reality
require "capybara/rails"
require_relative "./sauce"
class ActionDispatch::IntegrationTest
# Make the Capybara DSL available in IntegrationTest's
include Capybara::DSL
include ::Sauce
end
# Assuming your Sauce username and Access Key are environment variable
auth = "#{ENV["SAUCE_USERNAME"]}:#{ENV["SAUCE_ACCESS_KEY"]}"
# The Sauce Labs test endpoint
url = "http://#{auth}@ondemand.saucelabs.com/wd/hub"
capabilities = Selenium::WebDriver::Remote::Capabilities.internet_explorer
# Capybara lets you create custom drivers, which we're doing here.
# We're basing it off the built-in Capybara Selenium driver.
# All the arguments except `app` are passed to Selenium.
Capybara.register_driver :sauce do |app|
Capybara::Selenium::Driver.new(app,
:browser => :remote, :url => url,
:desired_capabilities => capabilities)
end
# Make Capybara start on a specific port
Capybara.configure do |config|
config.server_port = 3000
end
# Set Capybara to use Sauce Labs for everything
Capybara.default_driver = :sauce
Capybara.javascript_driver = :sauce
Capybara.current_driver = :sauce
# Add to your gemfile
# If you're using Rails, you might want to add to the :development and :test group
gem "sauce_whisk"
# test/support/sauce.rb
# This module encapsulates session cleanup code, including metadata.
require "sauce_whisk"
module Sauce
# Overwrite minitest's after_teardown method as their docs suggest
def after_teardown
if Capybara.current_session
# Selenium based sessions have unique IDs. These are the same as
# the Sauce Labs job ID and are needed for interacting with the
# REST API. These are only present for Selenium sessions, so if
# you're mixing drivers you might need some sort of check here
# (or ideally, don't mix in the Sauce module for those tests)
@session_id = Capybara.current_session.driver.browser.session_id
# Capybara cleans up the session but re-uses the browser for each test.
# Sauce Labs recommends using a fresh Sauce Labs session every test, so
# we have to clear out the sessions and close them down
Capybara.reset_sessions!
Capybara.current_session.driver.quit
begin
# Sauce Whisk is the REST API wrapper gem. We use it to
# update the name and success status of the test after it's
# finished.
job = SauceWhisk::Job.new({:id => @session_id})
# The passed? and name methods are Minitest methods. You could
# run the name through another method to, say, remove
# underscores and replace them with spaces if you wanted
job.passed = passed?
job.name = name
job.save
rescue
# We're doing nothing in case of REST API errors. You might
# want to take some action here instead.
end
end
end
end
# test/integration/some_test.rb
# Tests don't have to be in integration *or* subclass IntegrationTest
# but for the purposes of this example we are.
require "test_helper"
# Check the comments in the capybara.rb file to see how Sauce is included here
class SomeTest < ::ActionDispatch::IntegrationTest
test "rendered page contains application layout" do
visit("http://localhost")
assert(page.has_selector?("html>head+body"))
assert_match(/Home/, page.title)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment