ankit.pandey
Back to SDET Engineering Journal
AGENT-02EXPERIMENTAL•10 min read•Intermediate

AI Playwright Test Generator Agent: Synthesizing Robust Page Objects & Resilient Locators

Generating reliable Playwright automation using accessibility locators, explicit network assertions, and zero arbitrary timeouts.

#Code Generation#Page Objects#Accessibility Locators#Playwright Automation

01. The Problem

Naive AI code generators spit out fragile scripts full of page.waitForTimeout(5000) and brittle CSS paths like div > div:nth-child(3) > button, creating high test flakiness and maintenance debt.

02. The Concrete Engineering Scenario

An SDET needs to automate the WrightPay Send Money wizard (Recipient selection -> Amount & currency conversion -> Fee confirmation -> Payout dispatch). The agent must produce a TypeScript Page Object Model adhering to best practices.

Agent Role Definition:

Acts as an Automation Engineer agent that takes HTML DOM snapshots and test blueprints, generating clean Page Object classes with role-based locators (getByRole, getByLabel) and web-first assertions.

03. Context & Inputs Provided to Agent

  • •Cleaned DOM snapshot of Send Money page (accessible landmarks, buttons, forms).
  • •Network endpoints trace (/api/v1/exchange-rates, /api/v1/transfers).
  • •Test blueprint specifying valid inputs and expected UI state changes.

04. Agent Reasoning & Planning Steps

Step 1: DOM Semantic Analysis — Scans for ARIA roles, input labels, and data-testid attributes, preferring getByRole('button', { name: 'Continue' }) over CSS selectors.
Step 2: Action Encapsulation — Groups interactions into atomic Page Object methods (selectBeneficiary, enterAmount, confirmTransfer).
Step 3: Network Synchronization — Pairs UI button clicks with explicit page.waitForResponse() to eliminate timing race conditions.
Step 4: Output Assembly — Generates TypeScript file with full type safety.

Playwright Interaction

Emits clean Playwright TypeScript code utilizing auto-waiting assertions (expect(locator).toBeVisible()) and request fixtures for backend preconditioning.

Evidence Collection

Validates the generated code by running Playwright in dry-run syntax validation mode to ensure TypeScript compilation without syntax errors.

05. Concrete Code Excerpt

Synthesized Page Object Class (SendMoneyPage.ts)
import { Page, Locator, expect } from '@playwright/test';

export class SendMoneyPage {
  readonly recipientSelect: Locator;
  readonly amountInput: Locator;
  readonly submitButton: Locator;
  readonly exchangeRateQuote: Locator;

  constructor(private readonly page: Page) {
    this.recipientSelect = page.getByRole('combobox', { name: /select beneficiary/i });
    this.amountInput = page.getByLabel(/amount to send/i);
    this.submitButton = page.getByRole('button', { name: /confirm & transfer/i });
    this.exchangeRateQuote = page.locator('[data-testid="live-fx-rate"]');
  }

  async executeTransfer(beneficiaryName: string, amount: number) {
    await this.recipientSelect.selectOption({ label: beneficiaryName });
    await this.amountInput.fill(amount.toString());
    await expect(this.exchangeRateQuote).toBeVisible();

    const [response] = await Promise.all([
      this.page.waitForResponse(res => res.url().includes('/api/v1/transfers') && res.status() === 201),
      this.submitButton.click(),
    ]);
    return response.json();
  }
}

Failure & Edge Case Handling

When encountering unlabelled controls, the agent flags an accessibility warning: 'Input field at line 42 lacks accessible label; fell back to data-testid. Recommend filing a11y defect.'

Measurable Result

Generates a fully typed SendMoneyPage.ts class and an accompanying test file in 30 seconds that passes linting and strict TypeScript compilation.

Human Review & Approval Gate

The SDET reviews locator resilience, confirms that setup fixtures use API seeding rather than UI clicking to accelerate test execution, and commits the class.

Key Engineering Lessons Learned

Lesson 01

Explicitly instructing the agent to forbid page.waitForTimeout() enforces proper web-first assertions.

Lesson 02

Providing the agent with a sample golden Page Object as few-shot context produces 10x higher code consistency.

Lesson 03

Combining UI actions with API response promises eliminates 95% of asynchronous flakiness.