-
Notifications
You must be signed in to change notification settings - Fork 115
Swappable Sauce
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.
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 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
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
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.
We recommend using Env Vars for any situation where you're likely to want to use Sauce, including CI.