Skip to end of metadata
Go to start of metadata

This script illustrates the basic set up of a test that runs in the Sauce Labs virtual browser cloud. It includes using environment variables for authentication credentials, using tags and build numbers for test result management, and reporting the Pass/Fail status of the test to the Sauce Labs dashboard.

Example Only

The code in this topic is presented as an example only, since your tests and testing environments may require specialized scripting. This information should be taken only as an illustration of how you could set up your tests with Sauce Labs, and is not directly supported by Sauce.

Prerequisites

Setup Example Script

When this script runs, it opens the browser specified in the script, navigates to the saucelabs.demo web app, and then closes the browser. If you want to run this script using your Sauce Labs credentials and have it navigate to your own web app:

  1. Follow the instructions in Best Practice: Use Environment Variables for Authentication Credentials to set up your Sauce Labs account access.
  2. Enter the URL for the web app you want to test in the place of saucedemo.com.
  3. Once you've been able to run the test against your web app, you can use the Platform Configurator to experiment with changing the desired capabilities of the test. 

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

There are examples using both pytest and unittest frameworks, change directories to the relevant module before running your tests:

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

     Click here to view the full script

    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

  • https://github.com/saucelabs-training/demo-python/blob/master/on-boarding-modules/unittest-examples/test_module4_unittest.py

     Click here to view the full script

    test_module4_unittest.py
    # Selenium 3.14+ doesn't enable certificate checking
    import unittest
    import os
    from selenium import webdriver
    
    sauce_username = os.environ["SAUCE_USERNAME"]
    sauce_access_key = os.environ["SAUCE_ACCESS_KEY"]
    remote_url = "https://ondemand.saucelabs.com:443/wd/hub"
    
    class Module4Test(unittest.TestCase):
    
        def setUp(self):
            sauceOptions = {
                'screenResolution': '1280x768',
                'seleniumVersion': '3.141.59',
                'build': 'Onboarding Sample App - Python + UnitTest',
                '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
            }
            # In ChromeOpts, we define browser and/or WebDriver capabilities such as
            # the browser name, browser version, platform name, platform version
            chromeOpts = {
                'platformName': 'Windows 10',
                'browserName': 'chrome',
                'browserVersion': 'latest',
                'goog:chromeOptions': {'w3c': True},
                'sauce:options': sauceOptions
            }
            self.driver = webdriver.Remote(command_executor=remote_url, desired_capabilities=chromeOpts)
    
        def test_should_open_chrome(self):
            # Substitute 'http://www.saucedemo.com' for your own application URL
            self.driver.get("https://www.saucedemo.com")
            assert ("Swag Labs" in self.driver.title)
    
        def tearDown(self):
            if self.driver.title == 'Swag Labs':
                self.driver.execute_script('sauce:job-result=passed')
            else:
                self.driver.execute_script('sauce:job-result=failed')
            self.driver.quit()
    
    
    if __name__ == '__main__':
        unittest.main()

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:

'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:

    $ pip install -r requirements.txt
  2. Set your Sauce Labs Credentials as envrionment variables, indicated by the following lines in the script:

        sauce_username = os.environ["SAUCE_USERNAME"]
        sauce_access_key = os.environ["SAUCE_ACCESS_KEY"]
  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:

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

    unittest:

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