Skip to contentPlayForge

Can AI write Playwright tests for me?

Written by Jatin Thacker, Automation Architect · Updated

Yes, AI tools can draft Playwright tests quickly, and Playwright can set up planner, generator and healer agents for your AI tool to do exactly that. What they cannot do is tell you whether a test can fail, so every generated test still needs a person to review it and run it.

This answer is one piece of Chapter 31: Reviewing AI-Written Tests.

  • Why Agent-Written Tests Pass Review
  • The Assertion That Isn't One
  • Tests That Pass for the Wrong Reason
  • Setup That Silently Does Nothing
  • Reading a Diff You Didn't Write
  • Accept, Fix, or Throw Away

Playwright's docs describe three test agents: a planner that "explores the app and produces a Markdown test plan", a generator that turns the plan into test files, and a healer that runs the suite and repairs failing tests. General-purpose AI coding tools will also write a test from a description.

AI drafts tend to get the shape right: a test() block, sensible locators, an await on each line. The defect that slips through is usually small and reads as correct. A common one is isVisible() with the result thrown away. It returns a boolean straight away and checks nothing, so the line passes whatever the page shows.

The example shows that pattern next to the fix, on a catalogue page that has no heading called Dashboard. The draft passes. The reviewed version, await expect(...).toBeVisible(), fails with element(s) not found, which is the correct result.

So use AI for the typing, and keep the judgement: what to test, whether the assertion can fail, and whether the test is worth keeping.

ai-draft.spec.js(https://practice.learnplayforge.com)
import { test, expect } from '@playwright/test'; // The catalog page has no heading called 'Dashboard'. test('AI draft: discards the boolean', async ({ page }) => { await page.goto('/catalog.html?mock=static&seed=42&autologin=1'); await page.getByRole('heading', { name: 'Dashboard' }).isVisible(); }); test('reviewed: asserts with expect', async ({ page }) => { await page.goto('/catalog.html?mock=static&seed=42&autologin=1'); await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible(); });
Output
  1) › reviewed: asserts with expect
    Error: expect(locator).toBeVisible() failed
    Expected: visible
    Error: element(s) not found

  1 failed
  1 passed (7.2s)
Test yourself
An AI draft ends with await page.getByRole('heading', { name: 'Dashboard' }).isVisible();. The heading is missing from the page. What happens?

Practised in the full course: the drill A read is not a check.

See the chapter

AI can write the test now. The job is knowing whether it checks anything. That's what the course teaches, one chapter at a time.

163 lessons · 226 drills · start free

← All questions

Training a team? Talk to us.