Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Swappable Sauce

Dylan Lacey edited this page Jul 16, 2013 · 1 revision

Using an environment variable to switch between local and remote testing

The easiest way to run your tests both locally and remotely, is by using an environment variable. This document covers how to run your tests against Sauce Labs and locally, targeting the setup in the Parallel Testing tutorial; but is relevant to most test suites.

We'll be using the "RUN_ON_SAUCE" environment variable in this example, so make sure it's not already part of your environment variables.

Rspec Tags

If you've not moved to using tags to run your tests on Sauce Labs, now is a great time to do so. We're going to use the value of the environment variable as the value of the tag, so when it's undefined, our tests will run locally:

describe "spring onion", :sauce => ENV["RUN_ON_SAUCE"] do
  # SNIP #
end

Sauce Connect

Sauce Connect starts by default, any time you run your test suite. Let's configure that to rely on the environment variable.

# spec/sauce_helper.rb or wherever your Sauce.config block is
Sauce.config do |c|
  c[:start_tunnel] = ENV["RUN_ON_SAUCE"]
end

Capybara

Finally, change your Capybara driver to run with whatever alternative drivers you want.

# spec/spec_helper.rb or some other set-up location
if ENV["RUN_ON_SAUCE"]
  Capybara.default_driver = :sauce
  Capybara.javascript_driver = :sauce
else
  Capybara.default_driver = :webkit
  Capybara.javascript_driver = :selenium
end

Running your tests

Prepend RUN_ON_SAUCE=true to whatever command usually kicks off your tests:

RUN_ON_SAUCE=true rake sauce:spec
RUN_ON_SAUCE=true rspec

Make sure, if you're using a parallel testing suite, that your test environment can deal with the parallel browser load. If not, you might want to switch to testing in serial for some tests.

CI

We recommend using Env Vars for any situation where you're likely to want to use Sauce, including CI.