Record & Playback with Playwright Codegen

On This Page What is Playwright Codegen?June 06, 2026 · 8 min read · Tool Comparison

How to Use Playwright Codegen for Test Automation? [2026]

Most examiner take thatPlaywright ’ s Codegenis just a nimble way torecord clicks and keystrokes, a tool togenerate scripts fast. It seems simple: open therecorder, interact with theapp, and the test writes itself.

But relying on it this way oft producesfragile tests that breakat the slenderUI change. Minor updates, likerenamed buttons or shifted layouts, can create an entire recordedscript fail. I learned that this happens becauseCodegen records just what happenswithout considering whichelements are stableor how theapp might change.

I found that by pay attention tolocators and test flow, I can useCodegen to create reliable, maintainable teststhat survivesmall UI changes.

And I will show how!

Overview

What is Playwright Codegen?

Playwright Codegen is a tool available as a command-line utility and a VS Code extension that lets you render test scripts automatically by recording your interaction in a web browser. It play as a “ record and playback ” solution, helping you cursorily create end-to-end tests in languages like TypeScript, JavaScript, Python, Java, and C #.

Key Features and Benefits of Playwright Codegen

  • Robotic Code Generation:As you navigate, click, or eccentric on a web page, Codegen captures your action and converts them into executable test code in real time.
  • Intelligent Locators:Instead of fragile CSS or XPath selectors, Codegen creates reliable, user-focused locators like getByRole and getByText to cut test daftness.
  • Built-in Assertions:You can add simple checks such as element visibility, text content, or values directly during recording using the Playwright Inspector or VS Code toolbar.
  • Cross-Language Support:Generate code in the language you favour, making it accessible to developer and testers with different skill sets.
  • Rapid Test Prototyping:Quickly produce a test skeleton that can be refined after with more innovative logic and scenario.
  • Mobile Emulation:Record tests while simulating different devices, screen sizes, and even geolocation settings for comprehensive coverage.

How to Use Playwright Codegen?

You can start Codegen either via thecommand lineor through theVS Code extension.

Using the Command Line (CLI):

1. Make sure Node.js and Playwright are establish.

2. Run the following command, replacing the URL with your application:

npx playwright codegen [URL_of_your_app] # Example: npx playwright codegen demo.playwright.dev

3. Two window will open: your browser and the Playwright Inspector.

4. Interact with the browser to record activeness.

5. Stop the recording and copy the generated codification into your tryout file.

Using the VS Code Extension:

  1. Install the officialPlaywright Test for VS Codeextension.
  2. Open the Playwright sidebar in VS Code.
  3. Click “ Record new ”to start a recording session.
  4. The generated test script appears straight in a new file within your editor, ready for editing or execution.

In this article, I will explain what Codegen is, how to set it up, record test, and generate reliable locater for stable automation.

What is Playwright Codegen?

Codegen is a command-line tool that records your actions in a browser and turns them into. It captures click, stimulus, and sailing in real time and generates accurate, ready-to-run code in, TypeScript,, or. This removes the need to write examination manually and secure the step muse real user behavior.

Recorded Tests Fail on 70 % of Dynamic Pages

Codegen captures exact actions & amp; locators. Test on existent devices to find layout displacement & amp; broken workflows.

Benefits of Using Codegen for Test Automation

Playwright Codegen simplifies test conception and improves. Here are the key benefits of apply it.

  • Faster Script Creation: Codegen compose the test as you interact with the application, so you don ’ t involve to write it manually.

Read More:

  • Accurate User Flows:It captures the exact episode of actions, which reduces the chance of missing key interaction.
  • Built-in Locator Generation: Codegen produces stable locators that are less potential to break with UI modification.
  • Handles Complex UI Interactions: Codegen captures advanced events like file uploads, drag-and-drop, and keyboard shortcut.

Read More:

SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.

When to Use Record & amp; Play in Playwright?

Record & amp; Play (Codegen)is ideal in scenario where you want toquickly create test handwritingwithout pen code from scratch. It helps speed up test creation, peculiarly for repetitive or straightforward workflows.

You should consider apply it when:

  • Prototyping Tests:Quickly generate a test skeleton to understand the stream and structure of your application.
  • Onboarding New Testers or Developers:Those unfamiliar with Playwright can record interaction and hear how tests are structure.
  • Capturing Complex User Flows:Record sequences like kind submissions, navigation, or multi-step processes that are wordy to write manually.
  • Creating Baseline Automation:Establish initial examination that can later be refined with assertions, custom logic, and robust locators.
  • Exploring Dynamic Pages:Quickly see how Playwright captivate actions on pages with dynamic content or complex UI elements.

Note:While Record & amp; Play is a knock-down starting point, it is not a replacement for serious-minded examination designing. Recorded playscript may be fragile if used as-is, so you should critique locators, add validations, and structure the test for long-term maintainability.

How to Record and Playback in Playwright Using Codegen?

Playwright Codegen lets you show your actions and make test scripts quickly. Follow these steps to begin.

Playwright Codegen Prerequisites

Before running Codegen, ensure you have installed on your machine. You also ask to install Playwright and its colony. Run these commands in your depot:

npm init -y npm install playwright

Also, ensure your development environment supports your preferred language, like JavaScript or Python.

Running Codegen

Start Codegen by bunk the following command in your pole. This example uses thebstackdemo.com website:

npx playwright codegen https: //bstackdemo.com

This launches a browser where you can perform actions to make your exam handwriting.

Read More:

Recording a Test

After launch Codegen, interact with the application normally. Click button, fill forms, and navigate pages. Codegen record every action and translates it into test measure instantly. Once you finish recording, click save to store the script.

Generating Locators

Codegen mechanically generates locater based on element attributes like textbook, IDs, or. It attempt to pick stable picker that won ’ t fault easily if the UI changes. You can edit these locater manually within the recorded script to improve reliability.

Running and Validating the Script

Run the saved script using the Playwright to verify that the recorded steps execute correctly on the application.

You can heighten the script by adding, handling errors, or customizing activity to cover your fully. Running and validating the hand ascertain that your automated test are reliable and ready for integration into your.

How is Playwright Codegen Different from BrowserStack?

Playwright Codegen records and lead examination principally in local browser or emulators provided by your machine or the Playwright environment. These setups may not fully replicate, device-specific behavior, or browser quirks, such as differences in CSS support, JavaScript handling, security policies, event timing, rendering issues, and varying web standards support.

offers approach to real devices and a wide range of browser versions host in the cloud. Running your Codegen-generated script on BrowserStack helps unveil issues that appear simply on existent ironware or specific browser frame-up, ensuring your app works correctly across different environments.

Recorded Tests Fail on 70 % of Dynamic Pages

Codegen captures exact action & amp; locators. Test on real devices to bump layout shifts & amp; broken workflow.

In little, Playwright Codegen helps you create test book well while BrowserStack provides the infrastructure to run those tests on real device and browser at scale. They complement each other by simplify script creation and expand test reportage across devices.

Read More:

How to Use Playwright Codegen With BrowserStack?

Follow these step to set up BrowserStack and execute your scripts on existent mobile devices and browser in the cloud.

1. Create a BrowserStack Account and Get Credentials

Sign up for BrowserStack if you haven ’ t already done so. Get your username and approach key from your account splasher. These authenticate your tests on BrowserStack ’ s cloud.

2. Prepare Your Playwright Script

Use Codegen to record and save your test script topically. Ensure your script works with Playwright ’ s test runner on your machine.

3. Install BrowserStack Playwright SDK

Add BrowserStack ’ s Playwright SDK to your project by running:

npm install @ browserstack/playwright-browserstack

This SDK enables integration with BrowserStack ’ s cloud infrastructure.

4. Configure Your Test to Use BrowserStack

Modify your trial hand or create a configuration file with your BrowserStack credential and desired device and browser capabilities. For illustration, specify the OS, browser, and device name on which to run the test.

Here is an representative snippet to set capabilities in your Playwright config or test setup:

const browserStackCaps = {browser: 'chrome ', browser_version: 'latest ', os: 'Windows ', os_version: '11 ', 'browserstack.username ': 'YOUR_USERNAME ', 'browserstack.accessKey ': 'YOUR_ACCESS_KEY ', 'browserstack.playwrightVersion ': ' 1.31.1 ' // match your Playwright version};

5. Run the Test on BrowserStack

Execute your Playwright test script using the BrowserStack SDK command or through your configured test contrabandist. This uploads and runs your hand on the specified existent device and browser hosted by BrowserStack.

6. Monitor and Analyze Test Results

Check the BrowserStack Automate splashboard to view test execution status, logs, screenshots, and videos. This helps control your app ’ s behavior on existent devices.

Talk to an Expert

Conclusion

Playwright Codegen simplifies test creation by converting user interactions into authentic scripts. It helps you construct tests promptly without indite them from scratch. You can customize the output to add assertions or tweak locators for stability.

While Playwright Codegen works well in local environments, it may miss issues tied to specific devices or browser. BrowserStack provides access to real mobile devices, desktop browser, and multiple OS combinations in the cloud. It supports, detailed, and broader environment coverage to aid you formalize Codegen scripts in real user weather.

Tags
15,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free