The Sauce Labs Cookbook

Sauce Headless

Front End Performance Testing


External Resources

More Info

Page tree

Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.

Basic Setup


First, let's install the dependencies we'll need for the tutorial:

bash pip install selenium pytest pytest-xdist sauceclient

Note that you must have pip installed. The latest version of sauceclient (v. 0.2.1, 9 July 2015) supports both Python 2 and Python 3.

Running a Test

Let's get started with the simplest thing we can imagine. Let's make sure Google hasn't changed their name.

First, let's set up our browser:

```python from selenium import webdriver

sauce_url = ""

desired_capabilities = { 'platform': "Mac OS X 10.9", 'browserName': "chrome", 'version': "31", }

driver = webdriver.Remote(desired_capabilities=desired_capabilities, command_executor=sauce_url) driver.implicitly_wait(10) ``` Now we have our own pristine Virtual Machine in the Sauce cloud, ready to accept commands!

The implicitly wait method tells the browser to wait a set amount of time (in seconds) for elements to appear on the page before giving up. Without it, slow loading DOMs could cause our tests to fail when they might otherwise pass.

Now, let's use our new browser session!

python driver.get('') assert "Google" in driver.title driver.quit()

Wahoo! We just ran our first test! You can view the results of your tests on your dashboard.

Reporting Test Results

"Wait," you might be asking. "My test says Finished but what happens if it fails?"

Unfortunately, Sauce has no way to determine whether your test passed or failed automatically, since it is determined entirely by your business logic. We can, however, tell Sauce about the results of our tests automatically:

```python from sauceclient import SauceClient

sauce_client = SauceClient("sauceUsername", "sauceAccessKey"), passed=True) ```

Now, if you check your dashboard, you will see the status for the job as Passed.

Other Browsers

In our previous example, we ran our test on a very specific browser / OS combo. Sauce Labs actually supports hundreds of combinations of browsers and operating systems, including mobile! You can see the full list of options on our platform configurator page.

Advanced Usage

The previous example was good as an introduction, but as your tests grow, you are likely to find that you need a testing setup that scales as well.

Let's start with a sample test based on Python's unittest framework. Download the sample test with this command:

bash python -c "import urllib2; open('', 'wb').write(urllib2.urlopen('') .read() .replace('_U_', '\"sauceUsername\"') .replace('_K_', '\"sauceAccessKey\"'))"

Writing tests

Let's look at to see how it works.

The setUp() method initializes the browser testing environment by specifying the browser, version, and platform to test, then creates awebdriver.Remote to run the tests remotely:

python sauce_url = "" self.driver = webdriver.Remote( desired_capabilities=self.desired_capabilities, command_executor=sauce_url % (USERNAME, ACCESS_KEY) )

The webdriver.Remote is a standard Selenium interface, so you can do anything that you could do with a local Selenium test. The only code specific to Sauce Labs was the URL that makes the test run using a browser on Sauce Labs' servers.

Once connected to Sauce Labs, the test runs commands to remote-control a browser. This simple example test simply requests a page and makes a few simple assertions. It runs against several browsers simultaneously, to demonstrate parallelized testing, and reports its status to Sauce Labs when complete.

Running tests

We recommend running tests in parallel using py.test. Since our example test runs in two browsers, lets run both at the same time with -n2:

bash py.test -n2 --boxed

Eventually you will see output like this: ======================= test session starts ======================= platform darwin -- Python 2.7.5 -- pytest-2.5.1 plugins: xdist gw0 [2] / gw1 [2] scheduling tests via LoadScheduling .. ==================== 2 passed in 17.30 seconds ===================== (The exact output will depend on your setup, but you should see all tests passing.)

Include Page

Include Page

Table of Contents


Include Page

Include Page

You can clone this script from the saucelabs-training repository on GitHub:


There are examples using both pytest and unittest frameworks, change directories to the relevant module before running your tests:

Testing with a Proxy

If you're trying to run this script from behind a VPN or a corporate proxy, you must use either IPSec or Sauce Connect Proxy. Once you've downloaded and installed the relevant software, add the following capability to the test script:

Code Block
'tunnelIdentifier': '<tunnel_id>',

Running the Test

  1. Navigate to the root project directory and use pip to install the latest Selenium library for use in the script:

    Code Block
    $ pip install -r requirements.txt
  2. Set your Sauce Labs Credentials as envrionment variables, indicated by the following lines in the script:

    View Git file
  3. Depending on which framework you're using, your commands may be different to run the tests. Use any of the following command based on the chosen framework:


    Code Block
    pytest on-boarding-modules/pytest-examples/


    Code Block
     python -m unittest on-boarding-modules/unittest-examples/