Skip to content

browserstack/jest-playwright-browserstack

Repository files navigation

jest-playwright-browserstack

This sample shows how to run Jest + Playwright tests on BrowserStack using the BrowserStack Node SDK. The SDK reads browserstack.yml, fans your tests out across the platforms listed there, starts and stops BrowserStack Local automatically, and reports test status to the BrowserStack dashboard. Your test code stays plain @playwright/test + Jest -- no manual connect(), no capabilities in code.

BrowserStack Logo

Prerequisites

Setup

  • Clone the repo

  • Install dependencies:

    npm install
  • Add your credentials to browserstack.yml (replace YOUR_USERNAME / YOUR_ACCESS_KEY), or remove those two lines and export them as environment variables instead:

    export BROWSERSTACK_USERNAME=<browserstack-username>
    export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>

Run Sample Test

Runs the public bstackdemo.com add-to-cart scenario across every platform in browserstack.yml:

npm run sample-test

Run Local Test (BrowserStack Local)

Use this when the site under test is on localhost, a staging host, or behind a firewall. BrowserStack Local opens a secure tunnel and resolves bs-local.com back to your machine, so the cloud browser can reach a page only you can serve.

The bundled local test navigates to http://bs-local.com:45454/ and asserts the page title contains BrowserStack Local — so you must have something serving that page on port 45454 first:

  1. Serve a matching page locally (any page whose <title> contains "BrowserStack Local" satisfies the test):

    mkdir -p bs-local-site && printf '<!doctype html><title>BrowserStack Local</title><body>OK</body>' > bs-local-site/index.html
    (cd bs-local-site && python3 -m http.server 45454) &

    To test your own app instead, serve it on port 45454 (or change the port and the title assertion in tests/bstack_local_test.test.js).

  2. Set browserstackLocal: true in browserstack.yml.

  3. Run:

    npm run sample-local-test

The SDK starts and stops the BrowserStack Local tunnel for you -- no manual binary download or lifecycle management. The tunnel routes bs-local.com:45454 to your machine's localhost:45454.

How the SDK changes things

  • One browserstack.yml declares platforms, parallelism, the Local toggle, and reporting; the SDK picks them up automatically.
  • The SDK runs platforms in parallel for you -- one Jest run per (platform x parallelsPerPlatform) cell, no per-platform branching needed.
  • The SDK rewrites Playwright launches -- your test calls chromium.launch() and the SDK transparently redirects it to the per-platform browser configured in the yml (chrome / playwright-webkit / playwright-firefox). No chromium.connect(wss_url) plumbing.
  • The SDK starts and stops BrowserStack Local when browserstackLocal: true.

Repo layout

.
├── browserstack.yml            # SDK config: credentials, platforms, Local toggle, reporting
├── package.json                # SDK run scripts + deps
├── jest.config.js              # sample test config (bstackdemo)
├── jest.local.config.js        # local test config (bs-local)
└── tests/
    ├── bstack_sample.test.js       # bstackdemo add-to-cart scenario
    └── bstack_local_test.test.js   # BrowserStack Local scenario

Notes

Further Reading

Happy Testing!

About

Sample repo for customers

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors