Skip to content
← All posts
  • playwright
  • automation
  • AI
  • testing

Playwright CLI + AI: How to Write Smarter Tests Faster in 2025

Learn how to combine Playwright's powerful CLI with AI tools like GitHub Copilot and Claude to generate, debug, and maintain browser automation tests faster than ever before.

Kundalik Jadhav7 min read

Why Playwright + AI Is the Combination QA Engineers Need Right Now

Browser automation used to mean hours of writing locators, debugging flaky selectors, and maintaining tests that broke every time the UI changed. Playwright already solved a lot of that with its excellent tooling. But now, combining Playwright's CLI with AI assistants takes productivity to a completely different level.

This guide walks you through the Playwright CLI commands that matter most, and shows you exactly how to use AI tools alongside them — from generating test scaffolds to fixing broken tests to writing assertions you didn't know you needed.


The Playwright CLI: What It Can Actually Do

Most QA engineers know npx playwright test. Far fewer use the full CLI. Here's a breakdown of the commands that genuinely save time.

1. playwright codegen — Your AI's Starting Point

npx playwright codegen https://www.qaplayground.com

codegen opens a browser and records every click, fill, and navigation you make, outputting ready-to-run test code in real time. You can choose your language:

npx playwright codegen --target=javascript https://www.qaplayground.com/practice
npx playwright codegen --target=python https://www.qaplayground.com/bank

Where AI comes in: codegen produces functional but often verbose tests. Paste the output into your AI assistant (Copilot, Claude, Cursor) and ask it to:

  • Refactor repetitive locators into a Page Object Model
  • Add expect() assertions the recorder missed
  • Extract hardcoded values into test data variables
  • Group related tests into a test.describe() block

The recorder gives you the skeleton. AI gives it structure.


2. playwright test --ui — Visual Debugging Without the Guesswork

npx playwright test --ui

The Playwright UI Mode gives you a timeline, a DOM snapshot at each step, and the ability to re-run individual tests from any point. It is the fastest way to understand why a test failed.

Where AI comes in: When a test fails, copy the error message and the relevant test code into your AI assistant. A prompt like:

"This Playwright test is failing with TimeoutError: Locator.click: Timeout 30000ms exceeded. Here is the test code and the error. What is likely wrong and how do I fix it?"

...will get you a diagnosis in seconds — whether it is a missing await, a wrong locator strategy, or a race condition. You stop staring at stack traces and start fixing.


3. playwright test --debug — Step Through Tests Like a Pro

npx playwright test login.spec.js --debug

Debug mode opens the Playwright Inspector, which lets you step through each action one at a time, see the current DOM state, and test locators interactively in the console. When a specific step fails, you can pause right before it and inspect what the page actually looks like.

# Debug only one test by name
npx playwright test --debug -g "should login with valid credentials"

Where AI comes in: After stepping through the failing test and identifying the problematic selector, ask AI to suggest a more resilient locator. For example:

"My Playwright test uses page.locator('.btn-submit') but it breaks when the design changes. The button always has the text 'Submit' and a data-testid='submit-btn'. What locator strategy should I use?"

AI will explain the trade-offs between CSS class locators, role-based locators, and data-testid attributes — and write the replacement for you.


4. playwright show-report — Understanding What Failed at Scale

npx playwright show-report

After a test run, this command opens an HTML report with pass/fail status, screenshots on failure, video recordings, and trace files. When you have 50 tests and 12 failures after a deploy, this report tells you the story.

Where AI comes in: Copy the list of failing tests and their error messages into AI and ask:

"These 12 Playwright tests started failing after today's deployment. Group them by likely root cause."

AI will often spot patterns — for example, five failures around login suggest an auth change, three around a table suggest a selector change — giving you a triage plan before you open a single file.


5. playwright show-trace — Deep Dive Into Any Test Run

npx playwright show-trace trace.zip

Traces capture every network request, every DOM snapshot, and every action in a timeline view. They are especially useful for intermittent failures — the kind that only happen in CI.

Where AI comes in: Describe what the trace shows to an AI assistant. You might say:

"My trace shows the login button was clicked at step 4, but the navigation to /dashboard never happened. The network tab shows a 302 redirect to /login instead. What should I check?"

AI reasons through the scenario — expired session, a missing cookie, a CSRF token issue — and points you at the right place to look.


6. playwright test --reporter — Formatting Results for CI and Teams

npx playwright test --reporter=html
npx playwright test --reporter=json
npx playwright test --reporter=dot

Playwright supports multiple reporter formats out of the box. JSON output is particularly useful for piping into dashboards or log aggregators.

Where AI comes in: Paste your JSON test results into AI and ask it to:

  • Summarize pass/fail rates per spec file
  • Identify the slowest tests
  • Write a Slack message summarizing the CI run

This is especially useful if you want to automate CI notifications without building a custom reporter.


A Real Workflow: From Zero to Full Test Suite With AI

Here is a practical end-to-end workflow that combines the CLI and AI tools.

Step 1 — Record the happy path:

npx playwright codegen https://www.qaplayground.com/bank

Walk through the bank login, dashboard, and account creation. You now have a raw recorded script.

Step 2 — Refactor with AI: Paste the script into Claude or Copilot Chat. Prompt:

"Refactor this into a Page Object Model with separate classes for LoginPage and AccountsPage. Add assertions for each major action."

Step 3 — Run and debug:

npx playwright test --ui

Use UI Mode to identify any failures. For each failure, ask AI to diagnose based on the error.

Step 4 — Generate edge case tests: Paste your LoginPage class into AI and prompt:

"Based on this login page structure, write 5 negative test cases — wrong password, empty fields, SQL injection attempt, very long input, and account lockout."

Step 5 — Add to CI:

npx playwright test --reporter=html --workers=4

Push your tests. Share the HTML report link with the team.


AI Prompts That Actually Work With Playwright

Here are prompts that consistently produce useful output:

Situation Prompt
Converting a recorded script "Refactor this Playwright codegen output into a Page Object Model"
Writing assertions "Add meaningful expect() assertions to this Playwright test"
Fixing a timeout "This Playwright test times out on this locator. Suggest alternatives"
Data-driven tests "Rewrite this test to run with multiple input datasets using test.each"
CI failures "These tests pass locally but fail in CI. What are common causes in Playwright?"
Locator strategy "Which locator is most resilient for this HTML element: [paste HTML]"

What AI Cannot Do (Yet)

Be honest with yourself about AI's limits in test automation:

  • It does not know your app. AI generates plausible tests, not correct ones. Always run generated tests against the real app and verify they fail for the right reasons.
  • It cannot catch visual regressions. For pixel-level comparisons, you still need expect(page).toHaveScreenshot() with a baseline.
  • It will hallucinate locators. AI may suggest selectors that look right but do not exist. Always validate generated locators with the Playwright Inspector.
  • Flaky tests need trace analysis. AI can suggest what to look for, but reading an actual Playwright trace requires human judgment.

Getting Started Today

If you want a real target to practice against, QA Playground (qaplayground.com/practice) has 22 interactive elements — inputs, tables, alerts, drag-and-drop, calendars — all designed specifically for automation practice. Run playwright codegen against the practice pages, feed the output to AI, and you will have a meaningful test suite within an hour.

The combination of Playwright's CLI and AI tools does not replace QA engineers. It removes the repetitive parts — the boilerplate, the locator debugging, the test scaffolding — so you can spend time on what actually requires judgment: test strategy, edge case thinking, and understanding what needs to be tested in the first place.

Start with codegen. Fix it with AI. Own the result.