Context
The suite had about 130 Playwright tests in TypeScript, on the critical paths of a PHP web app: real pages, hard to test any other way and slow to test by hand. A run took 36 minutes end to end, including building the image the tests run in, and nothing ran in parallel. The refactor took about 3 days to implement and about a week to finish, while features kept shipping.
Problem
Seeding was the core problem. There were three ways to seed, with no convention: initial database data; raw SQL in tests, so tests knew the schema; and creating data through the UI, fine for some cases and bad for others. Run in parallel, tests got in each other's way. Only about 90 of the tests ran; the rest were skipped because they were too hard to maintain. A few tests were flaky, some parts were slow, and the structure made new tests hard to add. About 20–25 % of pipeline runs had to be re-run, so even a small fix meant waiting again. An earlier cleanup by hand moved only 2 or 3 tests.
Why AI
Many changes were needed, the target structure was clear with only small gaps, and moving tests by hand had already proved slow. The agent rewrote each test into the new structure, keeping exactly its steps and assertions. It wrote seed data through the seeding API, which meant understanding what each test needed. It fixed flaky tests from their selectors and failures. It wrote new tests with a dedicated skill on how tests work in this project. Iterating was safe because the test-only seeding endpoints have parity tests that keep them from drifting from the real endpoints.
Where the script won
Scripts renamed selectors and added classes at specific places in the app. The expected test structure was first part of the agent's instructions. It moved into lint rules (ESLint) and architecture tests run by hooks, so the agent no longer had to think about it. Those checks are general, not test-specific, but they keep the structure. The agent still made roughly 90 % of the changes, though that split is hard to say.
Orchestration
I used Claude Code with Claude Opus. I did one module first. Once it worked and the structure was right, I repeated the same approach per module. A main session held the overall picture and gave each agent enough context for one module, so no agent read everything. Each agent got rules on how to work and where not to drift, the structure of the seeding, and rules on what the tests test. Agents ran in parallel through a skill that gave each its own worktree and database. A progress file recorded each step and each decision. The loop per test: read the old test, rewrite it, run it, run it again for flakiness, commit. A human checked that the same things were tested.
Orchestration, top to bottom: a main session that knows the whole suite gives one agent per module enough context. The agents run in parallel, each in its own worktree with its own database. Each agent reads the old test and rewrites it with the same steps and assertions, seeding its data through the API, with lint rules and architecture tests run by hooks. It runs the test, then runs it again to catch flakiness; a flaky test goes back to the rewrite. A stable test is committed and the step is recorded in the progress file. A human checks that the same things are tested as before.
Trace replayPort one test in module A and catch a flaky neighbour
Budget line at 100% (250,000 tokens)
Step 1 of 15: HARNESS. The main session hands over one test.
The main session hands over one test.
The main session knows the whole suite. It gives this agent one unit in module A: a stub test whose old version relied on a fully seeded database. The brief names the old test, the seeding helpers and the conventions to follow.
Why it mattersEnough context for one unit, not the whole suite: no agent has to read everything.
Event log
- Tool calls
- 0
- Failures caught
- 0
- Tokens reclaimed
- 0
- Human gates
- 0
Guardrails
- The agent could change only test code and the test-only seeding endpoints. The classes added to the app came from scripts, not the agent, and were reviewed like the agent's changes.
- Architecture tests forbid skipped tests.
- Lint rules enforce the Playwright conventions: where await goes, how to select elements, when to wait.
- Hooks run the lint rules and architecture tests, so the agent gets the failure itself.
- Parity tests keep the test-only seeding endpoints in line with the real endpoints.
- Each agent works in its own worktree and database, so parallel agents cannot collide.
- A human reviewed every change: no production code modified, no behaviour changed, and each new test does the same steps and assertions as the old one.
- No token, time or cost caps.
Evals
Method. Several runs of the suite, locally and in the pipeline. The run time. Tests run versus skipped. A human check that each rewritten test kept the old test's steps and assertions.
Results. Before, about 90 tests ran; after, about 130, none skipped. The run went from 36 to 12–14 minutes, mostly from running in parallel, which works because each test seeds its own data. Coverage is slightly higher, since nothing is skipped. Flakiness is still being worked on. The new suite caught a few real bugs, mostly in report validation under different feature configurations. Each was fixed in its own ticket, with its own end-to-end test. No bad tests were found. Some tests were too long and were split to fit the Playwright conventions.
Outcome
All done: every test is in the new structure. The team still writes new end-to-end tests with the same structure, skill and harness. Changing the direction and structure of a suite became much cheaper. End-to-end tests used to be low priority; with agents, that part can grow much more. Both the cost of changing the suite and the cost of maintaining it went down.
What I'd change
Put more into scripts, so more is checked automatically. Split long tests while moving them. Use a checklist per test instead of comparing old and new.
Notes
In CI the tests run against a database with no specific data. Each test seeds its own data first, so tests run fully independently.
The app is the same one as in Two frameworks, one app, but this was separate work.