Skip to end of metadata
Go to start of metadata

CircleCI

This repository contains example scripts and dependencies for running automated Selenium tests on Sauce Labs using Python.You can use these scripts to test your Sauce Labs authentication credentials, setup of your automated testing environment, and try out Sauce Labs features.

For Demonstration Purposes Only

The code in these scripts is provided on an "AS-IS" basis without warranty of any kind, either express or implied, including without limitation any implied warranties of condition, uninterrupted use, merchantability, fitness for a particular purpose, or non-infringement. These scripts are provided for educational and demonstration purposes only, and should not be used in production. Issues regarding these scripts should be submitted through GitHub. These scripts are maintained by the Technical Services team at Sauce Labs.


Prerequisites

 Click here to see detailed instructions

Before attempting an automated test, ensure you've installed the following software:


Install Git

Git is a version control system that lets you check out code from a repository, work with that code on your own branch, and then merge that code with any changes that have been made by other developers. Git is an essential tool for distributed development teams, and is a critical component of the continuous integration/continuous development toolchain.

MacOSX:
  1. Go to https://git-scm.com/downloads.
  2. Under Downloads, click Mac OS X.
  3. When the download completes, double-click the .dmg file open the installer package.
  4. Double-click the installer package to begin the installation.

    Security Warning

    You may see a warning message that the package can't be opened because it's not from a recognized developer. If this happens, go to System Preferences > Security and Privacy Settings, and click Open Anyway.

  5. Click Continue for the installation, and enter your local password to authorize the installation.
Windows:
  1. Go to https://git-scm.com/downloads
  2. Under Downloads, click on Windows.
  3. When the dialog opens asking if you want to allow the app to make changes to your device, click Yes.
  4. Follow the steps in the setup wizard to complete the installation. You should accept all the default settings.

Install Python and PIP

MacOSX:
  1. Using brew install python:

    $ brew install python3
    

    Python 2.7 is included by default on recent versions of Mac OSs. If it somehow not included, install it by running brew install python

  2. (Optional) Install pip to manage packages. Modern versions of Python come with pip included. To verify if you have pip installed, run

    pip -V
    

    in your command prompt. You should see something like this:

    pip 18.1 from /path/of/python/installation
    

    If you get an error or "pip not found" message, you can install pip separately using the following commands:

    $ curl -O http://python-distribute.org/distribute_setup.py
    $ python distribute_setup.py
    $ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
    $ python get-pip.py
    
  3. Install virtualenv in order to manage projects that have different or conflicting dependencies--it also prevents you from accidentally installing global python dependencies.

    $ pip install virtualenv
    
Windows:
  1. Go to https://www.python.org/downloads/.
  2. Click the "Download Python <version>" button.
  3. Download the package and extract it on you system.
  4. Open the executable and follow the prompts to complete installation.

Install a Test Framework

A testing framework is a set of guidelines/rules for desiging test cases. Frameworks themselves consist of test and reporting classes that allow QA engineers to test efficiently. This repo only supports pytest and unittest.

For more information regarding other libraries, checkout our GitHub repo that catalogs all sample test frameworks.


Install Pytest
  1. Open a terminal (OSX) or command prompt (Windows)
  2. Navigate to the pytest directory:
    cd on-boarding-modules/pytest-examples/
    
  3. Resolve the package dependencies:
    sudo pip install -r requirements.txt 
    
  4. Run tests:
    pytest
    

Install UnitTest

unittest is an included test module in the Python standard library. Its API will be familiar to anyone who has used any of the JUnit/nUnit/CppUnit series of tools.

  1. Open a terminal (OSX) or command prompt (Windows)
  2. Navigate to the unittest-examples directory:
    cd on-boarding-modules/unittest-examples
    
  3. Resolve the package dependencies:
    sudo pip install -r requirements.txt 
    
  4. Run tests:
    python -m unittest
    

Install an IDE

It's recommended to install and Integrated Developer Environment, or a text editor, to help manage package dependencies, interperters, and overall code execution. There are several options available, some of them are free and some require payment:

  • PyCharm community edition is free, Professional version requires subscription.
  • Visual Studio Code free text editor from Microsoft with a wide variety of extensions.
  • Komodo Edit, free, text editor, stripped down version of Komodo IDE (paid version).

Setup a python interpreter:


Sauce Labs Test Setup

  1. Navigate to the root project directory and use pip to install the latest Selenium library for use in the script:

    $ pip install -r requirements.txt
    
  2. Run the Test Script

    In order to run the test on www.saucelabs.com, change the values of the SAUCE_USERNAME and SAUCE_ACCESS_KEY variables in the test script to your Sauce Username and Sauce Access Key values. To retrieve this information, login to your saucelabs.com account and navigate to User Settings;

  3. Visit the saucelabs.com automated build page and select the build Onboarding Sample App - Python to see the following test case:

    1-first-test


Choosing the Correct Data Center

IP Ranges and Data Center Endpoints
Sauce Labs has data centers in both the US and EU. You can access Sauce Labs services from either location by providing the appropriate URL endpoints, and whitelisting their associated IP ranges. 

 Click here to view US Data Center Endpoints

Virtual Device Cloudhttps://ondemand.saucelabs.com/wd/hub

Real Devices:  https://ondemand.us-west-1.saucelabs.com

Sauce Connect Proxyhttps://saucelabs.com/rest/v1 (do not need to specify, Sauce Connect Proxy will use this by default)

SSO:

REST API 

IP Ranges:

162.222.72.0/21 (this is equivalent to the range 162.222.72.1 – 162.222.79.254)
66.85.48.0/21 (this is equivalent to the range 66.85.48.0 - 66.85.55.255)

 Click here to view EU Data Center endpoints

For more information, including framework configuration for the European data center, check out Sauce Labs European Data Center Configuration Information.

Virtual Device Cloudhttps://ondemand.eu-central-1.saucelabs.com/wd/hub

Real Devices:  https://ondemand.eu-central-1.saucelabs.com

Sauce Connect Proxyhttps://eu-central-1.saucelabs.com/rest/v1 (specify with -x argument)

SSO:

REST API:

IP Range:

185.94.24.0/22 (this is equivalent to the range 185.94.24.0 - 185.94.27.255



Instant Test with Environment Variables

This example script follows the best practice of using environment variables in place of hardcoded authentication credentials. Once you've been able to successfully run the instant test with hardcoded credentials, you can use this test to make sure that you've correctly set up your environment variables for authentication.

 Click here to view the example script

Clone from: https://github.com/saucelabs-training/demo-python/blob/master/on-boarding-modules/pytest-examples/test_module2_pytest.py

test_module2_pytest.py
import pytest
from selenium import webdriver
import os
from _pytest.runner import runtestprotocol


@pytest.fixture
def driver(request):
    sauce_username = os.environ["SAUCE_USERNAME"]
    sauce_access_key = os.environ["SAUCE_ACCESS_KEY"]
    remote_url = "https://ondemand.saucelabs.com:443/wd/hub"

    desired_cap = {
        'platform': 'Mac OS X 10.13',
        'browserName': 'safari',
        'version': '11.1',
        'build': 'Onboarding Sample App - Python + Pytest',
        'name': '2-user-site',
        'username': sauce_username,
        'accessKey': sauce_access_key
    }

    browser = webdriver.Remote(command_executor=remote_url, desired_capabilities=desired_cap)
    yield browser
    browser.quit()


# Here is our actual test code. In this test we open the saucedemo app in chrome and assert that the title is correct.
def test_should_open_chrome(driver):
    # Substitute 'http://www.saucedemo.com' for your own application URL
    driver.get("https://www.saucedemo.com")
    actual_title = driver.title
    expected_title = "Swag Labs"
    assert expected_title == actual_title

Instant Test with Best Practices and Sauce Labs Reporting

This example script illustrates the use of several best practices in test design, and will report Pass/Fail status to the Sauce Labs dashboard.

 Click here to view the example script

Clone from: https://github.com/saucelabs-training/demo-python/blob/master/on-boarding-modules/pytest-examples/test_module4_pytest.py

test_module4_pytest.py
import pytest
import os
from selenium import webdriver
from _pytest.runner import runtestprotocol

@pytest.fixture
def driver(request):
    sauce_username = os.environ["SAUCE_USERNAME"]
    sauce_access_key = os.environ["SAUCE_ACCESS_KEY"]
    remote_url = "https://ondemand.saucelabs.com:443/wd/hub"

    sauceOptions = {
        'screenResolution': '1280x768',
        'seleniumVersion': '3.141.59',
        # best practices involve setting a build number for version control
        'build': 'Onboarding Sample App - Python + Pytest',
        'name': '4-best-practices',
        'username': sauce_username,
        'accessKey': sauce_access_key,
        # tags to filter test reporting.
        'tags': ['instant-sauce', 'ruby-rspec', 'module4'],
        # setting sauce-runner specific parameters such as timeouts helps
        # manage test execution speed.
        'maxDuration': 1800,
        'commandTimeout': 300,
        'idleTimeout': 1000,
    }

    chromeOpts = {
        'platformName': 'Windows 10',
        'browserName': 'chrome',
        'browserVersion': 'latest',
        'goog:chromeOptions': {'w3c': True},
        'sauce:options': sauceOptions
    }

    browser = webdriver.Remote(command_executor=remote_url, desired_capabilities=chromeOpts)
    yield browser
    browser.quit()


def test_should_open_chrome(driver):
    driver.get("https://www.saucedemo.com")
    actual_title = driver.title
    expected_title = "Swag Labs"
    assert expected_title == actual_title

Running Local Tests

Developing websites/apps within your local network is secure and efficient. The drawback to this method is that local assets are not publicly-accessible on the Internet, so the browsers/devices in the Sauce Labs cloud can't load and test your app. The solution is to use Sauce ConnectSauce Connect is a proxy server that creates a secure tunnel connection between the Sauce Labs virtual machine that runs your test and your local  network. You can also use Sauce Connect to test applications and websites that are located within 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 not publicly accessible. 

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, 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.