Skip to end of metadata
Go to start of metadata

Sauce Labs is a cloud platform for executing automated and manual mobile and web tests. Sauce Labs supports running automated tests with Selenium WebDriver (for web applications) and Appium (for native and mobile web applications).This tutorial will provide you with an example of how to get your node.js tests up and running on 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 should install Selenium WebDriver 2.47.0 or lower
  • You will need to have Node.js v.0.11 or higher already installed

Quick Start

Configuring Selenium tests to run on Sauce Labs is simple. The basic change is to switch from using a local Selenium driver, to using a remote driver pointed at ondemand.saucelabs.com. Note that this example assumes you have set your Sauce Labs authentication credentials as environment variables, a recommended best practice for testing on Sauce. This code sample shows how to set up a very simple standalone JavaScript test script that will authenticate you to Sauce Labs, and then test against Chrome to find the title of Google's home page. 

standalone.chrome.js Code Example
var client = require('webdriverio').remote({
    user: process.env.SAUCE_USERNAME,
    key: process.env.SAUCE_ACCESS_KEY,
    desiredCapabilities: {
        browserName: 'chrome'
    }
});

client
    .init()
    .url('http://google.com')
    .getTitle().then(console.log)
    .end();

Running the Sample Test

Once you have everything set up, you can run the example test simply by running the example file. 

$ node sauce_node_example_test.js

Code Sample

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

var webdriver = require('selenium-webdriver'),
    username = "YOUR_SAUCE_USERNAME",
    accessKey = "YOUR_SAUCE_ACCESS_KEY",
    driver;

driver = new webdriver.Builder().
  withCapabilities({
    'browserName': 'chrome',
    'platform': 'Windows XP',
    'version': '43.0',
    'username': username,
    'accessKey': accessKey
  }).
  usingServer("http://" + username + ":" + accessKey +
              "@ondemand.saucelabs.com:80/wd/hub").
  build();

driver.get("http://saucelabs.com/test/guinea-pig");

driver.getTitle().then(function (title) {
    console.log("title is: " + title);
});

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. 

If you want to use Sauce Connect, you need to set host to localhost and the port to 4445, as shown in this example script.
Sauce Connect and Node.js Example
var client = require('webdriverio').remote({
    user: process.env.SAUCE_USERNAME,
    key: process.env.SAUCE_ACCESS_KEY,
    host: 'localhost',
    port: 4445,
    desiredCapabilities: {
        browserName: 'chrome'
    }
});

client
    .init()
    .url('http://localhost')
    .getTitle().then(console.log)
    .end();

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. For more on this see the Selenium WebDriver - Running Your Tests in Parallel blog.

Before you start running tests in parallel, you should review the Best Practices for Running Tests, especially the topics on avoiding external test dependencies and avoiding dependencies between tests.

Match Thread Count to Concurrency Limit

You should match your thread count to your concurrency limit, which is shown in the My Account section of your user profile information on the Sauce Labs dashboard.  

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

Reporting on Test Results

"Wait," you might be asking, "My test says 'Complete' 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. You can, however, tell Sauce about the results of your tests as shown by this code snippet from the example file for running node.js files in parallel.

saucelabs.updateJob(driver.sessionID, {
      name: title,
      passed: passed
    }, done);