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.

...

  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
python first_test.py

Check your dashboard and you will see that your test has just run on Sauce!

Tip
titleWait!

...

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. Using implicit waits is one of our recommended best practices.

...

Analyzing the Code

If you 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:

driver = webdriver.Firefox()

However, to run on Sauce we use the general webdriver.Remote() instead of the specific webdriver.Firefox(), and then pass it two paramaters: command_executor and desired_capabilties.

(Note: 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 the Sauce Labs servers.)

# 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

Because we are not specifying webdriver.Firefox() as we were before, we must use desired capabilities to specify what browser/OS combination(s) to spin up and execute against.

If you want to run against different platforms, simply update your desired capabiltlies to something new, like so:

desired_cap = {'browserName': "chrome", 'platform': "Windows 8.1", 'version': "42.0"}

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.code closely, you'll see that basics for setting up a test to run on sauce are very straightforward, and really only require elements.

Include Page
_python_quick_start
_python_quick_start

 Running Tests Against Local Applications

...