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

Prerequisites

...

Code Example

Include Page_python_code_example_python_code_exampleBefore getting started, you should read the Python Test Setup with Sauce Labs Tutorial.

  • 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 use pip to install it.

    Code Block
    pip install selenium
  • You should also install the Sauce Python client, which provides features for reporting job information to the Sauce Labs dashboard. 

    Code Block
    pip install sauceclient

Code Example

This simple Python test script tests the Google front page. Despite its simplicity, it contains everything you need to know in order to run an automated test on Sauce Labs.

Code Block
languagepy
titlePython on Sauce Code Example
linenumberstrue
collapsetrue
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# This is the only code you need to edit in your existing scripts. 
# The command_executor tells the test to run on Sauce, while the desired_capabilties 
# parameter tells us which browsers and OS to spin up.
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)

# This is your test logic. You can add multiple tests here.
driver.implicitly_wait(10)
driver.get("http://www.google.com")
if not "Google" in driver.title:
    raise Exception("Unable to load google page!")
elem = driver.find_element_by_name("q")
elem.send_keys("Sauce Labs")
elem.submit()
print driver.title

# This is where you tell Sauce Labs to stop running tests on your behalf. 
# It's important so that you aren't billed after your test finishes.
driver.quit()
Tip
titleWait!

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.

...

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. Include Page_python_quick_start_python_quick_start

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

Code Block
languagepy
titleRunning a Local Python Test
driver = webdriver.Firefox() 

If you wanted to run on Sauce, you would instead use webdriver.Remote(), and then pass it two paramaters: 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.

Code Block
languagepy
titleRunning a Python Test Remotely on Sauce
# 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://YOUR_SAUCE_USERNAME:YOUR_SAUCE_ACCESSKEY@ondemand.saucelabs.com:80/wd/hub',
    desired_capabilities=desired_cap)

You can use the Platform Configurator to specify the desired capabilities for any browser/platform combination you want. 

Running the Test

  1. 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.
  2. Open terminal and navigate to the directory where the file is located.
  3. Execute the test:

    Code Block
    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

Include Page_running_tests_in_parallel_running_tests_in_parallelDeveloping apps on localhost is quick and efficient. The drawback is that localhost is not a publicly-accessible address on the Internet, so the browsers in the Sauce Labs cloud can't load and test your app. The solution is to use Sauce ConnectSauce Connect uses a secure tunnel protocol that gives specific Sauce machines access to your local network. You can also use it to test applications that are inside your corporate firewall. Sauce Connect is not required to run tests on Sauce Labs, only in situations where the application or website you want to test is on your local machine or behind a firewall. 

Running Tests in Parallel

Now that you’re running tests on Sauce, you may wonder how you can run your tests faster. One way to increase speed is by running tests in parallel across multiple virtual machines.

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. For more on this see the Selenium WebDriver - Running Your Tests in Parallel blog.

Before you start running tests in parallel, you should review the Python Test Setup with Sauce Labs Tutorial, especially the topics on avoiding external test dependencies and avoiding dependencies between tests.

Tip
titleParallel Tests and Concurrency Limits

The number of tests you can run in parallel is determined by the concurrency limit associated with your account. You can check this in you Sauce Labs dashboard under Concurrent VMs

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

...

"Wait," you might be asking, "My test says 'Complete' 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. You can, however, tell Sauce about the results of our tests automatically using the Sauce python client and adding these lines to your test. 

Code Block
languagepy
linenumberstrue
# 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) 

You should also follow our recommended best practice of adding build numbers, tags, and other identifying information to your tests so you can easily find and manage them in your test results and archives pages