With Sauce Labs you can run automated tests of your web apps against a wide variety of device/operating system/browser combinations. This tutorial will show you how to get started with testing with Python on Sauce.
Prerequisites
- You should have a Sauce Labs account
- You should have a Selenium environment with the bindings for Python set up on the local machine where you'll launch your tests. If you need help with this, check out the ReadMe in the Python example scripts repository on GitHub.
- You should read the Best Practices for Running Tests
You will need to install the Selenium WebDriver client driver to your local Python environment
You can either download the driver from the link, or usepip
to install it.You can also optionally install the Sauce Python client, which provides features for reporting job information to the Sauce Labs dashboard.
Code Example
Wait!
The explicit wait method tells the browser to wait a set amount of time (in seconds) for elements to appear on the page before giving up. Using explicit waits is one of our recommended best practices.
Analyzing the Code
If you look at the code closely, you'll see that basics for setting up a test to run on sauce are very straightforward, and really only require two elements.
Set Sauce URL and Access Credentials
If you wanted to run a Selenium test on Selenium locally, you would initiate a driver for the browser you want to test against like this.
driver = webdriver.Firefox()
To run a test on a remote service like Sauce Labs, all you need to change is your driver
definition, make sure that the sauce_endpoint
variable includes your Sauce USERNAME
and ACCESSKEY,
and then pass it two parameters: command_executor
, which points to the Sauce cloud and uses your Sauce Labs authentication to log in, and desired_capabilties
, which specifies the browsers and operating systems to run the tests against.
# 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", } username = os.environ['SAUCE_USERNAME'] access_key = os.environ['SAUCE_ACCESS_KEY'] driver = webdriver.Remote( command_executor='https://{}:{}@ondemand.saucelabs.com/wd/hub'.format(username, access_key), desired_capabilities=desired_cap)
To find your Sauce Labs access key:
- Sign in to https://saucelabs.com with the username you set up when you created your account.
You will use this same username in your test script. - To find your access key:
- Click your name in the Account Profile menu in the upper-right corner.
- Click User Settings.
- Scroll down to Access Key and click Show.
- Enter the password you use to sign in to Sauce Labs to view the access key.
- Click the Copy icon.
Set Desired Capabilities
Once you have your tests set up to run in the Sauce cloud, you need to define the platform, browser, and version you want the test to run against, which is where desired capabilities come into play.
desired_cap = { 'platform': "Mac OS X 10.9", 'browserName': "chrome", 'version': "31",
You can manually enter the values you want for platform
, browsername
, and version
, or you can use the handy Platform Configurator to generate the desired_caps
values for any combination of platform, browser, and browser version.
Running the Test
- Copy the example code and save it into a file called
first_test.py
.
Make sure your username and access key are included in the URL passed through to the command_executor. - Open terminal and navigate to the directory where the file is located.
Execute the test:
python first_test.py
Check your dashboard and you will see that your test has just run on Sauce!
Running Local Tests
Running Tests in Parallel
You can run your tests in parallel at two levels, and you can run your tests in parallel across multiple browsers. For example, if you have 10 tests and want to run on five browsers, this would be parallelism of five. You can also run tests across browsers and each test in parallel. Using our previous example this would be more like 50 parallel tests. Doing this requires that your tests are written in a way that they do not collide with one another, as described in our Best Practice topics avoiding external test dependencies and avoiding dependencies between tests.
Check out Using Frameworks to Run Tests in Parallel for more information, and examples of framework setups for Java, Python, and other programming languages.
See the topics under Running Tests in Parallel with Python for more information and examples of setting up popular Python testing frameworks to run tests in parallel on Sauce
Reporting on Test Results
Unfortunately, Sauce has no way to determine whether your test passed or failed automatically, since it is determined entirely by your business logic. You can, however, tell Sauce about the results of our tests automatically using the Sauce python client and adding these lines to your test. # this authenticates you
from sauceclient import SauceClient
sauce_client = SauceClient("YOUR_SAUCE_USERNAME", "YOUR_SAUCE_ACCESSKEY")
# this belongs in your test logic
sauce_client.jobs.update_job(driver.session_id, passed=True)