ankit.pandey
Back to SDET Engineering Journal
AGENT-04EXPERIMENTAL•11 min read•Advanced

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.

#Test Pyramid#API Testing#Contract Testing#Architecture Strategy

01. The Problem

Teams suffer from the 'inverted testing pyramid' (ice-cream cone anti-pattern), writing hundreds of slow, flaky UI tests for validations that could be executed 100x faster and cheaper at the API or unit layer.

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?

Agent Role Definition:

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

Step 1: Pure Calculation Layer — Assigns exchange rate mathematical formulas and roundoff rules to Jest unit tests (130 tests).
Step 2: Boundary & Auth Layer — Assigns HTTP 400 parameter validations, token expirations, and negative paths to Supertest API tests (277 tests).
Step 3: Critical Journey Layer — Assigns only the top 3 customer revenue-generating journeys to Playwright browser E2E (49 tests).
Step 4: Layer Balance Calculation — Computes expected suite execution time and flakiness risk score.

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

Lesson 01

The best Playwright test is the one you don't have to write because the API layer proved the invariant.

Lesson 02

Use Playwright for confidence in user interaction and browser rendering; use API tests for exhaustive combinatorial validation.

Lesson 03

Preconditioning tests via direct database or API calls is the single biggest factor in fast, deterministic automation.