The Sauce Labs Cookbook

Sauce Headless

Front End Performance Testing

Analytics

External Resources

More Info


Page tree
Skip to end of metadata
Go to start of metadata

Health Check

CircleCI

Table of Contents

Overview

The following repository: https://github.com/saucelabs-training/demo-java, allows you to quickly run simple automated tests in order to validate your Java test environment with your saucelabs.com account credentials. Below are the steps to configure your test environment and run a simple automated test.

For Demonstration Purposes Only!

Disclaimer:

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.

Solution Outline

The repository has several demonstration scripts that fit various use cases, such as:

If you just wish to get up and running quickly with Sauce Labs, please follow the instructions below.

Prerequisites

In order to run this demo on your local machine, you must download, install, and configure the prerequisite software.

 Click here to view the prerequisite software installation guide

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 IntelliJ

IntelliJ is an integrated development environment that incorporates several tools for developing and running Java code. You will be using IntelliJ to write and edit the sample Selenium scripts used in the exercises. For these exercises you only need to download the free Community edition.

MacOSX:

  1. Go to https://www.jetbrains.com/idea/
  2. Click Download.
  3. On the Downloads page, select macOS.
  4. Under Community, click Download.
  5. When the download completes, double-click the .dmg file open the installer package.
  6. Double-click the installer package to begin the installation.
  7. Drag and drop the IntelliJ icon into the Applications folder.

Windows:

  1. Go to https://www.jetbrains.com/idea/
  2. Click Download.
  3. On the Downloads page, select Windows.
  4. Under Community, click Download.
  5. When the download completes, double-click the .exe file to launch the installation wizard. You should accept all the default settings.

Install the JDK

The Java SE Developer Kit lets you develop and deploy Java applications on desktops and servers. It is needed to compile our test code.

This project requires JDK version 1.8 at the least.

MacOSX:

  1. Go to the JDK downloads page, locate the latest release, select the download button.
  2. Under Java SE Development Kit {version}, select the Accept License Agreement radio button.
  3. Click the download link for Mac OS.
  4. When the download completes, double-click the .dmg file open the installer package. Double-click the installer package to begin the installation.

Windows:

  1. Go to the JDK downloads page, locate the latest release, select the download button.
  2. Under Java SE Development Kit {version}, select the Accept License Agreement radio button.
  3. Click the download link for Windows x64.
  4. When the download completes, double-click the .exe file open the installer package.
  5. Double-click the installer package to begin the installation. You should accept all the default settings.

Install Maven

Maven is a build automation and project management tool use for managing project builds, dependencies, and documentation. It uses a project object model (pom.xml) to manage Java-based projects. With our use case, it's very useful for configuring and managing test suites.

MacOSX:

  1. Go to Maven Apache website and download the following package: apache-maven-<version>-bin.tar.gz

  2. Extract the archive

    $ tar -xvf apache-maven-<version>-bin.zip
    
  3. Add the bin directory of the extracted directory (apache-maven-<version>) to the PATH variable:

    $ export M2=$M2_HOME/bin
    $ export PATH=$M2:$JAVA_HOME/bin:$PATH
    

    ***WARNING!***: Make sure you've set JAVA_HOME othewise mvn commands won't run. For instructions on how to set JAVA_HOME, visit this link

  4. Check to see if maven installed correctly:

    $ mvn -version
    

Windows:

  1. Go to Maven Apache website and download the following package: apache-maven-<version>-bin.zip
  2. Unpack the archive using an archive tool (for example WinZip)
  3. Add the unpacked distribution’s bin directory to your user PATH environment variable by:
    1. Open up the system properties (WinKey + Pause)
    2. Select the Advanced tab, and the Environment Variables button
    3. Add/Select the PATH variable in the user variables with the value:
      C:\Program Files\apache-maven-3.6.0\bin
      
  4. Open a new command prompt (Winkey + R then type cmd) and run mvn -v to verify the installation.

Use a Pre-built Workspace.

If you want to skip the prerequisite setup, you can try this demo in a cloud-based IDE hosted by gitopd.io

  1. After the session launches, navigate to the terminal and use the following commands to export your Sauce Labs credentials:

    eval $(gp env -e SAUCE_USERNAME=******)
    eval $(gp env -e SAUCE_ACCESS_KEY=******)

    Note: If you start a new session or terminal in gitpod you have to re-apply environment variables. For more information consult the gitpod documentation.

    eval $(gp env -e)
  2. Run a Maven test:

    mvn test -Dtest=Module2TestNGTest -pl on-boarding-modules/testng

Sauce Labs Test Setup


Import the Project

  1. Create a directory on your machine.
  2. Clone this repository into said directory.
    $ git clone https://github.com/saucelabs-training/demo-java.git
    
  3. Import the project into your IntelliJ (or IDE of your choice) as a Maven Project.
  4. Click through the prompts, and confirm when it asks to Import from Sources
  5. Choose the demo-java directory as the root directory of the project.

Set Your Sauce Labs Credentials

  1. Copy your Sauce Labs username and accessKey in the User Settings section of the Sauce Labs Dashboard.

  2. Open a Terminal window (command prompt for Windows) and set your Sauce Labs Environment variables:

    Mac OSX:
    $ export SAUCE_USERNAME="username"
    $ export SAUCE_ACCESS_KEY="accessKey"
    
    Windows:
    > set SAUCE_USERNAME="username"
    > set SAUCE_ACCESS_KEY="accessKey"
    

    To set an environment variables permanently in Windows, you must append it to the PATH variable.

    Go to Control Panel > System > Windows version > Advanced System Settings > Environment Variables > System Variables > Edit > New

    Then set the "Name" and "Value" for each variable

  3. Test the environment variables

    Mac OSX:
    $ echo $SAUCE_USERNAME
    $ echo $SAUCE_ACCESS_KEY
    

    ***WARNING FOR UNIX USERS!***: If you have problems setting your environment variables, run the following commands in your terminal:

    $ launchctl setenv SAUCE_USERNAME $SAUCE_USERNAME
    $ launchctl setenv SAUCE_ACCESS_KEY $SAUCE_ACCESS_KEY
    
    Windows:
    > echo %SAUCE_USERNAME%
    > echo %SAUCE_ACCESS_KEY%
    

Run a Maven Test

  1. Run the following command to update any package dependencies:

    $ mvn dependency:resolve
    
  2. Then run the following command to compile your test code:

    $ mvn test-compile
    
  3. Finally, run the following test to see if you've properly configured the test environment:

    $ mvn test -Dtest=Module2TestNGTest -pl on-boarding-modules/testng
    
    

    If you wish to run a specific test/sub-module in this emusim_testng use the following command:

    # for running a specific test:
    mvn test -Dtest=testname
    
    # for running a specific sub-module
    mvn test -pl subproject/
    

Health Check

CircleCI

Table of Contents

Overview

The following repository: https://github.com/saucelabs-training/demo-python, allows you to quickly run simple automated tests in order to validate your Python test environment with your saucelabs.com account credentials. Below are the steps to configure your test environment and run a simple automated test.

For Demonstration Purposes Only!

Disclaimer

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

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

 Click here to view the prerequisite software installation guide


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:


Use a Pre-built Workspace

If you want to skip the prerequisite setup, you can try this demo in a cloud-based IDE hosted by gitopd.io

  1. After the session launches, navigate to the terminal and use the following commands to export your Sauce Labs credentials:

    eval $(gp env -e SAUCE_USERNAME=******)
    eval $(gp env -e SAUCE_ACCESS_KEY=******)


    Note: If you start a new session or terminal in gitpod you have to re-apply environment variables. For more information consult the 
    gitpod documentation.

    eval $(gp env -e)
  2. Install dependencies and run a python test:

    pip install -r requirements.txt && /
    python on-boarding-modules/python-examples/test_module2.py

Sauce Labs Test Setup

Run a Sample 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. 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



Health Check

CircleCI

Table of Contents

Overview

The following repository: https://github.com/saucelabs-training/demo-ruby, allows you to quickly run simple automated tests in order to validate your Ruby test environment with your saucelabs.com account credentials. Below are the steps to configure your test environment and run a simple automated test.

For Demonstration Purposes Only!

Disclaimer:

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

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

 Click here to view the prerequisite software installation guide

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 Ruby

For Unix/Linux systems we use rbenv. rbenv is a tool that lets you install, run, and manage multiple versions of ruby environments on your system.

MacOSX:
  1. Using brew install rbenv:

    $ brew install rbenv
    
  2. Setup rbenv in your current bash shell:

    $ rbenv init
    
  3. Close and restart your terminal, or run the following command so changes take effect:

    $ source ~/.bashrc
    
  4. Verify that rbenv is configured correctly, and running on your system:

    $ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
    Checking for `rbenv' in PATH: /usr/local/bin/rbenv
    Checking for rbenv shims in PATH: OK
    Checking `rbenv install' support: /usr/local/bin/rbenv-install (ruby-build 20170523)
    Counting installed Ruby versions: none
      There aren't any Ruby versions installed under `~/.rbenv/versions'.
      You can install Ruby versions like so: rbenv install 2.2.4
    Checking RubyGems settings: OK
    Auditing installed plugins: OK
    
  5. Install a version of Ruby using rbenv, here are list of the available versions:

    $ rbenv install <ruby_version>
    
  6. The ruby versions are generally stored in the following directory:

    ~/.rbenv/versions
    
Windows:
  1. Go to https://rubyinstaler.org/downloads/.

  2. Under RubyInstallers, choose the latest version of Ruby+DevKit.

  3. Download the package and extract it on you system.

  4. Open the executable and follow the prompts to complete installation.

  5. To upgrade Ruby, use ridk install command.

    This installer configures Ruby as a native Windows application, to learn more about RubyInstaller refer to the this link

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:

  • Aptana Studio free, IDE developed by GitHub. There are versions for MacOSX, Windows, and Linux.
  • RubyMine free trial, paid after 30 days, developed by JetBrains.
  • Komodo Edit, free, text editor, stripped down version of Komodo IDE (paid version).
  • NetBeans free, open source IDE developed by Apache. Contains source binaries that you must download and compile.

Use a Pre-built Workspace

If you want to skip the prerequisite setup, you can try this demo in a cloud-based IDE hosted by gitopd.io

  1. After the session launches, navigate to the terminal and use the following commands to export your Sauce Labs credentials:

    eval $(gp env -e SAUCE_USERNAME=******)
    eval $(gp env -e SAUCE_ACCESS_KEY=******)


    Note: If you start a new session or terminal in gitpod you have to re-apply environment variables. For more information consult the 
    gitpod documentation.

    eval $(gp env -e)
  2. Install dependencies and run a ruby test:

    gem install bundler && /
    bundle install --path .bundle && /
    bundle exec rspec

Sauce Labs Test Setup

Setup the Project

  1. Setup Gemfile:

    • Install bundler so that we can easily update and resolve dependencies:

      $ gem install bundler
      
  2. Execute the bundle Command:

    • Use the following to install the required gems:

      $ bundle install --path .bundle
      
  3. Run the Test Scripts

Health Check

CircleCI

Table of Contents

Overview

The following repository: https://github.com/saucelabs-training/demo-js, allows you to quickly run simple automated tests in order to validate your NodeJS test environment with your saucelabs.com account credentials. Below are the steps to configure your test environment and run a simple automated test.

For Demonstration Purposes Only!

Disclaimer

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

In order to run this demo on your local machine, you must download, install, and configure the prerequisite software.

 Click here to view the prerequisite software installation guide


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 NPM and NodeJS

NPM is the primary package manager that will help to resolve dependency issues.

  1. Go to https://www.npmjs.com/get-npm](https://www.npmjs.com/get-npm).
  2. Click the button that reads "Download Node.js and NPM.
  3. The site will detect which operating system you're currently running and present the requesite packages.
  4. Choose LTS, and open up the package to run the installer.
  5. Follow the prompts to complete the installation.
  6. To ensure NPM and Node.js installed correctly, open a terminal/shell and run the following command:
    node -v
    npm -v
    

NodeJS lets you develop and deploy JavaScript applications on desktops and servers. If you which to download and install the package manually refer to the following instructions:

MacOSX:

  1. Go to https://nodejs.org/en/download/.
  2. Under LTS, click Mac OS Installer X.
  3. When the download completes, double-click the .pkg file to open the installer
  4. Follow the prompts to complete the installation.

Windows:

  1. Go to https://nodejs.org/en/download/.
  2. Under LTS, click Windows Installer.
  3. When the download completes, double-click the .msi file to open the installer
  4. Follow the prompts to complete the 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 mocha, jasmine, and protractor. For more information regarding other libraries checkout our GitHub repo that catalogs all sample test frameworks.

Install Mocha
  1. Open a terminal (OSX) or command prompt (Windows)
  2. Navigate to the mocha-examples directory:
    cd ~/on-boarding-modules/mocha-examples
    
  3. Resolve the package dependencies:
    npm install
    
  4. Run tests:
    npm test
    
Install Jasmine
  1. Open a terminal (OSX) or command prompt (Windows)
  2. Navigate to the jasmine-examples directory:
    cd ~/on-boarding-modules/jasmine-examples
    
  3. Resolve the package dependencies:
    npm install
    
  4. Run tests:
    npm test
    

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:

  • Atom IDE free, IDE developed by GitHub.
  • WebStorm free trial, paid after 30 days, developed by JetBrains.
  • Komodo Edit, free, text editor, stripped down version of Komodo IDE (paid version).
  • Brackets free, text editor, contains a wealth of community plugins and extensions.

Use a Pre-built Workspace

If you want to skip the prerequisite setup, you can try this demo in a cloud-based IDE hosted by gitopd.io

  1. After the session launches, navigate to the terminal and use the following commands to export your Sauce Labs credentials:

    $ eval $(gp env -e SAUCE_USERNAME=******)
    $ eval $(gp env -e SAUCE_ACCESS_KEY=******)


    Note: If you start a new session or terminal in gitpod you have to re-apply environment variables. For more information consult the 
    gitpod documentation.

    $ eval $(gp env -e)


  2. Navigate to the desired test directory, for example:

    $ cd mocha-chai-tests
  3. Resolve the package dependencies and then run the test:

    $ npm install && npm test

Sauce Labs Test Setup

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.

Run A Sample Test

  1. Clone the Repository and set your Sauce Labs Credentials:

    git clone https://github.com/saucelabs-training/demo-js.git
    export SAUCE_USERNAME=********
    export SAUCE_ACCESS_KEY=*******
    
  2. Navigate to the desired directory (e.g.):

    cd mocha-chai-tests
    
  3. Resolve Dependencies (via package.json)

    npm install
    
  4. Run the Test

    npm test
    

    You should see the following output in the console:

    > demo-js@0.0.1 test /path/to/demo-js
    > node on-boarding-modules/node-examples/module2-test.js
    
    Test Passed!
    
    
  5. Visit the saucelabs.com automated build page and select the build Onboarding Sample App - NodeJS to see the following test case:

    2-user-site


If you want to get up and running with Sauce Labs but don't have any Selenium test scripts, you can use the ones in this topic to see how how it works. All you need to do is add your Sauce Labs username and password, and then run the script from the local machine where you have your Selenium environment set up. When the script runs, it will connect to Sauce Labs using your authentication credentials, launch the browser set in the test script, load the Sauce Labs demo website, and then close the browser and quit the test.

After running the test, you can log into the Sauce Labs web interface to see the test listed on your dashboard, and view information about the test, including video, by clicking on the test name to view the Test Details page.

If you want to experiment with the sample script further, try using the Platform Configurator to change the Desired Capabilities of the test, or substitute the URL of the web app you want to test for the sauce.demo URL. 

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

Instant Test

Download this script from the GitHub repo to your local machine, provide the information for SAUCE_USERNAME and SAUCE_ACCESS_CODE as indicated in the script, and then save and run the script. If you log in to saucelabs.com before you run the script, you'll be able to watch the script's progress as it executes.

 Click here to view the example script

Clone from: https://github.com/saucelabs-training/demo-csharp/blob/master/SauceExamples/Web.Tests/OnboardingTests/InstantSauceTest.cs

using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using System;
using ExpectedConditions = SeleniumExtras.WaitHelpers.ExpectedConditions;

namespace Web.Tests.OnboardingTests
{
    [TestFixture]
    [Category("InstantSauceTest"), Category("NUnit"), Category("Instant")]
    public class InstantSauceTest
    {
        private IWebDriver _driver;
        [Test]
        public void ShouldOpenOnSafari()
        {
            /*Easy Option For Sauce Authentication:
            *You can hardcode the values like this example below, but the best practice is to use environment variables
            */
            var sauceUserName = "YOUR USER NAME";
            var sauceAccessKey = "YOUR ACCESS KEY";

            /*
             * In this section, we will configure our test to run on some specific
             * browser/os combination in Sauce Labs
             */
            var capabilities = new DesiredCapabilities();
            //set your user name and access key to run tests in Sauce
            capabilities.SetCapability("username", sauceUserName);
            //set your sauce labs access key
            capabilities.SetCapability("accessKey", sauceAccessKey);
            //set browser to Safari
            capabilities.SetCapability("browserName", "Safari");
            //set operating system to macOS version 10.13
            capabilities.SetCapability("platform", "macOS 10.13");
            //set the browser version to 11.1
            capabilities.SetCapability("version", "11.1");
            //set your test case name so that it shows up in Sauce Labs
            capabilities.SetCapability("name", TestContext.CurrentContext.Test.Name);

            //create a new Remote driver that will allow your test to send
            //commands to the Sauce Labs grid so that Sauce can execute your tests
            _driver = new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com:80/wd/hub"),
                capabilities, TimeSpan.FromSeconds(600));
            //navigate to the url of the Sauce Labs Sample app
            _driver.Navigate().GoToUrl("https://www.saucedemo.com");

            //Create an instance of a Selenium explicit wait so that we can dynamically wait for an element
            var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(5));
            //wait for the user name field to be visible and store that element into a variable
            var userNameField = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("[type='text']")));
            //type the user name string into the user name field
            userNameField.SendKeys("standard_user");
            //type the password into the password field
            _driver.FindElement(By.CssSelector("[type='password']")).SendKeys("secret_sauce");
            //hit Login button
            _driver.FindElement(By.CssSelector("[type='submit']")).Click();

            //Synchronize on the next page and make sure it loads
            var inventoryPageLocator =
                wait.Until(ExpectedConditions.ElementIsVisible(By.Id("inventory_container")));
            //Assert that the inventory page displayed appropriately
            Assert.IsTrue(inventoryPageLocator.Displayed);
        }

        /*
         *Below we are performing 2 critical actions. Quitting the driver and passing
         * the test result to Sauce Labs user interface.
         */
        [TearDown]
        public void CleanUpAfterEveryTestMethod()
        {
            var passed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed;
            ((IJavaScriptExecutor)_driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
            _driver?.Quit();
        }
    }
}

To find your Sauce Labs access key:

  1. Sign in to https://saucelabs.com with the username you set up when you created your account. 
    You will use this same username in your test script. 
  2. To find your access key:
    1. Click your name in the Account Profile menu in the upper-right corner.
    2. Click User Settings.
    3. Scroll down to Access Key and click Show.
    4. Enter the password you use to sign in to Sauce Labs to view the access key.
    5. Click the Copy icon.

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-csharp/blob/master/SauceExamples/Web.Tests/OnboardingTests/InstantSauceTest2.cs

using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System;
using System.Threading;

namespace Web.Tests.OnboardingTests
{
    [TestFixture]
    [Category("InstantSauceTest"), Category("NUnit"), Category("Instant")]
    public class InstantSauceTest2
    {
        private IWebDriver _driver;
        [Test]
        public void ShouldOpenOnSafari()
        {
            /*
             * Best Practice
             * Instead of using hardcoded username and access key, you should store
             * the credentials in environment variables on your system. Not sure how to do this?
             * This document will help:
             * https://wiki.saucelabs.com/display/DOCS/Best+Practice%3A+Use+Environment+Variables+for+Authentication+Credentials
             */
            var sauceUserName =
                Environment.GetEnvironmentVariable("SAUCE_USERNAME", EnvironmentVariableTarget.User);
            var sauceAccessKey =
                Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY", EnvironmentVariableTarget.User);

            /*
             * In this section, we will configure our test to run on some specific
             * browser/os combination in Sauce Labs
             */
            var capabilities = new DesiredCapabilities();
            capabilities.SetCapability("username", sauceUserName);
            capabilities.SetCapability("accessKey", sauceAccessKey);
            capabilities.SetCapability("browserName", "Safari");
            capabilities.SetCapability("platform", "macOS 10.13");
            capabilities.SetCapability("version", "11.1");
            capabilities.SetCapability("name", TestContext.CurrentContext.Test.Name);
            _driver = new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com:80/wd/hub"),
                capabilities, TimeSpan.FromSeconds(600));

            //TODO Please supply your publicly available web url in place of saucedemo.com
            //In the future, you will learn how to access private URLs
            _driver.Navigate().GoToUrl("https://www.saucedemo.com");

            //Hardcoded sleep just for demonstration purposes
            Thread.Sleep(10000);
            //This test will always pass for demonstration purposes
            Assert.IsTrue(true);
        }

        /*
         *Below we are performing 2 critical actions. Quitting the driver and passing
         * the test result to Sauce Labs user interface.
         */
        [TearDown]
        public void CleanUpAfterEveryTestMethod()
        {
            var passed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed;
            ((IJavaScriptExecutor)_driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
            _driver?.Quit();
        }
    }
}

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-csharp/blob/master/SauceExamples/Web.Tests/OnboardingTests/InstantSauceTest4.cs

using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using System.Threading;

namespace Web.Tests.OnboardingTests
{
    /*
     * These scripts are simply for demonstration purposes.
     * They should not be used as an example of good practices for how to do test automation.
     */
    [TestFixture]
    [Category("InstantSauceTest"), Category("NUnit"), Category("Instant")]
    [Parallelizable]
    public class InstantSauceTest4
    {
        private IWebDriver _driver;
        private readonly string _sauceUserName =
            Environment.GetEnvironmentVariable("SAUCE_USERNAME", EnvironmentVariableTarget.User);
        private readonly string _sauceAccessKey =
            Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY", EnvironmentVariableTarget.User);
        private IJavaScriptExecutor _javascriptExecutor;

        [Test]
        public void BestPractices()
        {
            /*
             * Commenting is one of the most powerful ways to debug your failed tests.
             * Using the sauce:context command below will allow you to place
             * comments inside of Sauce Labs logs that you can read and analyze.
             * Comment your important methods and your automation will drastically improve
             */
            _javascriptExecutor.ExecuteScript("sauce:context=Open SauceDemo.com");
            _driver.Navigate().GoToUrl("https://www.saucedemo.com");

            _javascriptExecutor.ExecuteScript("sauce:context=Sleep for 10000ms");
            Thread.Sleep(10000);
            Assert.IsTrue(true);
        }

        [SetUp]
        public void ExecuteBeforeEveryTest()
        {
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.SetCapability("browserName", "Safari");
            caps.SetCapability("platform", "macOS 10.13");
            caps.SetCapability("version", "11.1");
            caps.SetCapability("username", _sauceUserName);
            caps.SetCapability("accessKey", _sauceAccessKey);
            caps.SetCapability("name", TestContext.CurrentContext.Test.Name);

            //Tags are an excellent way to control and filter your test automation
            //in Sauce Analytics. Get a better view into your test automation.
            var tags = new List<string> { "demoTest", "sauceDemo" };
            caps.SetCapability("tags", tags);
            /*
             * One of the most important things that you can do to get started
             * is to set timeout capabilities for Sauce based on your organizations needs
             */
            //How long is the whole test allowed to run?
            caps.SetCapability("maxDuration", 3600);
            //Selenium crash might hang a command, this is the max time allowed to wait for a Selenium command
            //600sec is a great start for majority of engineers
            caps.SetCapability("commandTimeout", 600);
            //How long can the browser wait before a new command?
            //1000sec is max and is a good timeout duration for most engineers
            caps.SetCapability("idleTimeout", 1000);
            /*
             * Setting a build name is one of the most fundamental pieces of running
             * successful test automation. Builds will gather all of your tests into a single
             * 'test suite' that you can analyze for results.
             * You should always group your tests into builds.
             */
            caps.SetCapability("build", "SauceDemo");

            _driver = new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com:80/wd/hub"),
                caps, TimeSpan.FromSeconds(600));

            _javascriptExecutor = ((IJavaScriptExecutor)_driver);
        }


        [TearDown]
        public void CleanUpAfterEveryTestMethod()
        {
            var passed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed;
            _javascriptExecutor.ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
            _driver?.Quit();
        }
    }
}

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. 

For more information, check out the example scripts in our GitHub repo.  



With Sauce Labs you can run automated tests of your web apps against a wide variety of device/operating system/browser combinations.  This topic provides you with the information and code examples you need to get your PHP tests up and running with Sauce.

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

Before you get started, you should review the Best Practices for Running Tests.

You need to have these components installed to set up testing on Sauce with PHP:

Quick Start

You can set up any PHP test you've already written to run on Sauce, regardless of the testing framework it uses. All you need to do is change the test from running locally, to running on the Sauce cloud by defining the sauce URL, your authentication credentials, and a set of desired capabilities for the test. 

This example shows how you could edit the existing test to run on Sauce. You can use the Platform Configurator to specify the desired capabilities for any browser/platform combination you want. 

You can also clone this script directly from our GitHub repo.

<?php
  // Setup: $ php composer.phar require facebook/webdriver

  require_once('vendor/autoload.php');
  use Facebook\WebDriver\Remote\RemoteWebDriver;
  use Facebook\WebDriver\WebDriverBy;

  $web_driver = RemoteWebDriver::create(
    "https://YOUR_SAUCE_USERNAME:YOUR_SAUCE_ACCESS_KEY@ondemand.saucelabs.com:443/wd/hub",
    array("platform"=>"Windows 7", "browserName"=>"chrome", "version"=>"40")
  );
  $web_driver->get("https://saucelabs.com/test/guinea-pig");

  /*
    Test actions here...
  */

  $web_driver->quit();
?>

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. 

For more information, check out the example scripts in our GitHub repo

Reporting Test Results

Sausage includes a number of features that make it easier for Sauce to report on your test results. 

By default, Sauce Labs doesn't know how to display the name of your test. Sausage comes up with a good name (TestClass::testFunction) and reports it with your test so it's easy to find on your dashboard. Similarly, Sauce has no way to know if a particular test passed or failed. Sausage catches any failed assertions and reports the status of the test to Sauce after it's complete. Upon test failure Sausage will generate an authorized link to the failed job report on the Sauce Labs website, to facilitate reporting to people who need to know the details of the test. The job remains private (unless you change the status yourself), but others can follow the link without needing to log in with your credentials. See the topic Building Links to Test Results for more information.

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, and associate tests with build numbers in your continuous integration pipeline.

Got questions?

As these scripts are not maintained by Sauce support, the best way to get help help – and help us improve – is to submit an issue on GitHub.



  • No labels