The Sauce Labs Cookbook

Sauce Headless

Front End Performance Testing

Insights

External Resources

More Info


Page tree

Versions Compared

Key

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

Table of Contents

Dependencies

First, add the Selenium WebDriver API to your local Python environment using pip:

pip install selenium 

# If you don't have pip installed, run:
sudo easy_install pip

 Code Example

Now let's look at a simple Python test script testing the Google front page. Despite the simplicity of this example test, it actually contains everything you need to know in order to run an automated test on Sauce Labs:

...

Let's look at the test a little closer so you can write one like it, or set your existing tests to run on Sauce.

 Running Tests on Sauce

If you wanted to run Selenium locally, you might initiate a driver for the browser that you want to test on like so:

...

# this is how you set up a test to run on Sauce Labs
desired_cap = {
    'platform': "Mac OS X 10.9",
    'browserName': "chrome",
    'version': "31",
}
driver = webdriver.Remote(
    command_executor='http://philg:45753ef0-4aac-44a3-82e7-b3ac81af8bca@ondemand.saucelabs.com:80/wd/hub',
    desired_capabilities=desired_cap)

 Command Executor

The command_executor code tells our script to use browsers in the Sauce Labs cloud instead of a local browser. Here we simply pass in a URL that contains your Sauce username and access key, which can be found on your dashboard.

 Testing on Different Platforms

...

The desired capabilities are a set of keys and values that will be sent to the Selenium server running in the Sauce Labs cloud. These keys and values tell the Selenium server the specifications of the automated test that you will be running. Using our Platforms Configurator you can easily determine the correct desired capabilities for your test.

 Running Tests Against Local Applications

...

Sauce Connect is a tunneling app that allows you to execute tests securely when testing behind firewalls or on localhost. For more detailed information, please visit see the Sauce Connect docs

 Reporting to the Sauce Labs Dashboard

 Recording Pass/Failure Results

...

# this authenticates you 
from sauceclient import SauceClient
sauce_client = SauceClient("philg", "45753ef0-4aac-44a3-82e7-b3ac81af8bca")

# this belongs in your test logic
sauce_client.jobs.update_job(driver.session_id, passed=True)

 Setting a Build Number

Now, you may want to associate this with a build id in your Continuous Integration pipeline. To do this, just include a build number in your desired capabilities:

...

This is important for organizing tests in our new dashboard. Please see our docs for more information here

 Tagging Your Tests

You can also create custom tags for your tests that you can use to search on the archives page:

desired_cap = {
    'platform': "Mac OS X 10.9",
    'browserName': "chrome",
    'version': "31",
    'build': "build-1234",
    'tags': [ "tag1", "tag2", "tag3" ]
}

 Running Tests in Parallel

Now that you're running tests on Sauce, you may wonder how you can run your tests more quickly. Running tests in parallel is the answer! To do this we will need to use a third party test runner. The most popular third party test runners for Python are py.test and nose.

 Parallel Testing with py.test

...

Visit your dashboard and you should see four tests running at once! Use the py.test docs to learn more about how to properly use py.test for running tests in parallel.

 Parallel Testing with nose

...