Cloud Execution & BrowserStack

31Cloud Execution & BrowserStack

What you will master here

  • Why and when to run tests in the cloud
  • BrowserStack, Sauce Labs, LambdaTest — comparison + setup
  • Playwright remote browsers (CDP endpoint)
  • Real device vs emulator vs cloud-VM
  • Parallel + cross-browser at scale
  • Cost + privacy considerations
  • Self-hosted Selenium Grid / Playwright Grid alternatives

31.1 Why run tests in the cloud

31.2 The vendor landscape

VendorStrengthsNotes
BrowserStackLargest device farm, real iOS/Android, mature Playwright integration"Browserstack Automate" for desktop, "App Automate" for mobile native
Sauce LabsEnterprise-grade, strong reports, security certificationsPricier; common in regulated industries
LambdaTestCompetitive pricing, "TestMu AI" featuresCatching up on real-device count
CrossBrowserTesting (SmartBear)Mature, decent device farmLess Playwright-first
Microsoft Playwright Service (Azure)First-party — runs Playwright on Azure, integrated with Playwright Test runnerPure parallel scaling; no real-device support yet

31.3 Running Playwright on BrowserStack

// playwright-bs.config.ts
import { defineConfig, devices } from '@playwright/test';

const BS_USER = process.env.BROWSERSTACK_USERNAME!;
const BS_KEY  = process.env.BROWSERSTACK_ACCESS_KEY!;

function bsCdp(opts: { browser: string; os: string; osVersion: string; browserVersion?: string }) {
  const caps = {
    browser: opts.browser,
    'browser_version': opts.browserVersion || 'latest',
    os: opts.os,
    'os_version': opts.osVersion,
    'browserstack.username': BS_USER,
    'browserstack.accessKey': BS_KEY,
    'browserstack.local': false,
    'project': 'Acme', 'build': process.env.GITHUB_RUN_ID,
  };
  return `wss://cdp.browserstack.com/playwright?caps=${encodeURIComponent(JSON.stringify(caps))}`;
}

export default defineConfig({
  workers: 5,
  use: { ignoreHTTPSErrors: true },
  projects: [
    { name: 'chrome-win11',  use: { connectOptions: { wsEndpoint: bsCdp({ browser: 'chrome',  os: 'Windows', osVersion: '11' }) } } },
    { name: 'safari-mac14',  use: { connectOptions: { wsEndpoint: bsCdp({ browser: 'webkit',  os: 'OS X',    osVersion: 'Sonoma' }) } } },
    { name: 'firefox-linux', use: { connectOptions: { wsEndpoint: bsCdp({ browser: 'firefox', os: 'Linux',   osVersion: 'Ubuntu' }) } } },
  ],
});

Run as usual: npx playwright test --config=playwright-bs.config.ts. Sessions appear in your BrowserStack dashboard with video, network, console.

31.4 Microsoft Playwright Service (Azure)

// Microsoft's first-party cloud service for Playwright Test
import { defineConfig } from '@playwright/test';
import { getServiceConfig, ServiceOS } from '@azure/microsoft-playwright-testing';

export default defineConfig(
  getServiceConfig(import('./playwright.config'), {
    serviceAuthType: 'ENTRA_ID',
    os: ServiceOS.LINUX,
    runId: process.env.GITHUB_RUN_ID,
    useCloudHostedBrowsers: true,
  }),
);

You write tests once; runner farms them to Azure browsers. Reports surface in Azure portal.

31.5 BrowserStack Local — for staging not on public internet

Your staging URL is private (only reachable from VPN / corp network). BrowserStack Local opens a secure tunnel from the BrowserStack browser to your machine / CI runner.

npm i -D browserstack-local
# Or download CLI:
./BrowserStackLocal --key $BROWSERSTACK_ACCESS_KEY --local-identifier acme-pr-123

Pass browserstack.local: true in caps, then BrowserStack browsers can reach https://staging.internal.acme.com.

31.6 Self-hosted alternative — Playwright Selenium Grid replacement

If you can't use a vendor (regulatory, cost), self-host. Options:

# k8s — simple per-test pod approach
apiVersion: batch/v1
kind: Job
metadata: { name: playwright-shard-3 }
spec:
  template:
    spec:
      containers:
      - name: pw
        image: mcr.microsoft.com/playwright:v1.50.0-jammy
        command: ["npx","playwright","test","--shard=3/8"]
      restartPolicy: Never

31.7 Real-device vs emulator vs cloud-VM

OptionProsCons
Real device (BrowserStack, Sauce)True iOS Safari, hardware sensors, real touchMost expensive; slower; queueing
Cloud emulator (BrowserStack)Cheap, parallel, fastMisses hardware bugs
Local emulator (Android Studio / Xcode)Free, debuggableSlow setup, one at a time
Playwright device profile (emulation)Free, fast, in your normal CIRuns on Linux WebKit — not real iOS Safari
Practical mix80% on Playwright device emulation (cheap, fast, in CI). 15% on cloud emulator for Safari coverage. 5% on real device sample for critical flows before release.

31.8 Cost + privacy

Module 29 — Cloud Execution Q&A

Why run tests in the cloud?
Cross-browser/device coverage (Safari only on macOS; real iOS), parallelism beyond local CI, geo-distributed testing, and access to private staging from anywhere. Trade-off: cost per session and PII considerations.
How does Playwright connect to BrowserStack?
Via a WebSocket endpoint. You build a wss://cdp.browserstack.com/playwright?caps=... URL with browser/OS capabilities and your credentials, then use it as connectOptions.wsEndpoint in the Playwright config. Each test session is a remote browser on their fleet.
What's BrowserStack Local?
A secure tunnel binary that bridges BrowserStack's cloud browsers to your private staging environment. The remote browser thinks it's talking to a public URL but actually traffic flows through your machine to your internal server.
What's the Microsoft Playwright Service?
Azure-hosted Playwright execution — first-party from the Playwright team. You configure your existing Playwright project to run remote browsers on Azure; tests run there in parallel. Pure scale, no real-device support yet.
Real device vs emulator — when to use which?
Emulator (Playwright device profile or cloud emulator) for 95% of mobile coverage — cheap, fast, in CI. Real device for final sign-off of critical flows: touch handlers, iOS Safari quirks, hardware sensors. Sample real-device runs nightly or before release.
How do you self-host browser parallelism without vendors?
Run Playwright in containers (mcr.microsoft.com/playwright image) orchestrated by Kubernetes/Nomad. Each shard is one Job/Pod. For Selenium-style language-agnostic use, Selenium Grid 4 or Aerokube Moon.
What are typical cloud-execution costs?
Vendors charge per parallel session-minute. 5 parallel × 8h × 22 days ≈ $400-800/month for mid-tier plans. Real-device plans are ~3× emulator plans. Negotiate annual contracts; usage often grows.
What's a key privacy concern with cloud testing?
Anything your test enters into the cloud browser is captured in the vendor's video and logs — including PII or secrets if you're careless. Use synthetic data, mask sensitive fields, or self-host for regulated data.
How do you debug a failing test that only fails on a specific browser in the cloud?
Vendors capture video + network + console per session. Open the session in their dashboard, scrub the video, check console errors. If you need deeper, run the same configuration locally (e.g. brew install playwright webkit, then Playwright test --project=webkit) — match the rendering engine.
Can you run Playwright with Selenium Grid?
Not directly — Playwright doesn't speak WebDriver. But you can connect Playwright to a remote browser via CDP/WebSocket (their connect or launchServer pattern). For Selenium Grid-style management of Playwright, use Aerokube Moon or self-managed Kubernetes pods.