Conclusion First
I tried two different paths for Playwright UI automation:
- MCP-driven AI execution
- Playwright Codegen recording
MCP was powerful but too slow for my day-to-day case execution, so I moved to Codegen for UI scenarios. That gave me a much faster path to stable UI tests.
For test data preparation, I realized UI-only setup is inefficient, and API-based setup is the right direction. I am still refining that part, but this post documents a practical setup you can start using today.
Why I Changed My Approach
My original idea was: “Let AI execute test cases for me through MCP.”
That worked functionally, but execution speed and iteration time became the bottleneck. For quick trial-and-error on selectors and user flows, I needed something faster.
So I switched to Codegen:
- record actions,
- generate Playwright code in real time,
- and quickly convert that into maintainable test cases.
Result: UI automation moved forward much faster.
Environment
- Node.js 18+
- Playwright (TypeScript)
- VS Code
- Optional MCP client/server integration for AI-driven browser control
1) MCP Setup for AI-Driven Execution
What MCP is useful for
MCP is good when you want AI to:
- explore pages,
- run scenario-like steps,
- assist investigation and debugging.
Basic setup idea
Depending on your MCP client, configuration style differs, but conceptually you register a Playwright-capable MCP server and allow browser actions.
Example (conceptual):
| |
Why I did not use MCP as the main path
- Great flexibility, but slower feedback loop for repetitive case authoring.
- For frequent small edits (selector fix, assertion tuning), Codegen + direct coding felt much faster.
2) Codegen Setup for Fast UI Test Authoring
Project initialization
| |
Choose:
- TypeScript
- Playwright test directory (for example
tests/) - Browsers you need
Launch Codegen
| |
Useful options:
| |
Recommended workflow with Codegen
- Record one clean happy-path flow.
- Move generated code into page-object or helper functions.
- Replace brittle selectors with stable locators (
getByRole,getByTestId). - Add clear assertions and wait strategy.
- Re-run in headed and headless mode.
Example command:
| |
3) API Test Setup for Fast Test Data Preparation
UI setup for preconditions is usually slow. API setup is faster and more deterministic.
Base API test structure
Create tests/api/data-setup.spec.ts:
| |
Use API setup before UI tests
Pattern A: run API suite first in CI.
| |
Pattern B: call API setup in globalSetup.
playwright.config.ts:
| |
tests/setup/global-setup.ts:
| |
Cleanup strategy (important)
Always add teardown to remove test data. If not, data pollution will break reproducibility.
4) Copy-Paste Setup Blocks (Directly in This Blog)
Below are the exact files I use as a starting point.
.env.example
| |
package.json scripts (test:ui, test:api, test:all)
| |
playwright.config.ts (with setup + teardown)
| |
tests/setup/global-teardown.ts (auto cleanup)
| |
Command usage
| |
5) Suggested Folder Structure
| |
6) Pros and Cons from My Experience
MCP
Pros:
- Flexible, exploratory, and helpful for AI-assisted investigation.
- Good for ad-hoc scenario execution.
Cons:
- Too slow for high-frequency test authoring loops.
- Harder to standardize as the primary path for large regression packs.
Codegen
Pros:
- Very fast for building initial UI scripts.
- Great for selector discovery and flow capture.
- Easy to iterate while watching the browser.
Cons:
- Raw generated code needs refactoring.
- Without cleanup, tests can become brittle.
API Test for Data Setup
Pros:
- Much faster than preparing data via UI.
- More deterministic and CI-friendly.
- Reduces UI test runtime significantly.
Cons:
- Requires understanding of service contracts and auth.
- Needs robust cleanup to avoid dirty test environments.
7) My Current Practical Strategy
I now use a hybrid strategy:
- Use MCP for investigation and AI-assisted exploration.
- Use Codegen for fast UI script creation.
- Use API tests to prepare preconditions and test data.
- Keep UI tests focused on user-visible behavior only.
This gives the best balance between speed and reliability.
Final Takeaway
If your Playwright automation feels slow, separate concerns:
- exploration (MCP),
- UI flow authoring (Codegen),
- data preparation (API test).
For me, this separation was the turning point from “automation is possible” to “automation is practical.”