Sauce Labs Tutorial: Cross-Browser Testing in the Cloud

Sauce Labs Tutorial: Cross-Browser Testing in the Cloud

Sauce Labs is one of the oldest cloud testing platforms in the industry, offering access to thousands of browser and OS combinations without managing your own infrastructure. This tutorial walks through setting up Sauce Labs for cross-browser testing, running your first tests, and integrating with CI/CD pipelines.

What Is Sauce Labs?

Sauce Labs is a cloud-based testing platform that provides on-demand access to real browsers, mobile devices, and operating systems for automated and manual testing. Instead of maintaining a local Selenium Grid or virtual machine farm, you point your WebDriver tests at Sauce Labs and let them handle the infrastructure.

Key capabilities:

  • Cross-browser testing — Chrome, Firefox, Safari, Edge across hundreds of versions
  • Real device testing — iOS and Android physical devices
  • Sauce Connect — secure tunnel for testing apps behind firewalls
  • Visual testing — screenshot comparison and visual diffing
  • Analytics — test history, failure trends, and performance insights

Setting Up Sauce Labs

1. Create an Account

Sign up at saucelabs.com. The free tier gives you limited parallel tests and minutes — sufficient for evaluating the platform. Paid plans start around $199/month for teams.

2. Get Your Credentials

From your account dashboard, navigate to User Settings and copy your:

  • Username
  • Access Key

Store these as environment variables:

export SAUCE_USERNAME=<span class="hljs-string">"your-username"
<span class="hljs-built_in">export SAUCE_ACCESS_KEY=<span class="hljs-string">"your-access-key"

Never hardcode credentials in test files.

3. Install Dependencies

For a Node.js project using WebdriverIO:

npm install --save-dev @wdio/cli @wdio/sauce-service

For Python with Selenium:

pip install selenium

Running Your First Test

WebdriverIO Configuration

Create wdio.sauce.conf.js:

exports.config = {
  user: process.env.SAUCE_USERNAME,
  key: process.env.SAUCE_ACCESS_KEY,
  region: 'us',
  
  capabilities: [{
    browserName: 'chrome',
    browserVersion: 'latest',
    platformName: 'Windows 11',
    'sauce:options': {
      name: 'My First Sauce Test',
      build: `Build ${process.env.CI_BUILD_NUMBER || 'local'}`,
    }
  }],
  
  services: ['sauce'],
  
  specs: ['./tests/**/*.spec.js'],
  
  framework: 'mocha',
  reporters: ['spec'],
};

A Simple Test

// tests/homepage.spec.js
describe('Homepage', () => {
  it('should load the title correctly', async () => {
    await browser.url('https://example.com');
    const title = await browser.getTitle();
    expect(title).toContain('Example');
  });
});

Run it:

npx wdio run wdio.sauce.conf.js

You'll see the test appear in your Sauce Labs dashboard with a video recording, logs, and screenshots.

Python + Selenium

from selenium import webdriver
from selenium.webdriver.common.by import By
import os

sauce_options = {
    'username': os.environ['SAUCE_USERNAME'],
    'accessKey': os.environ['SAUCE_ACCESS_KEY'],
    'name': 'Homepage Test',
    'build': 'CI Build 1',
}

options = webdriver.ChromeOptions()
options.set_capability('platformName', 'Windows 11')
options.set_capability('browserVersion', 'latest')
options.set_capability('sauce:options', sauce_options)

driver = webdriver.Remote(
    command_executor='https://ondemand.us-west-1.saucelabs.com/wd/hub',
    options=options
)

driver.get('https://example.com')
assert 'Example' in driver.title
driver.quit()

Cross-Browser Testing Matrix

The power of Sauce Labs is running the same tests across many browsers simultaneously. Configure multiple capabilities:

capabilities: [
  {
    browserName: 'chrome',
    browserVersion: 'latest',
    platformName: 'Windows 11',
    'sauce:options': { name: 'Chrome Latest - Win11' }
  },
  {
    browserName: 'firefox',
    browserVersion: 'latest',
    platformName: 'Windows 11',
    'sauce:options': { name: 'Firefox Latest - Win11' }
  },
  {
    browserName: 'safari',
    browserVersion: '17',
    platformName: 'macOS 14',
    'sauce:options': { name: 'Safari 17 - macOS' }
  },
  {
    browserName: 'MicrosoftEdge',
    browserVersion: 'latest',
    platformName: 'Windows 11',
    'sauce:options': { name: 'Edge Latest - Win11' }
  }
]

With maxInstances: 4, all four run in parallel.

Sauce Connect Tunnel

If your app runs on localhost or behind a corporate firewall, you need Sauce Connect:

# Download Sauce Connect
curl -Lo sc.zip https://saucelabs.com/downloads/sauce-connect/latest/linux.zip
unzip sc.zip

<span class="hljs-comment"># Start tunnel
./sc --username <span class="hljs-variable">$SAUCE_USERNAME --access-key <span class="hljs-variable">$SAUCE_ACCESS_KEY --tunnel-name my-tunnel

Reference the tunnel in your capabilities:

'sauce:options': {
  tunnelName: 'my-tunnel',
}

Now Sauce Labs browsers can reach localhost:3000 through the secure tunnel.

CI/CD Integration

GitHub Actions

name: Cross-Browser Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      
      - name: Install dependencies
        run: npm ci
      
      - name: Run Sauce Labs tests
        env:
          SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
          SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
        run: npx wdio run wdio.sauce.conf.js

Add SAUCE_USERNAME and SAUCE_ACCESS_KEY to your repository secrets.

Sauce Labs vs Running Your Own Grid

Factor Sauce Labs Self-Hosted Grid
Setup time Minutes Days to weeks
Browser coverage 800+ combinations Limited to what you configure
Maintenance None Ongoing
Cost $199+/month Infrastructure + engineer time
Video/logs Built-in Requires extra tooling

For most teams, Sauce Labs eliminates significant infrastructure overhead. The cost becomes worth it when cross-browser coverage matters and maintenance time is expensive.

Common Pitfalls

Session timeouts. Sauce Labs kills idle sessions after 90 seconds by default. Add idleTimeout to your capabilities if tests have long waits.

Test naming. Always set name and build in sauce:options. Without names, the dashboard becomes unusable after a few hundred test runs.

Region selection. Use the closest region endpoint (us-west-1, eu-central-1) to reduce latency. A test running in the EU hitting the US endpoint adds unnecessary delay.

Parallel limits. Your plan defines how many parallel sessions you can run. Exceeding it queues tests — watch for unexplained slowdowns on large test suites.

Beyond Cross-Browser: HelpMeTest for Continuous Monitoring

Sauce Labs handles on-demand test runs — but production monitoring requires something running 24/7. HelpMeTest complements cloud testing platforms by continuously running your critical user flows against production, alerting you within minutes when something breaks.

Instead of re-running your full browser matrix on a schedule, HelpMeTest lets you write tests in plain English, runs them every 5 minutes, and sends alerts before users report problems. Free plan includes 10 tests with unlimited health checks — no credit card required.

Summary

Sauce Labs gives you instant access to a massive browser and device grid without infrastructure management. The core workflow:

  1. Set credentials as environment variables
  2. Point WebDriver at the Sauce Labs endpoint
  3. Specify browser, version, and OS in capabilities
  4. Run tests — video, logs, and screenshots captured automatically
  5. Use Sauce Connect for local/private environments
  6. Integrate with CI/CD via environment variable injection

For teams doing serious cross-browser testing, the infrastructure savings typically outweigh the subscription cost quickly.

Read more