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.

Basic Setup

Dependencies

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 = "http://sauceUsername:sauceAccessKey@ondemand.saucelabs.com:80/wd/hub"

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('http://google.com') 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")

sauce_client.jobs.update_job(driver.session_id, 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('example.py', 'wb').write(urllib2.urlopen('http://saucelabs.com/examples/example.py') .read() .replace('_U_', '\"sauceUsername\"') .replace('_K_', '\"sauceAccessKey\"'))"

Writing tests

Let's look at example.py 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 = "http://%s:%s@ondemand.saucelabs.com:80/wd/hub" 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 example.py

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
_setup_example_intro
_setup_example_intro

Include Page
_example_only
_example_only

Table of Contents

Prerequisites

Include Page
_python_requirements
_python_requirements

Include Page
_setup_example_script
_setup_example_script

You can clone this script from the saucelabs-training repository on GitHub: https://github.com/saucelabs-training/demo-python

Info

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
    pathon-boarding-modules/pytest-examples/test_module4_pytest.py
    lastline9
    repository-id36
    firstline8
    branchrefs/remotes/origin/master
  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:

    pytest:

    Code Block
    pytest on-boarding-modules/pytest-examples/test_module4_pytest.py

    unittest:

    Code Block
     python -m unittest on-boarding-modules/unittest-examples/test_module4_unittest.py