December 15, 2015

Automated Testing with Sauce Labs

Sauce Labs is an online provider of cross-platform testing. It offers Selenium testing, mobile testing, and JS unit testing on over 500 OS/browser platforms.

This article will focus on cross-platform testing a web application.

Regardless of whether it's close to a release or during each development iteration, it's good practice to assess how your application behaves on different platforms. For a web application, this means testing it on a wide variety of OS/browser version as possible. For example, let's say you want to support your website on Internet Explorer 11, Safari 8, and Google Chrome and Firefox versions.

This means having the following platform matrix and needing four separate machines:

Browser Windows 7 Windows 8 Mac OS X 10.10 Linux
Internet Explorer 11 X X
Safari 8 X X
Google Chrome X X X X
Firefox X X X X

Of course, users all over the world utilize older browser versions as well, and setting up a local infrastructure to support every OS/browser combination would be very cost inefficient.

In this case, we can opt for a Sauce Labs account. They have over 500 OS/browser platforms from which you can choose.

Embedding a Sauce Labs connection into your project

The first thing you need is a Sauce Labs account. Go to and sign in for a free trial. Then login to your account. For a tutorial on how to test manually on Sauce Labs, you can read more here:

Now we'll look into how we can connect to Sauce Labs and execute a test from an automation framework. The following code samples are written in Ruby with Capybara.

We use environment variables set from the execution command to specify if we want to run the test locally or on Sauce Labs.

if ENV['REMOTE'].to_s =~ /sauce/i

All of the code that follows will sit inside of this branch.

First of all, we need to define the URL that our project will use to communicate with Sauce Labs:

sauce_endpoint = "http://#{TestConstants.sauce_username}:#{TestConstants.sauce_access_key}"

The username is the one you defined when setting up your account, and the access key is listed in your account management panel.

Next, we will define some browsers that we want to have available for executing the test. In Selenium terms, these are called capabilities. Using environment variables, we prepare some capabilities that look like this:

if ENV['BROWSER'] == "ie"
   caps = {
       :platform => "Windows 8",
       :browserName => "Internet explorer",
       :version => "11.0"
 caps = {
     :platform => "Windows 7",
     :browserName => "Chrome",
     :version => "39.0"

Now all we need to do is pass the Sauce Labs URL and the capabilities to Capybara, so they can be used when instantiating a new Selenium test.

Capybara.register_driver :selenium do |app|, :browser => :remote, :url => sauce_endpoint, :desired_capabilities => caps)

Lastly, we use the following command in the command prompt to execute the test on Sauce Labs:

cucumber REMOTE=sauce BROWSER=ie

That should be all you need to know to set up a Sauce Labs account and get started with your cross-platform testing. If you have question or comments, please feel free to leave them in the comments section below!