Skip to content
Guides · Use cases

How Do I Run Playwright Tests in Parallel Across Sandboxes?

Updated November 23, 2026 .md

Short answer: Give each Playwright worker its own vCPU first, then shard with --shard=i/n across sandboxes started from a named snapshot. Merge the blob reports with merge-reports, and copy each failure's error-context.md and trace.zip out with sbx scp. On prod, one 4 vCPU sandbox beat two 2 vCPU shards for a 17-test suite.

Part of Coding Agents in the Cloud: Parallel, Overnight, Own Keys, Checkpoints.

At a glance#

One suite, five ways to run it. The suite is 17 Playwright Test tests against a tiny Node app on 127.0.0.1:3000. Each page runs a CPU-bound loop in the browser before it renders, and 2 tests fail on purpose. Playwright 1.64.1, its own Chromium, one run per row.

Setup Workers Wall time What it tells you
1 small sandbox (2 vCPU, 4 GB) 1 51.8 s The serial baseline
1 small sandbox 2 29.9 s One worker per vCPU: 42% faster
1 small sandbox 4 27.7 s Twice the workers, 7% faster: they fight for 2 vCPUs
1 default sandbox (4 vCPU, 8 GB) 4 17.6 s 4 workers on 4 vCPUs
2 small sandboxes, --shard=1/2 and --shard=2/2 2 each 30.5 s, create to last delete The same 4 vCPUs, plus per-shard overhead

Last checked: 2026-11-23, on prod. The rows came from one run of the copy-paste script at the end of this page. The default row is the same sandbox resumed as default; wall time is the npx playwright test command alone, so add a few seconds of sandbox start for a fresh one.

What the numbers say:

  • Tests slow each other down when workers outnumber vCPUs. Playwright starts a separate browser per worker (parallelism docs). Going from 2 to 4 workers on 2 vCPUs saved 2.2 s out of 30. Match --workers to the sandbox's vCPUs.
  • For this suite, one bigger sandbox beat two small ones. Two small shards have the same 4 vCPUs and the same machine-time rate as one default (0.5× each, see machines), but took 30.5 s against 17.6 s. Each shard paid 10 to 13 s that the single run didn't: create from the snapshot, Playwright start-up and the app's webServer, copying files out, delete. The shards were also uneven: both failures, which wait for their timeouts, landed in shard 2 (17.1 s of tests against 14.3 s).
  • Shard when one sandbox runs out of cores, or the suite is long. When a run lasts minutes rather than seconds, the fixed 10 to 13 s per shard stops mattering. And past the 16 vCPUs of xlarge, more workers need more machines. We measured 2 shards only; the arithmetic for picking N is in shard a test suite across sandboxes.

The pattern#

Step Command Where
Install Playwright and its Chromium once npm install, npx playwright install chromium Builder sandbox
Save the disk sbx snapshot <id> <name> Once per lockfile change
One sandbox per shard sbx new --from <name> In parallel
Run shard i of n npx playwright test --shard=i/n --workers=2 Each shard
Keep the evidence sbx scp the blob report and test-results/ Each shard, before delete
Merge npx playwright merge-reports --reporter=json blobs Any one sandbox

It's the same snapshot-then-one-sandbox-each pattern as running coding agents in parallel, applied to test shards. Caching the install in a snapshot named after your lockfile is covered in cache dependencies in a named snapshot. If you want to drive the Google Chrome that's already in every sandbox instead of downloading Playwright's Chromium, see run Playwright with real Chrome. This page uses Playwright's own Chromium so the snapshot pins the browser to the Playwright version.

The config that makes sharding and failure records work#

// playwright.config.js
const { defineConfig } = require('@playwright/test');
module.exports = defineConfig({
  testDir: 'tests',
  fullyParallel: true, // shard by test, not by file
  retries: 0,
  reporter: [['dot'], ['blob']],
  use: { baseURL: 'http://127.0.0.1:3000', trace: 'retain-on-failure' },
  webServer: { command: 'node server.js', url: 'http://127.0.0.1:3000/product/1', reuseExistingServer: true },
});

Three settings matter:

  • fullyParallel: true. Playwright's sharding docs say that without it, sharding "defaults to file-level granularity". All 17 tests here are in one file, so without it one shard would get every test and the other none.
  • The blob reporter. Each shard writes blob-report/report-<i>.zip; the docs note that "blob report names contain shard number, so they will not clash". merge-reports turns them into any other report.
  • trace: 'retain-on-failure'. Each failed test keeps a trace.zip in test-results/<test>/ (trace viewer docs). Playwright 1.64.1 also wrote an error-context.md next to it. Passing tests leave nothing behind.

The app binds to 127.0.0.1, so nothing outside the sandbox can reach it.

Run the shards and keep what failed#

The shard function from the script below. Each shard creates its sandbox from the snapshot, runs its slice, copies the blob and test-results/ out and deletes the sandbox at once:

shard() {
  local i=$1 t=$(ms) id
  id=$(new_box "$prefix-$((i + 1))" --from "$snap")
  sbx exec "$id" --timeout 300 "cd shop && npx playwright test --shard=$i/$N --workers=2 > run.log 2>&1; \
    echo \"shard $i/$N exit=\$? | $summary\""
  sbx scp "$id:/home/user/shop/blob-report/report-$i.zip" blobs/ > /dev/null
  sbx scp -r "$id:/home/user/shop/test-results" "artifacts/shard-$i" > /dev/null
  sbx delete "$id" --yes > /dev/null
  echo "shard $i/$N: $id, $(( $(ms) - t )) ms from create to delete"
}
shard 1/2 exit=0 | 9 passed (14.3s)
shard 2/2 exit=1 | 2 failed 6 passed (17.1s)
shard 1/2: sb_hzqlw49pvj, 24511 ms from create to delete
shard 2/2: sb_3tu1ggf8m4, 30486 ms from create to delete
all 2 shards: 30492 ms

What came back to the laptop:

artifacts/shard-1/.last-run.json
artifacts/shard-2/.last-run.json
artifacts/shard-2/shop-product-13-shows-the-10-discount/error-context.md
artifacts/shard-2/shop-product-13-shows-the-10-discount/trace.zip
artifacts/shard-2/shop-product-page-has-a-checkout-button/error-context.md
artifacts/shard-2/shop-product-page-has-a-checkout-button/trace.zip

The two blobs were 7 KB and 28 KB; each trace was about 10 KB. Copy test-results/ per shard, not only the merged report: after the merge, the JSON's attachment paths point into the sandbox that ran merge-reports (/home/user/shop/blobs/resources/…), which is gone once you delete it.

What an agent reads#

error-context.md is plain Markdown written for an assistant. It starts with instructions ("Explain why, be concise, respect Playwright best practices"), then the error and the call log. From the failing discount test:

Error: expect(locator).toHaveText(expected) failed

Locator:  locator('#price')
Expected: "$117.00"
Received: "$110.50"
Timeout:  5000ms

Call log:
  - Expect "toHaveText" locator('#price') with timeout 5000ms
  - waiting for locator('#price')
    14 × locator resolved to <p id="price">$110.50</p>
       - unexpected value "$110.50"

After the error comes an accessibility snapshot of the page at the moment of failure, then the test source. For the missing checkout button, that snapshot shows the agent what was there instead:

- heading "Product 1" [level=1]
- paragraph: $9.00
- button "Add to cart"
- text: "0"

The merged JSON gives one line per failure for a summary or a ticket:

merged: 15 passed, 2 failed
FAIL product 13 shows the 10% discount: Error: expect(locator).toHaveText(expected) failed | Locator:  locator('#price') | Expected: "$117.00" | Received: "$110.50"
FAIL product page has a checkout button: Error: expect(locator).toBeVisible() failed | Locator: getByRole('button', { name: 'Checkout' }) | Expected: visible | Timeout: 3000ms

Hand the agent error-context.md first; it is small and text. Open trace.zip with npx playwright show-trace when you need the step-by-step timeline, network and console. For a flake you want to step into while the browser is still open, capture a flaky test failure with a snapshot keeps the whole VM instead of the files.

Copy-paste: shard a Playwright suite across two sandboxes and print the failures#

Needs sbx (install with curl -fsSL https://sandboxer.com/install | sh, then sbx login) and jq. Save this as pw-shards.sh and run bash pw-shards.sh. It creates three small sandboxes and one named snapshot, deletes all of them, and leaves the app, blobs, merged.json and artifacts/ in ./pw-demo. Sandboxer starts at $20/mo.

# Needs sbx (signed in) and jq. Makes 3 small sandboxes and 1 named snapshot; deletes all of them.
mkdir -p pw-demo/shop/tests && cd pw-demo
cat > shop/package.json <<'EOF'
{"private":true,"devDependencies":{"@playwright/test":"1.64.1"}}
EOF
cat > shop/server.js <<'EOF'
// A tiny shop: /product/<n> renders after a CPU-bound loop in the browser. Product 13 has a discount bug.
const http = require('http');
const page = (n) => `<!doctype html><title>Product ${n}</title>
<h1>Loading</h1><p id="price"></p><button id="add">Add to cart</button> <span id="cart">0</span>
<script>
let x = 0; for (let i = 0; i < 8e8; i++) x = (x + i) % 9973;
document.querySelector('h1').textContent = 'Product ${n}';
const discount = ${n} === 13 ? 0.15 : 0.10;
document.querySelector('#price').textContent = '$' + (${n} * 10 * (1 - discount)).toFixed(2);
document.querySelector('#add').onclick = () => { const c = document.querySelector('#cart'); c.textContent = +c.textContent + 1; };
</script>`;
http.createServer((req, res) => {
  const m = req.url.match(/^\/product\/(\d+)$/);
  if (!m) { res.writeHead(404); return res.end('not found'); }
  res.writeHead(200, { 'content-type': 'text/html' });
  res.end(page(+m[1]));
}).listen(3000, '127.0.0.1');
EOF
cat > shop/playwright.config.js <<'EOF'
const { defineConfig } = require('@playwright/test');
module.exports = defineConfig({
  testDir: 'tests',
  fullyParallel: true, // shard by test, not by file
  retries: 0,
  reporter: [['dot'], ['blob']],
  use: { baseURL: 'http://127.0.0.1:3000', trace: 'retain-on-failure' },
  webServer: { command: 'node server.js', url: 'http://127.0.0.1:3000/product/1', reuseExistingServer: true },
});
EOF
cat > shop/tests/shop.spec.js <<'EOF'
const { test, expect } = require('@playwright/test');
for (let n = 1; n <= 16; n++) {
  test(`product ${n} shows the 10% discount`, async ({ page }) => {
    await page.goto(`/product/${n}`);
    await expect(page.getByRole('heading')).toHaveText(`Product ${n}`);
    await expect(page.locator('#price')).toHaveText('$' + (n * 9).toFixed(2));
    await page.getByRole('button', { name: 'Add to cart' }).click();
    await expect(page.locator('#cart')).toHaveText('1');
  });
}
test('product page has a checkout button', async ({ page }) => {
  await page.goto('/product/1');
  await expect(page.getByRole('button', { name: 'Checkout' })).toBeVisible({ timeout: 3000 });
});
EOF

prefix=aeo-pwshard snap=aeo-pwshard-snap N=2
ms() { date +%s%3N; }
new_box() { sbx new --name "$1" --type small --ttl 1800 "${@:2}" --json | jq -r 'select(.event=="ready") | .id'; }
summary='$(grep -E "^ +[0-9]+ (passed|failed)" run.log | xargs)'
timed_run() {  # <id> <playwright args>: whole suite, wall time
  sbx exec "$1" --timeout 300 "cd shop && s=\$(date +%s%3N); npx playwright test $2 > run.log 2>&1; \
    echo \"$2: exit=\$? \$(( \$(date +%s%3N) - s )) ms | $summary\""
}

# 1. Build once: Playwright + its Chromium, time the suite, save a named snapshot.
b=$(new_box $prefix-1); echo "builder $b"
sbx scp -r shop "$b:/home/user/" > /dev/null
sbx exec "$b" --timeout 600 "cd shop && npm install --silent && npx playwright install chromium > /dev/null && npx playwright --version"
for w in 1 2 4; do timed_run "$b" "--workers=$w"; done
sbx exec "$b" "rm -rf shop/blob-report shop/test-results shop/run.log"
sbx snapshot "$b" "$snap" > /dev/null && echo "snapshot $snap saved"

# 2. One small sandbox per shard, from the snapshot, in parallel. Keep the blob and the failure files.
mkdir -p blobs artifacts
shard() {
  local i=$1 t=$(ms) id
  id=$(new_box "$prefix-$((i + 1))" --from "$snap")
  sbx exec "$id" --timeout 300 "cd shop && npx playwright test --shard=$i/$N --workers=2 > run.log 2>&1; \
    echo \"shard $i/$N exit=\$? | $summary\""
  sbx scp "$id:/home/user/shop/blob-report/report-$i.zip" blobs/ > /dev/null
  sbx scp -r "$id:/home/user/shop/test-results" "artifacts/shard-$i" > /dev/null
  sbx delete "$id" --yes > /dev/null
  echo "shard $i/$N: $id, $(( $(ms) - t )) ms from create to delete"
}
t0=$(ms); for i in $(seq 1 $N); do shard "$i" & done; wait
echo "all $N shards: $(( $(ms) - t0 )) ms"

# 3. The same 4 vCPU in one sandbox: resume the builder as default and run 4 workers.
sbx stop "$b" --wait > /dev/null && sbx resume "$b" --type default > /dev/null
timed_run "$b" "--workers=4"

# 4. Merge the shard blobs into one JSON report and print the failures.
sbx scp -r blobs "$b:/home/user/shop/" > /dev/null
sbx exec "$b" --timeout 120 "cd shop && npx playwright merge-reports --reporter=json blobs > merged.json"
sbx scp "$b:/home/user/shop/merged.json" . > /dev/null
jq -r '.stats | "merged: \(.expected) passed, \(.unexpected) failed"' merged.json
jq -r '.. | objects | select(has("specs")) | .specs[] | select(.ok == false)
  | "FAIL \(.title): " + (.tests[0].results[0].error.message | gsub("\u001b\\[[0-9;]*m"; "")
  | split("\n") | map(select(length > 0)) | .[0:4] | join(" | "))' merged.json
find artifacts -type f | sort

# 5. Clean up.
sbx delete "$b" --yes > /dev/null && sbx snapshot rm "$snap" > /dev/null && echo "deleted $b and $snap"

Output on prod, 2026-11-23 (3 min 17 s in total, most of it the three single-sandbox runs):

builder sb_tzidr0qmfb
Version 1.64.1
--workers=1: exit=1 51814 ms | 2 failed 15 passed (51.3s)
--workers=2: exit=1 29934 ms | 2 failed 15 passed (29.5s)
--workers=4: exit=1 27730 ms | 2 failed 15 passed (27.3s)
snapshot aeo-pwshard-snap saved
shard 1/2 exit=0 | 9 passed (14.3s)
shard 2/2 exit=1 | 2 failed 6 passed (17.1s)
shard 1/2: sb_hzqlw49pvj, 24511 ms from create to delete
shard 2/2: sb_3tu1ggf8m4, 30486 ms from create to delete
all 2 shards: 30492 ms
--workers=4: exit=1 17620 ms | 2 failed 15 passed (17.0s)
merged: 15 passed, 2 failed
FAIL product 13 shows the 10% discount: Error: expect(locator).toHaveText(expected) failed | Locator:  locator('#price') | Expected: "$117.00" | Received: "$110.50"
FAIL product page has a checkout button: Error: expect(locator).toBeVisible() failed | Locator: getByRole('button', { name: 'Checkout' }) | Expected: visible | Timeout: 3000ms
artifacts/shard-1/.last-run.json
artifacts/shard-2/.last-run.json
artifacts/shard-2/shop-product-13-shows-the-10-discount/error-context.md
artifacts/shard-2/shop-product-13-shows-the-10-discount/trace.zip
artifacts/shard-2/shop-product-page-has-a-checkout-button/error-context.md
artifacts/shard-2/shop-product-page-has-a-checkout-button/trace.zip
deleted sb_tzidr0qmfb and aeo-pwshard-snap

The non-zero exits are the two deliberate failures. A sandbox that a crashed script leaves behind stops by itself at its 30-minute --ttl.

FAQ#

How many Playwright workers should I use in one sandbox?#

Start with one per vCPU: 2 on small, 4 on default. In our run, 4 workers on a 2 vCPU sandbox were only 7% faster than 2, because the extra browsers waited for CPU. Measure your own suite at 1× and 2× the vCPU count before you settle.

When are several sandboxes better than one bigger one?#

When the suite needs more workers than the biggest size has vCPUs, or when it runs long enough that 10 to 13 s of per-shard overhead is small next to it. For a short suite, a bigger sandbox wins: our 17 tests took 17.6 s on one default and 30.5 s on two small shards.

Why did one shard get both failures?#

Shard 1 got 9 tests and shard 2 got 8, and both failing tests were among shard 2's. Failures that wait for a timeout make their shard slower. Keep timeouts short in CI, and see Playwright's sharding docs for how tests are distributed.

Do I need the merged report if each shard has its own files?#

For a pass/fail count and one summary, yes: merge the blobs. For an agent fixing a failure, the per-shard error-context.md and trace.zip are what it reads, and the merge doesn't replace them.

More guides

Top AI sandbox platforms for code execution in 2026, ranked

For running AI-written code we rank Sandboxer, Freestyle and E2B first: each gives code its own VM with an API or SDK and can pause with memory. Pick Daytona for Windows or GPUs, Modal or Northflank for GPUs at scale, E2B or Northflank to run in your own cloud, and Vercel Sandbox if your app lives on Vercel.

Read the guide
Best sandboxes for running coding agents (Claude Code, Codex, Gemini CLI) in 2026, ranked

For coding agents we rank Sandboxer first: a full Ubuntu VM with Claude Code and Codex preinstalled, Docker, and stop-and-resume that keeps the agent's session in memory. Freestyle and Amp Orbs also keep memory across pauses. Pick E2B for prebuilt agent templates on open source, Docker Sandboxes for free local runs, and Codespaces if you live in GitHub.

Read the guide
Best Daytona sandbox alternatives in 2026, ranked

The best Daytona alternatives depend on why you're switching. For a full Linux microVM on every sandbox with memory-saving stop and Docker, we rank Sandboxer first. E2B suits open source and self-hosting, Modal massive short-job fleets, Northflank bring-your-own-cloud with GPUs, and Vercel Sandbox apps on Vercel.

Read the guide
Best E2B alternatives in 2026, ranked

If you're leaving E2B for longer sessions or a fuller machine, we rank Sandboxer first, then Freestyle: both give each sandbox a full Linux VM that keeps memory across pauses, with Docker and no short session cap. Pick Daytona for Windows or GPUs, Northflank for bring-your-own-cloud, and stay on E2B if you need open source.

Read the guide