Notes
Workers
Playwright runs tests in worker processes, each with its own browser. By default it uses about half your CPU cores, runs different files in parallel, and runs tests inside one file in order. Fully parallel lets every test run on any worker, which balances better. Serial mode forces a group to run in order, which you should rarely need. Set workers in the config or on the command line.
workers: process.env.CI ? 2 :
undefined // default: half CPUs
by default: FILES in parallel,
tests in a file run in order
fullyParallel: true
every test can run anywhere
test.describe.configure(
{ mode: "serial" }) rare!
npx playwright test --workers=4 The trap
Here is the trap, run for real. Six tests each reset the shop, log in as Alice, add two notebooks, and expect a cart count of two. With one worker, all six passed. With four workers fully parallel, all six failed: two saw four items and four saw six. The tests were adding to the same cart, and one test's reset wiped another's data mid run. Nothing was wrong with ShopRung; the tests shared state.
beforeEach: reset everything each test: log in as ALICE, add 2 notebooks, expect 2 --workers=1 6 passed --workers=4 --fully-parallel 6 failed Received: 4 (2 tests) Received: 6 (4 tests) tests added to the SAME cart
The fix
The fix: each test creates its own data. Here every test signs up a new user through the API, with an email built from the worker index, a timestamp and the test number, so it is unique. No global reset is needed. With four workers, fully parallel, six passed, and again on a second run. The rule for parallel suites: never share mutable data between tests, and never reset data other tests are using.
test("own user...", async
({ request }, info) => {
const id = `${info.workerIndex}` +
`-${Date.now()}-${i}`;
const email = `u${id}@shoprung.test`;
await request.post(
"/api/auth/signup", {data:
{ name, email, password }});
... add 2, expect 2 });
--workers=4 --fully-parallel
6 passed, twice Sharding
Workers use the cores of one machine. Sharding splits the suite across machines: four CI jobs each run with shard one of four, two of four, and so on. Each job writes a blob report, and a final step merges them into one HTML report. Workers and shards combine: four machines with two workers each run eight tests at a time. Part eight sets this up in GitHub Actions.
# 4 CI jobs, one shard each npx playwright test --shard=1/4 npx playwright test --shard=2/4 ... reporter: "blob" on each npx playwright merge-reports --reporter html ./all-blobs workers: inside one machine shards: across machines
Projects
For cross browser runs, Playwright uses projects. One project each for Chromium, Firefox and WebKit, using built in device descriptors for viewport and user agent, plus a mobile viewport such as a Pixel phone. Every test runs once per project, and you can run just one with the project flag. Mobile viewports in a desktop browser are emulation, good for responsive layout, not a substitute for real devices in Part six.
projects: [
{ name: "chromium", use:
devices["Desktop Chrome"] },
{ name: "firefox", use:
devices["Desktop Firefox"] },
{ name: "webkit", use:
devices["Desktop Safari"] },
{ name: "mobile", use:
devices["Pixel 7"] },
]
npx playwright test --project=webkit What to cover
Cross browser runs multiply the time, so choose. A common split: the full suite on one engine for every commit, a smoke set on all engines for each pull request, and everything on all engines nightly or before a release. Choose browsers from your real user analytics, not habit. Typical cross browser bugs are layout and CSS differences, date and input handling, storage behaviour and file downloads.
- every commit: one engine, full suite
- nightly or pre-release: all engines, full suite
- per PR: smoke tests on all engines
- pick browsers from YOUR analytics, not habit
- bugs found: layout, dates, input types, storage, CSS
Selenium Grid
Selenium scales with Selenium Grid. The simplest start is a Docker image, such as standalone Chrome on port four four four four, with extra shared memory so Chrome does not crash. Tests create a remote web driver with the grid's address and the browser options, instead of a local Chrome driver. Larger setups run a hub with many nodes, or on Kubernetes, and commercial cloud grids accept the same remote driver calls.
docker run -d -p 4444:4444
--shm-size=2g
selenium/standalone-chrome
var o = new ChromeOptions();
WebDriver d = new RemoteWebDriver(
new URL("http://localhost:4444"),
o);
hub + nodes, or Kubernetes;
cloud grids use the same API Parallel in Java
With Selenium, the test runner provides parallelism. JUnit five turns it on in a properties file, with parallel enabled and concurrent mode. TestNG sets parallel methods and a thread count in the suite file. Pytest uses x dist with dash n four. In every case, each thread needs its own driver, never a shared static one, usually held in a thread local or created per test.
# junit-platform.properties
junit.jupiter.execution
.parallel.enabled = true
junit.jupiter.execution.parallel
.mode.default = concurrent
# TestNG
<suite parallel="methods"
thread-count="4">
# pytest
pytest -n 4 (pytest-xdist) Common mistakes
Mistakes. Shared users, carts or orders between tests. Global resets while other tests run. A static shared web driver. Tests that only pass in a particular order. Every browser on every commit, which slows feedback. Treating mobile emulation as real device coverage. And sharded runs without a merged report, so nobody sees the whole picture.
- shared users, carts, orders
- global resets mid-run
- static shared WebDriver
- tests that need a run order
- all browsers on every commit (slow feedback)
- treating emulation as real device coverage
- no merged report for shards
Say it like this
In the interview: first I isolate data, so each test creates its own user and records through the API, with no global resets. Then I add workers on one machine and shards across CI jobs, merging the reports. For coverage, I use browser projects in Playwright or Selenium Grid, running one engine per commit and all engines nightly, with browsers chosen from analytics.
- "First isolate data: each test
- creates its own user and records via the API, no global
- resets. Then workers on one machine and shards across CI
- jobs with a merged report.
- Browser projects or Selenium
- Grid for coverage: one engine
- per commit, all engines
- nightly, chosen by analytics."