DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk6 min

Playwright Codegen: Record Browser Actions to Generate Tests

Use Playwright Codegen to record browser interactions, generate a test draft, and refine its locators and assertions for a reliable test.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run npx playwright codegen to open a browser and Playwright Inspector, interact with the page, then copy the generated test code into your project. You can provide a starting URL on the command line or navigate to it in the browser. Codegen is a useful first draft: review its locators and add assertions that prove the behavior you care about.

Record a browser flow with Playwright Codegen

  1. From your project directory, start Codegen with a page URL:

    npx playwright codegen https://demo.playwright.dev/todomvc

    The URL is optional. Run npx playwright codegen by itself and navigate in the opened browser instead. The command-line form is documented in the Playwright CLI reference.

  2. In the opened browser, perform the actions you want your test to reproduce: click controls, fill fields, and navigate as a real user would. Playwright Inspector displays generated code as you interact.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  3. When useful, add an assertion using Inspector’s toolbar controls. Codegen can generate checks for visibility, text, and value.

  4. Stop recording, copy the generated code into your editor, and review it before relying on it. The Codegen guide describes this recording-and-review workflow.

What Codegen records—and what you still need to decide

Codegen records interactions and produces code that targets elements through locators. It analyzes the rendered page and prioritizes role, text, and test ID locators; when a locator matches multiple elements, it attempts to make the target unique. That is helpful, but it cannot decide whether the chosen element or recorded scenario represents the behavior your test is meant to protect.

Review the scenario and target

Prefer resilient locators and retrying assertions

Playwright’s locator guidance recommends user-facing attributes and explicit contracts such as page.getByRole(). Locators resolve the current DOM element when an action runs, which helps when the DOM changes between steps. For assertions, the best-practices guide recommends web-first assertions that wait and retry until the expected condition is met. For example:

await expect(page.getByText('welcome')).toBeVisible();

Use an assertion that matches the behavior under test; a generated visibility, text, or value check is only useful if it verifies the expected result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose how to use Codegen

Need Use
Record a new flow in a standalone browser and Inspector Run npx playwright codegen [url].
Add recorded actions to an existing test in VS Code Put the cursor at the insertion point and use Record at cursor in the Playwright VS Code extension.
Generate a locator without recording a whole test Stop recording, use the locator picker, hover to preview the target, select it, then copy or edit the locator. The VS Code integration also offers a locator picker.

The VS Code recording and locator workflows are documented in Getting started with VS Code; the Inspector locator picker is also covered by the Generating tests guide.

Configure browser, language, viewport, and session state

Codegen has CLI options for choosing the browser, output file, target language, and test ID attribute. The CLI reference lists Chromium, Firefox, and WebKit, with Chromium as the documented default. Because exact flags can vary with the installed Playwright version, check npx playwright codegen --help and the official CLI reference for your installation.

The Codegen guide also documents options for viewport size, device emulation, color scheme, timezone, geolocation, language, saved storage state, and HTTP Basic Authentication. Examples below show the documented option names; verify their availability and syntax against your installed version.

Set browser, output, language, and test ID attribute

npx playwright codegen --browser firefox --output=tests/recorded.spec.ts --target=playwright-test --test-id-attribute=data-testid https://example.com

Use --browser to select Chromium, Firefox, or WebKit; --output to write generated code to a file; --target to choose a language or target format; and --test-id-attribute when the application uses a test ID attribute other than the default. The CLI documentation includes Python output examples.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Match the target environment

npx playwright codegen --viewport-size="1280,720" --color-scheme=dark --lang=en-US --timezone=America/New_York --geolocation="37.7749,-122.4194" https://example.com

--viewport-size sets the recording viewport. --device emulates a named device, including its viewport and user agent. The guide also lists --color-scheme, --timezone, --geolocation, and --lang for context emulation. Match these settings to the environment whose behavior the test needs to cover; changing them can change the page layout and available content.

Reuse an authenticated session carefully

Use --save-storage to save cookies, local storage, and IndexedDB state, then --load-storage to reuse it in a later recording. Treat the resulting file as sensitive authentication data: keep it local, add it to .gitignore, or delete it when finished, as the Codegen guide advises.

npx playwright codegen --save-storage=playwright/.auth/state.json https://example.com

For a later session, load that state with:

npx playwright codegen --load-storage=playwright/.auth/state.json https://example.com

Use --user-data-dir to record with a separate existing browser profile directory. The guide notes that Chrome 136 and later prevent automated tools from accessing the default user data directory, so use a separate directory rather than relying on the default profile.

Handle HTTP Basic Authentication with care

--http-credentials supplies HTTP Basic Authentication credentials. The Codegen guide warns that credentials are sent to any origin that requests them during recording and are included in generated code. Record only in a controlled session, and keep credentials out of committed files.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pause to configure a headed browser

For custom setup before opening Codegen controls, the guide describes calling page.pause() in a headed browser. This is an option for a test or setup flow that needs to configure its browser context before you begin recording.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common Codegen problems

  • The browser or Inspector does not start: Confirm Playwright is installed for the project and run the command from the intended project directory. Check npx playwright codegen --help for the installed CLI’s accepted options and syntax.

  • The generated locator targets the wrong item or is ambiguous: Use the locator picker to inspect the target, then replace or refine the locator using a stable, user-facing role, text, or explicit test ID contract. Review the locator guide.

  • The test fails after the page changes: Re-record only if the user journey changed. Otherwise, inspect the locator and add an assertion for the intended outcome. Prefer a web-first assertion that retries instead of a one-time state check.

    Free tools Windows power users keep installed

    One-click scans. No signup required.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • The recording does not reflect the expected mobile or regional experience: Set the appropriate device or viewport and, if relevant, language, timezone, geolocation, and color scheme before recording.

  • A saved session does not authenticate: Confirm the correct storage file was loaded and that it contains the required state. Storage may expire or become invalid; save a fresh session when needed, and do not commit the file.

  • Chrome rejects use of the default profile directory: With Chrome 136 or later, use a separate directory through --user-data-dir; automated tools cannot access the default user data directory according to the Codegen guide.

Or skip the browser setup

If your goal is to capture a site rather than build a Playwright test, ScreenshotNeo is a website screenshot API and MCP server. One GET request can return a PNG, JPEG, WebP, or PDF. For example, this cURL request saves a WebP screenshot of Stripe:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. It accepts cookie and consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed. Its MCP server gives AI agents tools to take screenshots, get page information, and capture PDFs. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Frequently Asked Questions

Can I use Codegen just to get a locator?

Yes. Stop recording, use the locator picker to preview and select the element, then copy or edit the locator. VS Code also has a locator picker.

Does Codegen create assertions as well as actions?

Yes. Its assertion controls can generate visibility, text, and value checks.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Can Codegen generate Python?

Yes. The CLI’s target option selects the output language or format, and the official CLI reference includes Python output examples.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.