Can AI write Playwright tests for me?
Written by Jatin Thacker, Automation Architect · Updated
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.
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();
}); 1) › reviewed: asserts with expect
Error: expect(locator).toBeVisible() failed
Expected: visible
Error: element(s) not found
1 failed
1 passed (7.2s)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 chapterRelated questions
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