<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Change03</title>
    <description>The latest articles on DEV Community by Change03 (@change03).</description>
    <link>https://dev.to/change03</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4278186%2F6c560a9c-7a42-401c-b0de-629c84c77fc0.png</url>
      <title>DEV Community: Change03</title>
      <link>https://dev.to/change03</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/change03"/>
    <language>en</language>
    <item>
      <title>Recording a real browser session into a Playwright suite: locators, POM, fixtures and secrets</title>
      <dc:creator>Change03</dc:creator>
      <pubDate>Wed, 02 Dec 2026 11:18:59 +0000</pubDate>
      <link>https://dev.to/change03/recording-a-real-browser-session-into-a-playwright-suite-locators-pom-fixtures-and-secrets-gfi</link>
      <guid>https://dev.to/change03/recording-a-real-browser-session-into-a-playwright-suite-locators-pom-fixtures-and-secrets-gfi</guid>
      <description>&lt;p&gt;Playwright's &lt;code&gt;codegen&lt;/code&gt; is good at exactly one thing, and it is the thing I use it for: telling me which locator would find the element I just clicked. Everything after that — the part where the recording becomes a file my team is willing to keep — is left as an exercise.&lt;/p&gt;

&lt;p&gt;That gap is where I kept losing half a day per flow. So I built a recorder that closes it: it watches my real browser session and writes the test file I would have written. This post is the design of that generator, with the decisions that mattered and the ones that turned out to be traps.&lt;/p&gt;

&lt;h2&gt;
  
  
  What &lt;code&gt;codegen&lt;/code&gt; produces, and why it doesn't survive review
&lt;/h2&gt;

&lt;p&gt;Record a checkout flow and you get something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;goto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'http://localhost:3000/checkout'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Email'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Email'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'buyer@example.com'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Password'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hunter2'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByRole&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'button'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Pay'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every line is correct. The file, as a file, has five problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;One file, so every flow shares one namespace and one URL.&lt;/li&gt;
&lt;li&gt;The URL is absolute and hard-coded, so the suite only runs where it was recorded.&lt;/li&gt;
&lt;li&gt;The password is in the source, which means it is one &lt;code&gt;git add .&lt;/code&gt; away from being in the repository.&lt;/li&gt;
&lt;li&gt;Typing is recorded as calls, so a forty-character search box is forty calls.&lt;/li&gt;
&lt;li&gt;There is no structure: no page objects, no fixtures, no steps — nothing a failure report can point at.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Points 1–5 are not locator problems. They are &lt;em&gt;generation&lt;/em&gt; problems, and they are solvable with information the recorder already has at the moment you press Stop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Four decisions that decide whether the output is usable
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Record in the session the flow lives in
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;codegen&lt;/code&gt; opens a fresh browser profile. That is fine for a public demo and useless for the flow I actually need to test: a client's staging environment behind a login, or a dev server with a session cookie I spent ten minutes creating.&lt;/p&gt;

&lt;p&gt;Recording in the user's own tab means the recorder has to be attached to &lt;em&gt;that&lt;/em&gt; tab only while recording, survive navigation, and detach when it stops. In a Manifest V3 extension those are three separate problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Attachment&lt;/strong&gt;: &lt;code&gt;chrome.scripting.registerContentScripts&lt;/code&gt; while recording, &lt;code&gt;unregisterContentScripts&lt;/code&gt; on stop. Nothing runs on any page before Start, and closing the tab throws the recording away.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Navigation&lt;/strong&gt;: registering the script for the tab means it is re-injected on every page load, so a multi-page flow is one continuous recording.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SPA routing&lt;/strong&gt;: &lt;code&gt;pushState&lt;/code&gt;, &lt;code&gt;replaceState&lt;/code&gt; and &lt;code&gt;popstate&lt;/code&gt; belong to the page's own JavaScript world and are invisible from an isolated content script. So a small file is registered with &lt;code&gt;world: 'MAIN'&lt;/code&gt; that wraps those three calls and forwards the new URL with &lt;code&gt;postMessage&lt;/code&gt;. It changes nothing about the page — it only reports.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That third one is the difference between a recorder that works on server-rendered apps and one that works on the app you actually have.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. A locator is a ranked search with a uniqueness check
&lt;/h3&gt;

&lt;p&gt;This is the part I expected to be simple and wasn't. "Best locator" is not a single heuristic; it is an ordered list of candidates, and the first candidate that matches &lt;strong&gt;exactly one element on the live page&lt;/strong&gt; wins.&lt;/p&gt;

&lt;p&gt;The order, best first:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Test id&lt;/strong&gt; — &lt;code&gt;getByTestId('pay-now')&lt;/code&gt; when the attribute is present. An explicit contract with the app; nothing else is as stable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Role + accessible name&lt;/strong&gt; — &lt;code&gt;getByRole('button', { name: 'Checkout' })&lt;/code&gt;. User-facing, survives a refactor, fails loudly when the copy changes. This is the default answer when the app has no test ids.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Label&lt;/strong&gt; for form controls — &lt;code&gt;getByLabel('Email')&lt;/code&gt;, not &lt;code&gt;#email&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Placeholder, alt, title&lt;/strong&gt; — weaker, but still attributes a human wrote on purpose.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Own visible text&lt;/strong&gt; — only when the element's own text &lt;em&gt;is&lt;/em&gt; its label, and only if it is short. A container whose text includes three paragraphs is not a text locator.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A stable &lt;code&gt;id&lt;/code&gt;&lt;/strong&gt; — and "stable" is doing work here. &lt;code&gt;:r1:&lt;/code&gt;, &lt;code&gt;mui-1234&lt;/code&gt;, &lt;code&gt;radix-9&lt;/code&gt;, &lt;code&gt;react-select-2-input&lt;/code&gt;, &lt;code&gt;ember123&lt;/code&gt;, pure digits: all rejected. Ids that contain a readable word pass. React and MUI regenerate ids between renders, and a locator that breaks on every deploy is worse than a CSS path, because it &lt;em&gt;looks&lt;/em&gt; fine.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Structural CSS path&lt;/strong&gt; — the last resort, and it is honest about being one.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every candidate is validated against the live DOM with a matcher that approximates Playwright's own (role computation, accessible-name computation, text normalisation) before the step is accepted. If a candidate matches three elements, &lt;code&gt;.nth()&lt;/code&gt; is appended — but only then. In practice &lt;code&gt;.nth()&lt;/code&gt; shows up rarely, and when it does, it is the first thing I replace by hand.&lt;/p&gt;

&lt;p&gt;The trap: &lt;strong&gt;role + name is only reliable if you compute the accessible name the way the browser does.&lt;/strong&gt; &lt;code&gt;aria-label&lt;/code&gt;, then a real &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt;, then the element's own text, then &lt;code&gt;title&lt;/code&gt; — in that order. Get it wrong and every generated &lt;code&gt;getByRole&lt;/code&gt; in the file is subtly off, which is the worst possible failure mode because the test passes right up until it doesn't.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. The event stream is not the test
&lt;/h3&gt;

&lt;p&gt;A recording is a stream of raw events. A test is what's left after you delete the noise, and the deletion has to be deliberate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Typing&lt;/strong&gt; is coalesced per field: one &lt;code&gt;fill&lt;/code&gt; 450 ms after the last keystroke, with the final value. Forty calls become one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A double click&lt;/strong&gt; — two clicks on the same locator within 500 ms — becomes one &lt;code&gt;dblclick&lt;/code&gt;, not two &lt;code&gt;click&lt;/code&gt;s. This matters more than it sounds: Playwright's &lt;code&gt;dblclick&lt;/code&gt; triggers the browser's own double-click behaviour, and two &lt;code&gt;click()&lt;/code&gt; calls do not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Duplicate navigations&lt;/strong&gt; collapse. Pages fire &lt;code&gt;pushState&lt;/code&gt; on render far more often than they change screen, and a &lt;code&gt;goto&lt;/code&gt; to the page you are already on is pure noise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Abandoned fills&lt;/strong&gt; disappear. Focusing a field and leaving it empty is not a step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repeated check/uncheck&lt;/strong&gt; of the same box disappears. It is almost always an artefact of clicking around while thinking.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The page each step happened on&lt;/strong&gt; is kept on every step. Not for the report — for the page objects, which need to know which screen a locator belongs to.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these are difficult rules individually. Together they are the difference between a 40-line draft and an 11-line test.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Structure follows the screens you touched
&lt;/h3&gt;

&lt;p&gt;The recording already knows the path of every step, so grouping is a one-line fold: consecutive steps with the same path become a group, and each group becomes a class.&lt;/p&gt;

&lt;p&gt;That produces, for a three-screen checkout flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pages/checkout.page.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Locator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Page&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@playwright/test'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;BasePage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./base.page'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CheckoutPage&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;BasePage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'/checkout'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;emailInput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Locator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;passwordInput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Locator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;payNowButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Locator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;emailInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Email'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;passwordInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Password'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payNowButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pay-now'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;fillEmailInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;emailInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;fillPasswordInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;passwordInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;clickPayNowButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payNowButton&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three decisions in that file are worth defending:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;readonly&lt;/em&gt;&lt;/strong&gt; locators, not strings inside methods. Assigning them once in the constructor is what lets Playwright's auto-waiting and retry behaviour apply to every use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Methods take the value as a parameter&lt;/strong&gt; (&lt;code&gt;fillEmailInput(value)&lt;/code&gt;) instead of baking in what I typed. The page object owns the interaction; the test owns the data. Otherwise the second test that needs a different email has to copy the class.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method names come from the element&lt;/strong&gt;: &lt;code&gt;getByLabel('Email')&lt;/code&gt; becomes &lt;code&gt;emailInput&lt;/code&gt;, &lt;code&gt;getByTestId('pay-now')&lt;/code&gt; becomes &lt;code&gt;payNowButton&lt;/code&gt;, and the verb comes from the action. Repeated actions on the same element collapse into one method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Page objects then arrive as fixtures, so a test simply asks for the screens it needs and gets fresh instances:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// tests/fixtures.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@playwright/test'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;CartPage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'../pages/cart.page'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;CheckoutPage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'../pages/checkout.page'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Fixtures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;cartPage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CartPage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;checkoutPage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CheckoutPage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Fixtures&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;cartPage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;use&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CartPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;checkoutPage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;use&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CheckoutPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;expect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the spec reads like the flow, one &lt;code&gt;test.step&lt;/code&gt; per screen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./fixtures'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'cart to order'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'cart to order'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cartPage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;checkoutPage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Cart'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cartPage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;goto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cartPage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clickCheckoutButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Checkout'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;checkoutPage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillEmailInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'buyer@example.com'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;checkoutPage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillPasswordInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PASSWORD&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;checkoutPage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clickPayNowButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One side effect worth mentioning: naming the steps after screens makes the HTML report readable by someone who has never opened the test file, and a failure says "Checkout" instead of a line number.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Secrets: never read the value
&lt;/h3&gt;

&lt;p&gt;The password line above is &lt;code&gt;process.env.PASSWORD&lt;/code&gt;, and the reason is not that the generator replaces a literal — it is that &lt;strong&gt;the value never leaves the page&lt;/strong&gt;. The recorder does not read a password field's value at all; it records the field's &lt;em&gt;name&lt;/em&gt; and nothing else. It cannot leak what it never has.&lt;/p&gt;

&lt;p&gt;Detection is a narrow set of signals, because a false positive is cheap (one more env var) and a false negative is a credential in git:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;type="password"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;autocomplete&lt;/code&gt; values for passwords, one-time codes and cards (&lt;code&gt;current-password&lt;/code&gt;, &lt;code&gt;new-password&lt;/code&gt;, &lt;code&gt;one-time-code&lt;/code&gt;, &lt;code&gt;cc-number&lt;/code&gt;, &lt;code&gt;cc-csc&lt;/code&gt;, &lt;code&gt;cc-exp&lt;/code&gt;, …)&lt;/li&gt;
&lt;li&gt;field names, ids or labels containing &lt;code&gt;pass&lt;/code&gt;, &lt;code&gt;secret&lt;/code&gt;, &lt;code&gt;token&lt;/code&gt;, &lt;code&gt;otp&lt;/code&gt;, &lt;code&gt;card&lt;/code&gt;, &lt;code&gt;cvc&lt;/code&gt;, &lt;code&gt;ssn&lt;/code&gt;, &lt;code&gt;iban&lt;/code&gt;
The environment variable name is derived from the field, not from a counter: &lt;code&gt;name="user_password"&lt;/code&gt; becomes &lt;code&gt;USER_PASSWORD&lt;/code&gt;, and only collisions get a &lt;code&gt;_2&lt;/code&gt; suffix. &lt;code&gt;.env.example&lt;/code&gt; is generated with plausible placeholders (&lt;code&gt;PASSWORD=your-password&lt;/code&gt;, &lt;code&gt;CARD_NUMBER=your-test-card&lt;/code&gt;), never the recorded value — including when what I typed while recording was obviously fake.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The generated &lt;code&gt;playwright.config.ts&lt;/code&gt; loads &lt;code&gt;.env&lt;/code&gt; itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;readFileSync&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'node:fs'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;devices&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@playwright/test'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="cm"&gt;/** Read KEY=value pairs from .env without adding a dependency. */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;loadEnvFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'.env'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'utf8'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;\&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;/^&lt;/span&gt;\&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="no"&gt;Z0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9_&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;\&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;*=&lt;/span&gt;\&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;\&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;$&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/^&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"']|["&lt;/span&gt;&lt;span class="s1"&gt;']$/g, '');
      if (process.env[match[1]] === undefined) process.env[match[1]] = value;
    }
  } catch {
    // A missing .env is fine: CI passes the variables itself.
  }
}

loadEnvFile();

export default defineConfig({
  testDir: '&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;tests&lt;/span&gt;&lt;span class="s1"&gt;',
  use: {
    baseURL: process.env.BASE_URL ?? '&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//localhost:3000',&lt;/span&gt;
    &lt;span class="n"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'retain-on-failure'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;screenshot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'only-on-failure'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;projects&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'chromium'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;use&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;devices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'Desktop Chrome'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Eight lines instead of a &lt;code&gt;dotenv&lt;/code&gt; dependency, and a precedence rule that matters: variables already set in the shell or CI win over the file, so &lt;code&gt;BASE_URL=https://staging.example.com npm test&lt;/code&gt; does what you expect and CI does not need a committed &lt;code&gt;.env&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the generator deliberately does not do
&lt;/h2&gt;

&lt;p&gt;A recorder is a starting point, and pretending otherwise is how these tools get uninstalled. So the export is explicit about its boundaries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No assertions.&lt;/strong&gt; A recording proves a flow can be walked, not that it is correct. &lt;code&gt;expect()&lt;/code&gt; is the part that needs a human.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No iframes, shadow-DOM internals, canvas, file uploads or drag and drop.&lt;/strong&gt; Those need &lt;code&gt;frameLocator&lt;/code&gt;, &lt;code&gt;setInputFiles&lt;/code&gt;, &lt;code&gt;dragAndDrop&lt;/code&gt; or coordinates, and a guess at them is worse than a TODO.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No multi-tab flows.&lt;/strong&gt; One tab per recording; join them with &lt;code&gt;waitForEvent('popup')&lt;/code&gt; afterwards.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Text locators stay fragile.&lt;/strong&gt; When the copy changes, &lt;code&gt;getByText('Save')&lt;/code&gt; breaks. The fix is a &lt;code&gt;data-testid&lt;/code&gt; in the app and a re-record, and no generator can do that for you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The complete list ships with the product, and it is short on purpose.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I know the output compiles
&lt;/h2&gt;

&lt;p&gt;The failure mode of a code generator is confident, plausible, broken output. So the generator's own test suite does not just compare strings — it exercises the output:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;generated TypeScript is written to disk and type-checked with &lt;code&gt;tsc&lt;/code&gt; against the real &lt;code&gt;@playwright/test&lt;/code&gt; type definitions;&lt;/li&gt;
&lt;li&gt;the Python export (&lt;code&gt;pytest-playwright&lt;/code&gt;) is compiled with &lt;code&gt;py_compile&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;the ZIP is opened with Python's &lt;code&gt;zipfile&lt;/code&gt; and every entry is verified;&lt;/li&gt;
&lt;li&gt;the recorder itself runs against a jsdom page, so locator choice and step normalisation are tested against a DOM, not a mock.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the generator drifts from Playwright's types, the build fails before anything ships. That one check has caught more bugs than every unit test I wrote around it.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you want the whole thing
&lt;/h2&gt;

&lt;p&gt;I packaged this as &lt;strong&gt;RecSuite&lt;/strong&gt;, a Chrome extension: press record, walk the flow in the browser you are signed into, review the steps, export.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The recorder, step review, code preview and single-file export (TypeScript, JavaScript, Python) are free.&lt;/li&gt;
&lt;li&gt;The full project export — page objects, fixtures, config, CI workflow, &lt;code&gt;.env&lt;/code&gt; handling — is &lt;strong&gt;$39 one-time&lt;/strong&gt; (Personal, one developer, unlimited personal and client projects). A &lt;strong&gt;Team&lt;/strong&gt; licence is &lt;strong&gt;$99&lt;/strong&gt; for up to 10 developers.&lt;/li&gt;
&lt;li&gt;Product page: &lt;a href="https://hwmich5.gumroad.com/l/recsuite" rel="noopener noreferrer"&gt;https://hwmich5.gumroad.com/l/recsuite&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Documentation, screenshots and the privacy policy (no server, no analytics, nothing uploaded): &lt;a href="https://github.com/Change03/recsuite-playwright-recorder" rel="noopener noreferrer"&gt;https://github.com/Change03/recsuite-playwright-recorder&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you only take one thing from this post, take the locator order. Even a hand-written suite gets better when the ranking is explicit, and the uniqueness check is the part that keeps a test suite from quietly turning into a set of CSS paths.&lt;/p&gt;

</description>
      <category>playwright</category>
      <category>testing</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Refresh token rotation with reuse detection in Spring Boot 3.5 (and the concurrency trap)</title>
      <dc:creator>Change03</dc:creator>
      <pubDate>Wed, 02 Dec 2026 10:09:19 +0000</pubDate>
      <link>https://dev.to/change03/refresh-token-rotation-with-reuse-detection-in-spring-boot-35-and-the-concurrency-trap-1h83</link>
      <guid>https://dev.to/change03/refresh-token-rotation-with-reuse-detection-in-spring-boot-35-and-the-concurrency-trap-1h83</guid>
      <description>&lt;p&gt;Every Spring Boot project I start needs the same thing: login, sessions, password reset, roles, and a way to log a user out of a device they no longer have. It's maybe two weeks of work, it's never the interesting part, and it's very easy to get subtly wrong.&lt;/p&gt;

&lt;p&gt;This is the design I settled on, with the parts that actually cost me debugging time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shape: short-lived JWT + opaque rotating refresh token
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Access token&lt;/strong&gt; — JWT, HS256, 15 minutes. Validated in a filter; the request never touches the database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refresh token&lt;/strong&gt; — a random 256-bit value, 7 days, returned to the client. The server stores &lt;strong&gt;only a SHA-256 hash&lt;/strong&gt; of it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last decision is the one worth arguing about. Hashing means you can't look a token up by value — you hash the presented token and match the hash. In exchange, a leaked &lt;code&gt;refresh_tokens&lt;/code&gt; table contains no usable credentials. For a table that only ever needs equality lookups, that trade is easy to take.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rotation and reuse detection
&lt;/h2&gt;

&lt;p&gt;Each refresh token row belongs to a &lt;strong&gt;family&lt;/strong&gt;: one login on one device.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Client presents refresh token R.&lt;/li&gt;
&lt;li&gt;Server hashes it, finds the row, checks family status.&lt;/li&gt;
&lt;li&gt;Row is marked consumed, a successor S is issued in the same family.&lt;/li&gt;
&lt;li&gt;Client gets a new access token and S.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now the useful property: if a &lt;strong&gt;consumed&lt;/strong&gt; token appears again, one of two things happened — the client is replaying a token it shouldn't have, or someone stole it. Either way the family is revoked and every token in that login stops working. The user re-authenticates; the attacker's copy is dead.&lt;/p&gt;

&lt;p&gt;That's the whole theft-detection story, and it runs on the database you already have. No Redis, no token blacklist to keep warm.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trap: parallel refreshes
&lt;/h2&gt;

&lt;p&gt;Two tabs, both with an expired access token, both fire a refresh at the same time. The first rotates R → S. The second presents R, which is now consumed. Reuse detection fires, the family dies, and the user gets thrown out mid-work.&lt;/p&gt;

&lt;p&gt;Two fixes, and you need both:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client side&lt;/strong&gt; — a single-flight refresh in the axios interceptor: the first 401 starts a refresh, every other request awaits the same promise, then all of them retry with the new token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;refreshing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interceptors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;401&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_retried&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_retried&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;refreshing&lt;/span&gt; &lt;span class="o"&gt;??=&lt;/span&gt; &lt;span class="nf"&gt;refreshAccessToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;refreshing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;refreshing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Authorization&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`Bearer ${token}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;api&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Server side&lt;/strong&gt; — a short grace window: a just-consumed token presented again within a few seconds returns the same successor instead of triggering reuse revocation. Without this, a mobile network retry alone can log someone out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things I'd tell my past self
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Never log the raw refresh token, even at debug level. Once you hash on write, a raw token in a log is the only copy that exists.&lt;/li&gt;
&lt;li&gt;Expired rows need a scheduled cleanup; otherwise the table becomes a growth chart of your users' sessions.&lt;/li&gt;
&lt;li&gt;Distinguish revocation reasons in the audit log: user logout, device revoked, password change, and reuse detection are different events, and support questions depend on telling them apart.&lt;/li&gt;
&lt;li&gt;Rate-limit the auth endpoints. Password reset and email verification are cheap to abuse.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Around the core
&lt;/h2&gt;

&lt;p&gt;The rest is ordinary work: BCrypt, account enable/disable, USER/ADMIN roles, admin user search with pagination, per-device session list, security audit log with IP and user agent, Flyway migrations for MySQL 8 (H2 for local), RFC 9457 problem responses, OpenAPI 3 + Swagger UI, Actuator probes, and JUnit 5 + MockMvc integration tests that walk register → verify → login → rotate → replay → revoke.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I packaged it
&lt;/h2&gt;

&lt;p&gt;I got tired of writing this per project, so I turned it into a starter kit with the React frontend that consumes it (protected routes, role guards, the interceptor above). It's a paid product, which is why it's linked here — &lt;a href="https://hwmich5.gumroad.com/l/springlaunch" rel="noopener noreferrer"&gt;https://hwmich5.gumroad.com/l/springlaunch&lt;/a&gt; — $29, one developer, commercial license.&lt;/p&gt;

&lt;p&gt;If you just want the backend, the dev profile runs with no Docker and no database install:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; backend &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; ./mvnw spring-boot:run
&lt;span class="c"&gt;# demo@springlaunch.dev / Demo1234!, admin@springlaunch.dev / Admin1234!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rotation model is the part I'd like criticism on. If your approach handles the concurrent-refresh race better, I'd like to read it.&lt;/p&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>security</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
