sauceclient

Python client library for Sauce Labs API.

https://travis-ci.org/cgoldberg/sauceclient.svg?branch=master

About sauceclient

sauceclient is a Python client library for Sauce Labs API. You can manage a Selenium test environment and retrieve test assets from Sauce.

The API gives remote access to:

  • Test Jobs & Assets (Results, Logs, Videos, Screenshots)
  • Account
  • Platform Information
  • JavaScript Unit Tests
  • Temporary Storage
  • Tunnels

Note: sauceclient is not used for running tests on Sauce Labs’ services. Test execution is handled by Selenium WebDriver.

About Sauce Labs

Sauce Labs is a service for running remote Selenium WebDriver tests. It ghas VM’s with 800+ browser/OS combinations, allowing comprehensive cross-browser cross-platform test coverage.

Install

  • with pip:

    pip install sauceclient
    

Example Usage

  • public access:

    import sauceclient
    
    sc = sauceclient.SauceClient()
    status = sc.information.get_status()
    
  • with authorization:

    import sauceclient
    
    sc = sauceclient.SauceClient(
        'sauce-username',
        'sauce-access-key',
    )
    jobs = sc.jobs.get_jobs(full=True, limit=5)
    

Compatibility Note

Due to changes in the SauceLabs REST API, some of sauceclient’s classes and methods have been changed or renamed since the pre-1.0 releases.


Python Module Reference

sauceclient Module

Sauce Labs REST API client

Copyright (c) 2013-2017 Corey Goldberg

This file is part of: sauceclient https://github.com/cgoldberg/sauceclient

License: Apache Version 2.0 http://www.apache.org/licenses/LICENSE-2.0

Sauce Labs REST API documentation: http://saucelabs.com/docs/rest

class sauceclient.Account(client)[source]

Account Methods

These methods provide user account information and management. - https://wiki.saucelabs.com/display/DOCS/Account+Methods

change_access_key()[source]

Change access key of your account.

create_user(username, password, name, email)[source]

Create a sub account.

get_activity()[source]

Check account concurrency limits.

get_concurrency()[source]

Check account concurrency limits.

get_siblings()[source]

Get a list of sibling accounts associated with provided account.

get_subaccount_info()[source]

Get information about a sub account.

get_subaccounts()[source]

Get a list of sub accounts associated with a parent account.

get_usage(start=None, end=None)[source]

Access historical account usage data.

get_user()[source]

Access basic account information.

class sauceclient.Analytics(client)[source]

Analytics Methods

These methods provide user account information and management. - https://wiki.saucelabs.com/display/DOCS/Analytics+Methods

get_concurrency(start=None, end=None, interval=None, time_range=None, scope=None, owner=None, status=None, pretty=False)[source]
get_tests(start=None, end=None, size=None, time_range=None, scope=None, owner=None, status=None, pretty=False, error=None, build=None, skip=None, missing_build=False)[source]
class sauceclient.Information(client)[source]

Information Methods

Information resources are publicly available data about Sauce Lab’s service. - https://wiki.saucelabs.com/display/DOCS/Information+Methods

get_appium_eol_dates()[source]

Get a list of Appium end-of-life dates. Dates are displayed in Unix time.

get_platforms(automation_api='all')[source]

Get a list of objects describing all the OS and browser platforms currently supported on Sauce Labs.

get_status()[source]

Get the current status of Sauce Labs services.

class sauceclient.JavaScriptTests(client)[source]

JavaScript Unit Testing Methods

js_tests(platforms, url, framework)[source]

Start your JavaScript unit tests on as many browsers as you like with a single request.

js_tests_status(js_tests)[source]

Get the status of your JS unit tests.

class sauceclient.Jobs(client)[source]

Job Methods

delete_job(job_id)[source]

Removes the job from the system with all the linked assets.

delete_job_assets(job_id)[source]

Delete all the assets captured during a test run.

get_auth_token(job_id, date_range=None)[source]

Get an auth token to access protected job resources.

https://wiki.saucelabs.com/display/DOCS/Building+Links+to+Test+Results

get_job(job_id)[source]

Retreive a single job.

get_job_asset_url(job_id, filename)[source]

Get details about the static assets collected for a specific job.

get_job_assets(job_id)[source]

Get details about the static assets collected for a specific job.

get_jobs(full=None, limit=None, skip=None, start=None, end=None, output_format=None)[source]

List jobs belonging to a specific user.

stop_job(job_id)[source]

Terminates a running job.

update_job(job_id, build=None, custom_data=None, name=None, passed=None, public=None, tags=None)[source]

Edit an existing job.

class sauceclient.SauceClient(sauce_username=None, sauce_access_key=None, apibase=None)[source]

SauceClient class.

get_auth_string()[source]

Create auth string from credentials.

make_auth_headers(content_type)[source]

Add authorization header.

make_headers(content_type='application/json')[source]

Create content-type header.

request(method, url, body=None, content_type='application/json')[source]

Send http request.

exception sauceclient.SauceException(*args, **kwargs)[source]

SauceClient exception.

class sauceclient.Storage(client)[source]

Temporary Storage Methods

get_stored_files()[source]

Check which files are in your temporary storage.

upload_file(filepath, overwrite=True)[source]

Uploads a file to the temporary sauce storage.

class sauceclient.Tunnels(client)[source]

Tunnel Methods

delete_tunnel(tunnel_id)[source]

Get information for a tunnel given its ID.

get_tunnel(tunnel_id)[source]

Get information for a tunnel given its ID.

get_tunnels()[source]

Retrieves all running tunnels for a specific user.

tests Module

class tests.TestSauce(methodName='runTest')[source]
setUp()[source]

Hook method for setting up the test fixture before exercising it.

test_account_change_access_key(**keywargs)[source]
test_account_create_user(**keywargs)[source]
test_account_get_activity(**keywargs)[source]
test_account_get_concurrency(**keywargs)[source]
test_account_get_siblings(**keywargs)[source]
test_account_get_subaccount_info(**keywargs)[source]
test_account_get_subaccounts(**keywargs)[source]
test_account_get_usage(**keywargs)[source]
test_account_get_user(**keywargs)[source]
test_analytics_get_concurrency(**keywargs)[source]
test_analytics_get_tests(**keywargs)[source]
test_bad_request(**keywargs)[source]
test_information_get_appium_eol_dates(**keywargs)[source]
test_information_get_platforms(**keywargs)[source]
test_information_get_status(**keywargs)[source]
test_javascript_js_tests(**keywargs)[source]
test_javascript_js_tests_status(**keywargs)[source]
test_jobs_delete_job(**keywargs)[source]
test_jobs_delete_job_assets(**keywargs)[source]
test_jobs_get_auth_token(**keywargs)[source]
test_jobs_get_job(**keywargs)[source]
test_jobs_get_job_asset_url(**keywargs)[source]
test_jobs_get_job_assets(**keywargs)[source]
test_jobs_get_jobs(**keywargs)[source]
test_jobs_stop_job(**keywargs)[source]
test_jobs_update_job(**keywargs)[source]
test_storage_delete_tunnel(**keywargs)[source]
test_storage_get_stored_files(**keywargs)[source]
test_storage_upload_file(**keywargs)[source]
test_tunnels_get_tunnel(**keywargs)[source]
test_tunnels_get_tunnels(**keywargs)[source]

Running Selenium WebDriver

Selenium WebDriver in Python (local and remote)

Requirements

  • Python 2.7 or 3.x
  • Selenium 2.x bindings (pip install selenium)

Selenium - Local WebDriver example

Let’s start with a very simple Selenium WebDriver example…

consider the following Python code:

#!/usr/bin/env python

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://saucelabs.com/test/guinea-pig')
driver.quit()

This code uses webdriver.Firefox(), to invoke the local FireFox driver.

Selenium - Remote WebDriver example

Instead of running locally via webdriver.Firefox(), we can use webdriver.Remote(), and have it execute from a remote machine running Selenium Server. In this case, the Sauce Labs cloud:

#!/usr/bin/env python

from selenium import webdriver

SAUCE_USERNAME = 'your-username-string'
SAUCE_ACCESS_KEY = 'your-access-key-string'

driver = webdriver.Remote(
    desired_capabilities=webdriver.DesiredCapabilities.FIREFOX,
    command_executor='http://%s:%s@ondemand.saucelabs.com:80/wd/hub' %
    (SAUCE_USERNAME, SAUCE_ACCESS_KEY)
)
driver.get('http://saucelabs.com/test/guinea-pig')
id = self.driver.session_id
print 'Link to your job: https://saucelabs.com/jobs/%s' % id
driver.quit()

Running a Test From Local WebDriver

The following Python script executes a simple test against a remote web server. It drives the local FireFox browser:

#!/usr/bin/env python

import unittest
from selenium import webdriver


class Selenium2OnLocal(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_from_local(self):
        self.driver.get('http://saucelabs.com/test/guinea-pig')
        self.assertEqual('I am a page title - Sauce Labs', self.driver.title)
        body = self.driver.find_element_by_css_selector('body')
        self.assertIn('This page is a Selenium sandbox', body.text)

    def tearDown(self):
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()

Running a Test From Sauce Labs

Similar Python script as above, but now executing from Sauce Labs cloud. Notice the use of webdriver.Remote() as a replacement driver:

#!/usr/bin/env python

import unittest
from selenium import webdriver


SAUCE_USERNAME = 'your-username-string'
SAUCE_ACCESS_KEY = 'your-access-key-string'


class Selenium2OnSauce(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Remote(
            desired_capabilities=webdriver.DesiredCapabilities.FIREFOX,
            command_executor='http://%s:%s@ondemand.saucelabs.com:80/wd/hub' %
            (SAUCE_USERNAME, SAUCE_ACCESS_KEY)
        )

    def test_from_sauce(self):
        self.driver.get('http://saucelabs.com/test/guinea-pig')
        self.assertEqual('I am a page title - Sauce Labs', self.driver.title)
        body = self.driver.find_element_by_css_selector('body')
        self.assertIn('This page is a Selenium sandbox', body.text)

    def tearDown(self):
        id = self.driver.session_id
        print 'Link to your job: https://saucelabs.com/jobs/%s' % id
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()