AI API + UI Test Strategy Agent: Balancing the Pyramid to Prevent UI Bloat
A multi-agent decision engine that pushes test coverage down to HTTP contracts and unit tests, keeping E2E suites lean.
01. The Problem
02. The Concrete Engineering Scenario
WrightPay adds support for 12 new currency pairs with complex fee tiers, minimum payout amounts, and regulatory sanctions checks. The team wonders: should we write 48 UI tests or split them across layers?
Acts as a Quality Engineering Strategist that evaluates proposed feature requirements against execution cost, speed, and failure localization, outputting an optimal distribution across Unit, API, and E2E tiers.
03. Context & Inputs Provided to Agent
- •Feature specifications for multi-currency routing rules.
- •Current test execution telemetry (Unit: 1.7s, API: 8s, E2E: 35s).
- •System architecture diagram showing NestJS services and database schema.
04. Agent Reasoning & Planning Steps
Playwright Interaction
Defines the boundary of what Playwright should NOT test: bypassing UI login by injecting pre-computed JWT tokens directly into localStorage for downstream E2E specs.
Evidence Collection
Generates a comparative execution cost table: 48 UI tests = 4.2 minutes runtime; Hybrid Pyramid (35 Unit, 10 API, 3 UI) = 14 seconds runtime.
05. Concrete Code Excerpt
Fast API Preconditioning in Playwright Spec// Precondition via API in 45ms instead of 4,000ms UI clicks
test('Transfer history reflects newly completed transfer', async ({ page, request }) => {
// 1. Direct API call seeds database and executes transfer instantly
const transfer = await request.post('/api/v1/transfers', {
headers: { Authorization: `Bearer ${token}` },
data: { beneficiaryId, sourceAmount: 100, sourceCurrency: 'EUR' }
});
// 2. Browser only validates that the UI renders the seeded ledger record correctly
await page.goto('/dashboard/transactions');
await expect(page.getByText(transfer.reference)).toBeVisible();
});Failure & Edge Case Handling
Flags any pull request attempting to test form validation errors via browser clicking when an API schema validation test already covers the DTO.
Measurable Result
Reduces planned test suite runtime by 82% while preserving 100% boundary and risk coverage.
Human Review & Approval Gate
Engineering Lead adopts the strategy, enforcing the rule in CI that PRs must add API contract specs before UI specs.
Key Engineering Lessons Learned
The best Playwright test is the one you don't have to write because the API layer proved the invariant.
Use Playwright for confidence in user interaction and browser rendering; use API tests for exhaustive combinatorial validation.
Preconditioning tests via direct database or API calls is the single biggest factor in fast, deterministic automation.