Playwright Record & Play: A Quick Guide to Codegen for Test Automation

Table of Content

What is "Record & Play" in Playwright?

Interact with a web app manually (clicks, typing, navigation, etc.)

Auto-generate Playwright test code based on those actions

When to Use It

  • Learning Playwright's syntax 
    Helps beginners understand how actions translate to code.
  • Exploratory testing sessions 
    Record a session and later turn it into a regression test.
  • Rapid prototyping 
    Want to quickly script user flows without writing everything manually.

Why Use It

  • Saves time by reducing manual coding
  • Learns selectors and flows automatically (even suggesting better selectors)
  • Editable and extendable – use recorded code as a starting point and customize it
  • Helps in debugging or replicating test scenarios

CTA - Record & Play Scripts for web in Playwright Test Generator Codegen.png

 

How to Use "Record & Play" in Playwright

Prerequisites

1. Make sure Playwright is installed: npm init playwright@latest

2. OR if it is already installed: npx playwright install

There are two main ways to create tests in Playwright:

1. Manual Scripting: 
You can write the test code yourself using Playwright's API. This gives you full control over the logic, structure, and assertions in your test.

2. Using the Test Generator (Codegen): 
Playwright provides a tool called the Test Generator or Codegen, which acts like a test recorder. It automatically captures your actions on a website (like clicks, typing, navigation, etc.) and generates the corresponding test script for you. Once recorded, you can edit, save, and run the test.

Step_1: Open the terminal

  • Execute the command >> npx playwright codegen

    Picture, Picture
  • This command will open the Playwright Inspector along with the browser, and one will automatically trigger the launch of the other. By default, the browser launched is Chrome.

    Picture, Picture


Steps_2: launch the application URL on the opened browser 

  • Interacting with the elements and parallelly in the playwright inspector the script is automatically generating.

(the cursor on the element it is also suggesting required locator)

Group 1, Grouped object, Picture

Step_3: Once the recording is over >> Click on the “Record” button from playwright Inspector

Group 1, Grouped object, Picture

  • Inspector automatically generates a script for operations and a few more options are available in this playwright inspector Pick locator so by using it easily get locators for the elements.

E.g.: 

  1. Click on the Pick location option
  2. Click on any button OR link to generate the locator
  3. Check that the locator which is automatically created by the playwright Inspector

    Picture

Here, the script must be manually copied, and a new test file must be created under the test folder. The script must then be copied and executed. 

To solve this issue, a single command will create a new test file and cause the script to be automatically recorded in the file itself.

Step_1: Open the terminal

  • Execute the command >> npx playwright codegen --output tests/recordfile.spec.js OR npx playwright codegen –o tests/recordfile.spec.js

(Once command executed it will create file automatically and whatever the test have recorded, will be copied into this file with the steps)

Picture

Step_2: Once the browser and playwright inspector opened 

  • launch the application URL on the opened browser
  • Perform the steps
  • Once it done, click on the Record button from Playwright Inspector

    Inserting image..., Picture

A new file which is automatically created with script.

Conclusion:

Playwright's Record & Play (Codegen) feature is a powerful tool for quickly generating test scripts with minimal effort. Whether you're new to Playwright or looking to prototype user flows rapidly, this feature streamlines the process by capturing real-time interactions and turning them into editable, executable code.

It not only accelerates test creation but also helps you learn Playwright’s syntax, improve locator strategies, and build a solid automation foundation. With just a single command, you can launch a browser, record actions, and generate complete test scripts making automation simpler, faster, and smarter.

About Author

Divya Panchal

Divya Panchal is an ISTQB-certified QA Tester at PixelQA a Software Testing Company with expertise in both manual and automation testing. She has evolved from a trainee to a key contributor across multiple projects, consistently ensuring software quality and reliability. With a strong focus on continuous professional development, Divya is dedicated to expanding her knowledge in automation and API testing, underscoring her commitment to delivering high-quality software solutions.