QA AND TEST AUTOMATION · PART 5 · UI WEB AUTOMATION · SCALE · 57 OF 112

Parallel and Cross-Browser Testing, and Selenium Grid

Running UI suites faster and wider: Playwright workers, fullyParallel and sharding across CI machines, the shared-data trap shown for real on ShopRung (one shared user and 4 workers: 6 of 6 failed with counts of 4 and 6; a fresh user per test: 6 passed), browser and device projects, what cross-browser testing should cover, and Selenium Grid 4 with RemoteWebDriver and Docker.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Workers and fullyParallel
  • isolate data first
  • shards + merged reports
  • browser projects
  • Selenium Grid

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.

Parallel in Playwright
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.

One shared user, 4 workers
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.

Isolated data
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.

Shards
# 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.

Browser projects
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.

A sensible split
  • 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.

Grid 4
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.

Runner settings
# 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1How does Playwright run tests in parallel?

In worker processes, each with its own browser; files run in parallel by default, and fullyParallel lets individual tests run in parallel too.

Q2What is sharding?

Splitting a suite across machines (--shard=1/4 and so on), then merging blob reports into one HTML report.

Q3Why do tests fail only when run in parallel?

Shared mutable state: the same user, cart or records, global resets, or order dependencies between tests.

Q4How do you isolate test data for parallel runs?

Create unique data per test (for example a new user via the API, with unique emails), avoid global resets and clean up only what the test created.

Q5What is Selenium Grid?

Infrastructure that runs Selenium sessions on remote browsers and machines; tests connect with RemoteWebDriver to the grid URL.

Q6How do you run Selenium tests in parallel safely?

Enable parallelism in the runner (JUnit 5, TestNG, pytest-xdist) and give each thread its own driver, never a static shared one.

Q7How do you choose browsers for cross-browser testing?

From real user analytics and product requirements, balancing coverage against run time with per-commit, per-PR and nightly tiers.

Q8Is mobile emulation enough for mobile testing?

It covers responsive layout and touch basics, but not real device performance, OS behaviour or native apps; real devices are still needed.