DEV Community

Cover image for Stop Writing Boilerplate: How Playwright Codegen Will Change Your Test Automation Workflow
Khiem Phan
Khiem Phan

Posted on

Stop Writing Boilerplate: How Playwright Codegen Will Change Your Test Automation Workflow

We’ve all been there. You're starting to automate a new test case. You open your code editor, open the browser, open the DevTools, and begin the tedious process of inspecting elements, hand-crafting selectors, and writing the basic boilerplate code for every single click and text input. It’s a slow, meticulous process that can drain your momentum before you even get to the interesting part—writing the actual assertions.

But what if you could skip most of that? What if you could generate clean, best-practice test code simply by using your application like a real user?

That’s exactly what Playwright Codegen does. It’s the official code generation tool built directly into the Playwright library, and it is, without a doubt, one of the most powerful features in any modern automation framework. If you're not using it, you're leaving a massive amount of productivity on the table.

1. What is Playwright Codegen?

Playwright Codegen is an interactive tool that records your actions in a browser and automatically translates them into high-quality Playwright test code. When you launch it, it opens two windows: a browser window with your web app and an Inspector window that writes code in real-time as you interact with the page.

It’s more than just a simple recorder; it’s a smart assistant that helps you write better, more resilient tests from the very beginning.

2. Getting Started: Your First Recorded Test in Under a Minute

The best part about Codegen is how easy it is to get started. You don’t need to set up a complex project or write a single line of code.

1. Make sure you have Playwright installed. If not, open your terminal and run

# For Node.js/TypeScript projects
npm install @playwright/test
npx playwright install

2. Launch Codegen from your terminal. This single command is all it takes. It will open a new browser window and the Inspector.

# To generate TypeScript/JavaScript code
npx playwright codegen https://www.saucedemo.com/

Codegen’s first look

Codegen’s first look

  1. Perform your actions. In the browser window that just opened, go through a standard login flow:
    • Type standard_user into the username field.
    • Type secret_sauce into the password field.
    • Click the "Login" button.
  2. Watch the code appear. As you perform these actions, you will see the Playwright Inspector window populate with clean, ready-to-use code. Generated Typescript Code:
import { test, expect } from '@playwright/test';

test('test', async ({ page }) => {
  await page.goto('https://www.saucedemo.com/');
  await page.locator('[data-test="username"]').click();
  await page.locator('[data-test="username"]').fill('standard_user');
  await page.locator('[data-test="password"]').click();
  await page.locator('[data-test="password"]').fill('secret_sauce');
  await page.locator('[data-test="login-button"]').click();
});

Test script is generated in Playwright Inspector on the right

Test script is generated in Playwright Inspector on the right

You now have a working script that you can copy directly into your test file.

3. Why This Will Revolutionize Your Workflow

3.1. It Teaches Best Practices for Locators

This is Codegen’s most underrated feature. It doesn't just grab the first CSS class or ID it finds. It analyzes the element and generates the most user-facing, resilient locator possible, prioritizing roles, text, and placeholders. It actively teaches you to write better locators.

3.2. It Drastically Speeds Up Test Creation

For straightforward user flows, you can generate 80% of your test script in a matter of seconds. This allows you to focus your energy on the most important parts of testing: adding assertions, handling dynamic data, and structuring your code.

3.3. It Can Record Authenticated Sessions

This is a pro-level tip. Repetitive logins are a pain. Codegen can save your authentication state (cookies and local storage) so you can start your next recording session already logged in.

To save your state after logging in:

npx playwright codegen --save-storage=auth.json https://www.saucedemo.com/

To start a new session already logged in:

npx playwright codegen --load-storage=auth.json https://www.saucedemo.com/inventory.html

4. Conclusion: An Essential Tool, Not a Crutch

Playwright Codegen isn't designed to completely replace the need to write code. You will always need to refine the generated script, add robust assertions, and structure it within a proper test suite.

However, as a tool for accelerating the initial creation of tests, learning best-practice locators, and quickly debugging selectors, it is absolutely unparalleled. It removes the most tedious part of UI automation and lets you focus on what really matters: ensuring quality. If you're a part of any team working with Playwright, making codegen a part of your daily workflow is a no-brainer.

Top comments (0)