# How to create an automated test with AI, step by step

> AI writes the skeleton, the Playwright recorder supplies the real selectors, and you keep the judgment: a checkout test in about twenty minutes.

Source: https://qanary.dev/en/blog/create-an-automated-test-with-ai  
Product: Qanary, no-code automated testing for e-commerce  
Site index for AI assistants: https://qanary.dev/en/llms.txt

[Blog](https://qanary.dev/en/blog) · Guide

AI writes the skeleton, the Playwright recorder supplies the real selectors, and you keep the judgment: a checkout test in about twenty minutes.

[Antoine Bouchet](https://qanary.dev/en/about#antoine), CEO of Qanary, January 26, 2026 · updated November 3, 2026 · 5 min read

An AI assistant (ChatGPT, Claude, Gemini) writes the skeleton of a Playwright test in two minutes. The Playwright recorder then swaps its made-up selectors for the ones on your site, the AI makes the code more robust, you debug it, and the AI suggests the error cases. Plan on about twenty minutes for a full checkout flow, compared with one to two hours by hand in our experience. AI writes fast; you are the one who knows what deserves a test.

## In short

- AI is good at the structure of a test, but it invents the selectors: those come from your site, not from the AI.
- npx playwright codegen records the flow you click through by hand and gives you the real selectors.
- Work in small requests: one test that works, then improvements, then variants.
- Review every line: AI sometimes invents functions, selectors, or outdated practices.
- AI doesn’t know what matters to your revenue: choosing the tests is still your job.

## The scenario: a full purchase, from login to confirmation

The test follows a customer’s journey in eight steps: log in, search for a product, open the first result, add it to the cart, go to the cart, fill in shipping, pay by card, and check the confirmation page.

You need Node.js, Playwright (npm init playwright@latest), an AI assistant, a test customer account, and your payment provider’s test mode. Never use a real payment in a test: the card numbers below are Stripe test cards and only work in test mode.

## Five steps, in about twenty minutes

AI writes, Playwright corrects, you decide: five steps, from the skeleton to the error cases.

### 1. Have the AI write the skeleton (2 minutes)

Describe the scenario to the assistant, with the data and the structure you expect:

Write a Playwright test in TypeScript for a full checkout flow. 1. Log in with test@example.com / Test1234! 2. Search for "bird cage" and open the first result 3. Add it to the cart, then go to the cart 4. Fill in shipping: Jane Smith, 12 Mine Street, Springfield 12345 5. Pay by card: 4242 4242 4242 4242, 12/30, 123 6. Check that we land on the confirmation page, with an order number One assertion after each step, comments in English.

Here is an excerpt of what it returns:

import { test, expect } from '@playwright/test'; test('full purchase', async ({ page }) => { await page.goto('https://shop.example'); // Login await page.click('[data-testid="login-button"]'); await page.fill('[name="email"]', 'test@example.com'); await page.fill('[name="password"]', 'Test1234!'); await page.click('button[type="submit"]'); await expect(page.locator('[data-testid="user-menu"]')).toBeVisible(); // Search and add to cart await page.fill('[data-testid="search-input"]', 'bird cage'); await page.press('[data-testid="search-input"]', 'Enter'); await page.locator('[data-testid="product-card"]').first().click(); await page.click('[data-testid="add-to-cart-button"]'); await expect(page.locator('[data-testid="cart-badge"]')).toContainText('1'); // … shipping and payment … // Confirmation await expect(page).toHaveURL(/confirmation/); await expect(page.locator('[data-testid="order-number"]')).toBeVisible(); });

The structure is right: commented steps, an assertion after each one. But every selector is made up. There is no data-testid="login-button" on your site, unless you got lucky.

### 2. Replace the made-up selectors (5 minutes)

The Playwright recorder replaces them. Start it on your site, click through the flow by hand, and it writes the code as you go:

npx playwright codegen https://your-store.com

It produces selectors based on what the customer sees (a role, a label, a text), which hold up better than CSS classes:

await page.getByRole('link', { name: 'Sign in' }).click(); await page.getByLabel('Email').fill('test@example.com'); await page.getByLabel('Password').fill('Test1234!'); await page.getByRole('button', { name: 'Log in' }).click(); await page.getByPlaceholder('Search').fill('bird cage'); await page.getByPlaceholder('Search').press('Enter');

Copy these lines into the AI’s skeleton, step by step. You keep its structure and its assertions, with your site’s selectors.

### 3. Ask for a sturdier version (3 minutes)

Paste the test back into the assistant and ask for specific improvements:

Here is my Playwright test. Improve it: 1. One class per page (Page Object Model) to make it easier to maintain 2. A screenshot and a trace on failure, in the configuration 3. Empty the cart after the test [your code]

The main test becomes readable, almost like a recipe:

test('full purchase', async ({ page }) => { const account = new Account(page); const catalog = new Catalog(page); const checkout = new Checkout(page); await page.goto('/'); await account.logIn('test@example.com', 'Test1234!'); await catalog.addFirstResult('bird cage'); await checkout.ship({ firstName: 'Jane', lastName: 'Smith', address: '12 Mine Street', zip: '12345', city: 'Springfield' }); await checkout.payByCard({ number: '4242424242424242', expiry: '12/30', cvc: '123' }); await expect(page.getByRole('heading', { name: /thank you for your order/i })).toBeVisible(); await expect(page.getByText(/order #/i)).toBeVisible(); });

And the screenshots move into the configuration, instead of living in each test:

// playwright.config.ts (excerpt) use: { baseURL: 'https://your-store.com', screenshot: 'only-on-failure', trace: 'on-first-retry', },

### 4. Run the test and debug it (5 minutes)

Run the test with the browser visible:

npx playwright test checkout.spec.ts --headed

The first run almost always fails. For example:

Error: locator.click: Test timeout of 30000ms exceeded. waiting for getByRole('button', { name: 'Add to cart' })

Open the inspector with --debug (or the UI with --ui): it shows the page at the moment of failure. You can also paste the error and the code into the assistant. On an e-commerce site, the causes are almost always the same:

1. **The label is wrong**: the button says “Add to my cart.” Fix the text, or use a regular expression (/add/i).
2. **The button is disabled**: you have to pick a size or a color first. Add that step to the test.
3. **Something is covering it**: the cookie banner or a newsletter sign-up pop-up. Close them at the start of the test.
4. **The field is inside a frame**: credit card fields are often in an iframe from the payment provider. Playwright reaches them with page.frameLocator(…), which AI often forgets.

The fix takes a few minutes. The test passes.

### 5. Have the AI generate the error cases (5 minutes)

The happy path is covered; what’s left is everything that should fail cleanly. Ask the assistant for five error tests, with the same classes and conventions: wrong password, out-of-stock product, empty cart, declined card, invalid ZIP code. For example:

test('a declined card shows a clear message', async ({ page }) => { // … login, cart, and shipping as in the main test … await checkout.payByCard({ number: '4000000000000002', expiry: '12/30', cvc: '123' }); await expect(page.getByRole('alert')).toContainText(/declined/i); await expect(page).not.toHaveURL(/confirmation/); }); test('an empty cart cannot be checked out', async ({ page }) => { await page.goto('/cart'); await expect(page.getByRole('button', { name: 'Checkout' })).toBeDisabled(); });

After about twenty minutes, you have six tests: the full flow and its error cases. Next, write the ones for the journeys that bring in the most revenue.

## What AI does well, and what stays with you

AI writes repetitive code fast; you know what matters. The split fits in one table:

| AI does well | You do better |
| --- | --- |
| The structure, the imports, the setup | Choosing the journeys that matter for revenue |
| An assertion after every step, without missing one | Finding the odd cases: a customer who adds the same product a thousand times during a stock update |
| Classic error cases: empty field, invalid format | Judging whether a test is worth it: VAT, yes; a typo in a label, rarely |
| Variants of a test that works: other products, other customers | Understanding why a test fails one time in ten |
| Clues about why something fails | Keeping the whole suite consistent: naming, architecture, strategy |

## Five rules for using it well

1. **Work in small requests.** One test that works, then improvements, then variants. “Write all my tests” gives nothing good.
2. **Review every line.** Is this selector robust? Is this assertion useful? Does this test add anything?
3. **Give examples.** Paste two or three of your existing tests before the request: the AI picks up your conventions.
4. **Don’t keep what you don’t understand.** Ask for an explanation, or simplify.
5. **Check everything.** AI sometimes invents functions that don’t exist, selectors, or practices Playwright now advises against.

## The limits of this method

The resulting test is still code. You need someone who can read it, run it, and fix it, and it will need fixing often: every page redesign and every renamed button breaks selectors. To schedule runs, keep the videos, and alert the team, you still need a CI server and some plumbing. AI can also repair a broken test: that’s [self-healing](https://qanary.dev/en/blog/self-healing-tests-with-ai), which has limits of its own.

## The same test, without code, with Qanary

Qanary, no-code automated testing for e-commerce, creates this flow without a single line of code: you record it in a built-in browser, you write each check in one sentence, and Qanary replays it several times a day. When a fragile selector changes, self-healing repairs the test; at the first bug, you get the video and the explanation. For the details, see [functional testing](https://qanary.dev/en/use-cases/functional-testing).

Want to see Qanary test your site? Book your free 30-minute test run with Julia or Antoine.

**Antoine Bouchet** Ten years in product management, on high-impact sites. He wants to raise the quality of online stores, with a product that does exactly what its users need. [More about Antoine](https://qanary.dev/en/about#antoine)

## Read next

- Guide
  ### [Build a testing agent with Claude Code and Playwright](https://qanary.dev/en/blog/testing-agent-claude-code-playwright)
  One folder, one command: Claude explores a site, plans its tests, runs them with Playwright, and delivers a report backed by proof. 15 minutes to set up.
  **Antoine Bouchet**, June 2, 2026 · 7 min read
- Talk
  ### [Self-healing: how AI repairs a broken test](https://qanary.dev/en/blog/self-healing-tests-with-ai)
  Self-healing repairs a test when the site changes. Three approaches with AI, from the most guided to the most autonomous, and their limits (ParisTestConf 2026).
  **Antoine Bouchet**, March 30, 2026 · 4 min read
- Talk
  ### [The quality mindset: how to ship flawless WordPress sites](https://qanary.dev/en/blog/quality-mindset-wordpress-sites)
  Define your critical journeys, document them in a spreadsheet, and move from reactive quality to quality you control: our talk at WordCamp Nice 2026.
  **Antoine Bouchet**, February 13, 2026 · 2 min read

[All articles](https://qanary.dev/en/blog)
