What is saucelabs?
The 'saucelabs' npm package provides a Node.js client for interacting with the Sauce Labs API. Sauce Labs is a cloud-based platform for automated testing of web and mobile applications. This package allows you to manage Sauce Labs resources, such as jobs, tunnels, and assets, programmatically.
What are saucelabs's main functionalities?
Managing Jobs
This feature allows you to manage and retrieve details about your Sauce Labs jobs. The code sample demonstrates how to get details of a specific job using its job ID.
const { SauceLabs } = require('saucelabs');
const saucelabs = new SauceLabs({ user: 'your-username', key: 'your-access-key' });
async function getJobDetails(jobId) {
const job = await saucelabs.getJob(jobId);
console.log(job);
}
getJobDetails('your-job-id');
Managing Tunnels
This feature allows you to manage Sauce Connect tunnels. The code sample demonstrates how to list all active tunnels.
const { SauceLabs } = require('saucelabs');
const saucelabs = new SauceLabs({ user: 'your-username', key: 'your-access-key' });
async function getTunnels() {
const tunnels = await saucelabs.listTunnels();
console.log(tunnels);
}
getTunnels();
Uploading Assets
This feature allows you to upload assets to a specific job. The code sample demonstrates how to upload a file to a job using its job ID.
const { SauceLabs } = require('saucelabs');
const saucelabs = new SauceLabs({ user: 'your-username', key: 'your-access-key' });
async function uploadAsset(jobId, filePath) {
const response = await saucelabs.uploadJobAssets(jobId, { files: [filePath] });
console.log(response);
}
uploadAsset('your-job-id', 'path/to/your/file');
Other packages similar to saucelabs
webdriverio
WebdriverIO is a popular testing utility for Node.js that provides a high-level API for browser automation. It supports multiple browser drivers and services, including Sauce Labs. Compared to the 'saucelabs' package, WebdriverIO offers a more comprehensive solution for writing and running tests, while 'saucelabs' focuses on managing Sauce Labs resources.
selenium-webdriver
Selenium WebDriver is a widely-used tool for browser automation. It provides bindings for multiple programming languages, including JavaScript. While Selenium WebDriver can be used with Sauce Labs for running tests on their cloud infrastructure, it does not provide the same level of resource management capabilities as the 'saucelabs' package.
cypress
Cypress is an end-to-end testing framework that aims to make testing fast, easy, and reliable. It provides a rich API for writing tests and a powerful test runner. While Cypress does not natively integrate with Sauce Labs, it can be configured to run tests on Sauce Labs infrastructure. Compared to 'saucelabs', Cypress focuses more on the testing experience and less on resource management.
Node Sauce Labs
Wrapper around all Sauce Labs REST APIs for Node.js (v8 or higher) including support for Sauce Connect Proxy and TypeScript definitions.
Install
To install the package run:
npm install saucelabs
Options
user
Your Sauce Labs username.
Type: string
Default: process.env.SAUCE_USERNAME
key
Your Sauce Labs access key.
Type: string
Default: process.env.SAUCE_ACCESS_KEY
region
Your Sauce Labs datacenter region. The following regions are available:
us-west-1
(short us
)eu-central-1
(short eu
)us-east-1
(headless)
Type: string
Default: us
headless
If set to true you are accessing the headless Sauce instances (this discards the region
option).
Type: boolean
Default: false
proxy
If you want to tunnel your API request through a proxy please provide your proxy URL.
Type: string
Default: null
If you want to set request headers, as example {'User-Agent': 'node-saucelabs'}
Type: object
Default: {'User-Agent': 'saucelabs/<VERSION> (nodejs <PLATFORM>)'}
Usage
All accessible API commands with descriptions can be found here.
As CLI Tool
This package if installed globally can be used as CLI tool to access the API from the command line:
$ npm install -g saucelabs
...
$ sl listJobs $SAUCE_USERNAME --limit 5 --region eu
{ jobs:
[ { id: '19dab74f8fd848518f8d2c2cee3a6fbd' },
{ id: 'dc08ca0c7fa14eee909a093d11567328' },
{ id: '5bc6f70c777b4ae3bf7909a40f5ee41b' },
{ id: 'f40fe7b044754eaaa5f5a130406549b5' },
{ id: 'd1553f71f910402893f1e82a4dcb6ca6' } ] }
You can find all available commands and options with description by calling:
$ sl --help
$ sl listJobs --help
or update the job status by calling:
$ sl updateJob cb-onboarding 690c5877710c422d8be4c622b40c747f "{\"passed\":false}"
or download a job asset:
$ sl downloadJobAsset 690c5877710c422d8be4c622b40c747f video.mp4 --filepath ./video.mp4
or upload a job asset:
$ sl uploadJobAssets 690c5877710c422d8be4c622b40c747f --files ./video.mp4 --files ./log.json
or start Sauce Connect Proxy in EU datacenter:
$ sl sc --region eu --tunnel-identifier "my-tunnel"
$ sl sc --scVersion 4.5.4
$ sl sc --help
You can see all available Sauce Connect parameters on the Sauce Labs Wiki Page.
As NPM Package
The following example shows how to access details of the last job you were running with your account that is being exposed as environment variables as SAUCE_USERNAME
and SAUCE_ACCESS_KEY
. Alternatively you can pass the credentials via options
to the constructor:
import SauceLabs from 'saucelabs';
(async () => {
const myAccount = new SauceLabs();
console.log(myAccount.webdriverEndpoint)
const jobs = await myAccount.listJobs(
process.env.SAUCE_USERNAME,
{ limit: 1, full: true }
);
console.log(jobs);
const sc = await myAccount.startSauceConnect({
logger: (stdout) => console.log(stdout),
tunnelIdentifier: 'my-tunnel'
})
await sc.close()
await myAccount.uploadJobAssets(
'76e693dbe6ff4910abb0bc3d752a971e',
['video.mp4', 'log.json']
)
})()
You may wonder why listJobs
requires a username
as first parameter since you've already defined the process.env. The reason for this is that Sauce Labs supports a concept of Team Accounts, so-called sub-accounts, grouped together. As such functions like the mentioned could list jobs not only for the requesting account, but also for the individual team account. Learn more about it here
webdriverEndpoint
property
You can use the webdriverEndpoint
property of the client to get the full WebDriver endpoint to connect to Sauce Labs, e.g.:
const myAccount = new SauceLabs({
user: "YOUR-USER",
key: "YOUR-ACCESS-KEY",
region: 'eu'
});
console.log(myAccount.webdriverEndpoint);
Breaking changes from v1 to v2
Public APIs have changed from v1 to v2. Methods in v1 accepted a callback
trailing parameter which is no more available with v2, instead all methods now return a Promise which can be awaited
or then
'd.
Below, you can find the list of the mapped method names:
v1 | v2 |
---|
getAccountDetails(callback) | async getUser(username) |
getAccountLimits(callback) | ? |
getUserActivity(callback) | async getUserActivity(username) |
getUserConcurrency(callback) | async getUserConcurrency(username) |
getAccountUsage(start, end, callback) | ? |
getJobs(callback) | async listJobs(username, { ...options }) // with option: full: true |
showJob(id, callback) | async getJob(username, id) |
showJobAssets(id, callback) | ? |
updateJob(id, data, callback) | async updateJob(username, id, body) |
stopJob(id, data, callback) | async stopJob(username, id) |
deleteJob(id, callback) | ? |
getActiveTunnels(callback) | async listAvailableTunnels(username) |
getTunnel(id, callback) | async getTunnel(username, id) |
deleteTunnel(id, callback) | async deleteTunnel(username, id) |
getServiceStatus(callback) | async getStatus() |
getBrowsers(callback) | ? |
getAllBrowsers(callback) | async listPlatforms(platform) // pass "all" |
getSeleniumBrowsers(callback) | Selenium-RC no longer supported |
getWebDriverBrowsers(callback) | async listPlatforms(platform) // pass "webdriver" |
getTestCounter(callback) | ? |
updateSubAccount(data, callback) | ? |
deleteSubAccount(callback) | ? |
createSubAccount(data, callback) | ? |
createPublicLink(id, date, useHour, callback) | ? |
getSubAccountList(callback) | ? |
getSubAccounts(callback) | ? |
This module was originally created by Dan Jenkins with the help of multiple contributors (Daniel Perez Alvarez, Mathieu Sabourin, Michael J Feher, and many more). We would like to thank Dan and all contributors for their support and this beautiful module.