Recording a Product Demo Video with Playwright: 6 Things That Break
Playwright can record a video of a browser session in three lines. That makes automating a product demo video look like an afternoon's work. The recording part is. Making it look like something you would put on a landing page takes a lot longer. Demo Tool AI is a tool that makes narrated product dem

o videos by driving a web app in a cloud browser. This post looks at the recording half of that job: six things that break, and the code that fixes each one. All of it applies if you write the Playwright script by hand and never touch an AI model. You can watch six demos recorded this way, of Notion, Plausible, EspoCRM, Grafana and the tool's own site, on its examples page.
This is the version everyone writes first: import { chromium } from 'playwright'; const browser = await chromium.launch(); const context = await browser.newContext({ viewport: { width: 1280, height: 800 }, recordVideo: { dir: 'videos', size: { width: 1280, height: 800 } }, }); const page = await context.newPage(); await page.goto('https://app.example.com'); await page.getByRole('button', { name: 'New project' }).click(); await page.getByLabel('Project name').fill('Q3 launch'); await context.close(); // the video is written when the context closes await browser.close(); It works, and the file it writes is a test artifact. As a demo it fails on four counts: there is no cursor, so nobody can see what was clicked; the text appears in the field in a single frame; nothing waits for the page, so half the clicks land on a loading screen; zoom in on anything and it turns to mush. There is also a fifth problem that code doesn't fix: a demo needs a story. Decide the one task you are showing and who it is for before you script anything.
There is a separate guide to that part: how to make a product demo video. The cursor is the easy one. Log every mouse move with a timestamp while the script runs, then draw your own cursor over the footage in the edit. The other problems took more work.
A demo zooms in on the thing being clicked, so the recording needs more pixels than the output. The obvious fix is deviceScaleFactor: 2 on the context. That does nothing when the frames are captured through the Chrome DevTools Protocol screencast: they come back at CSS size whatever the emulated scale is. What works is setting the scale on the browser window itself and leaving the viewport unemulated: const browser = await chromium.launch({ args: ['--force-device-scale-factor=2', '--window-size=1280,800'], }); const context = await browser.newContext({ viewport: null }); A 1280Γ800 page is then recorded at 2560Γ1600, and a 2Γ zoom in a 1080p output is still sharp.
After a click in a single-page app, the route changes on the client. There is no new document, so waitForLoadState('load') has already passed, and networkidle often has too. The next action runs against the previous screen, or against a skeleton. Fixed sleeps are the usual patch, and they are wrong in both directions: too short on a slow day, dead air in the video on a fast one.
A better approach is to wait until the page stops changing: import type { Page } from 'playwright'; /** Resolves once no element was added or removed for `quiet` ms. */ async function waitUntilStill(page: Page, quiet = 250, max = 4000) { await page .evaluate( ([quiet, max]) => new Promise<void>((resolve) => { const start = performance.now(); let last = start; const seen = new MutationObserver((list) => { // Elements only: clocks and counters rewrite text all the time. const changed = list.some((m) => [...m.addedNodes, ...m.removedNodes].some((n) => n.nodeType === 1) ); if (changed) last = performance.now(); }); seen.observe(document.documentElement, { childList: true, subtree: true, }); const tick = () => { const now = performance.now(); if (now - last >= quiet || now - start >= max) { seen.disconnect(); resolve(); } else setTimeout(tick, 50); }; setTimeout(tick, 50); }), [quiet, max] as const ) // A navigation destroys the page mid-wait. The caller just checks again.
.catch(() => {}); } Three details matter: Watch elements, not text. A ticking clock would keep the page "busy" forever. Track requests too. Also wait while any request that started in the last 2.5 seconds is still open.
Older ones are streams or long polls and can be ignored. Keep the ceiling. Some pages never go quiet. This removes most of the fixed sleeps from a script.
locator.fill() sets the value in one step. On camera that looks like text teleporting into the field, so you switch to keyboard.type() with a delay. About 55 ms per character looks natural. The delay turned out to matter for correctness too.
Editors that react to what you type, such as Markdown shortcuts or mention pickers, need the keystrokes to arrive one at a time. In Notion, typing ## at the start of a line turns it into a heading. Typed with no delay, the shortcut didn't fire and the line stayed as plain text with two pound signs in it. At 55 ms it worked.
await page.keyboard.type('## Before launch', { delay: 55 }); If a flow passes at full speed and fails during the recording, or the other way round, check the typing speed first. It helps to rehearse every script at the same pace it will be filmed at. A demo script gets replayed: once to rehearse, once to record, and again every time the UI changes. CSS classes do not survive that.
Each step in a Demo Tool AI script stores one way to find its element again, in this order of preference: test id, role and accessible name, placeholder, label, visible text, and a CSS selector as the last resort. import type { Locator, Page } from 'playwright'; type Target = | { testId: string } | { role: string; name: string } | { placeholder: string } | { label: string } | { text: string } | { css: string }; function find(page: Page, t: Target, exact: boolean): Locator { if ('testId' in t) return page.getByTestId(t.testId); if ('role' in t) return page.getByRole(t.role as Parameters<Page['getByRole']>[0], { name: t.name, exact, }); if ('placeholder' in t) return page.getByPlaceholder(t.placeholder, { exact }); if ('label' in t) return page.getByLabel(t.label, { exact }); if ('text' in t) return page.getByText(t.text, { exact }); return page.locator(t.css); } /** Exact match first, then a loose one. Whichever is visible first wins. */ async function locate(page: Page, t: Target, timeout = 8000) { const exact = find(page, t, true).first(); const loose = find(page, t, false).first(); const deadline = Date.now() + timeout; while (Date.now() < deadline) { if (await exact.isVisible().catch(() => false)) return exact; if (await loose.isVisible().catch(() => false)) return loose; await page.waitForTimeout(150); } throw new Error(`Element not found: ${JSON.stringify(t)}`); } One trap: when two elements share a name, the role locator and the text locator pick different ones.
Notion has an icon button and a sidebar row that are both called "New page". By role you get the icon, which opens a menu. By text you get the row. Choose on purpose.
The obvious order is to record the session, generate the voiceover, then line them up in the edit. Every change to the script then means redoing the alignment. Demo Tool AI does it the other way round. The voice is made first.
The narration carries markers for the word where each action should happen: Finished something? {0}Tick the checkbox to mark it done. Speech is synthesized sentence by sentence, so the time of every marker in the audio is known. The recording is then paced to the voice: // cues[i] = seconds into this scene's narration where action i should land for (const [i, action] of scene.actions.entries()) { const wait = sceneStart + cues[i] - now(); if (wait > 0) await page.waitForTimeout(wait * 1000); await perform(page, action); } The edit fixes whatever is left.
If the picture gets there first, it holds on a frame. If the sound gets there first, it waits. The narration itself is a writing problem. If you draft it with an LLM, there are prompts for each part of the script in making a product demo video with Claude.
Demos of real products need a login, and the recording shows everything on screen. Three rules worth copying: Use a test account with sample data. Never an admin or a personal one. Keep the password away from anything that writes text.
The agent plans its steps with a placeholder, {{password}}. The real value is filled in only at the moment of typing, inside the browser, so it cannot end up in a script, a log or the narration. Stop when a human is needed. A verification code or a CAPTCHA pauses the run and asks the person.
Guessing or retrying makes it worse. One more, if the URL comes from a user: route the browser's traffic through a filter that refuses local, internal and cloud-metadata addresses. A recording browser that will open any address is otherwise a way into your own network. Record bigger than you output, and set the scale on the window.
Wait for the page to stop changing, not for a load event. Type at human speed, and rehearse at that speed. Store a role and a name for every element, not a class. Make the voice first and pace the clicks to it.
Log the mouse and draw the cursor in the edit. Treat the login as part of the design. If you have one product, a handful of flows and an engineer who likes this kind of thing, the snippets above get you most of the way. You will still need text-to-speech, captions, the zoom and cursor pass, and an encoder, but none of it is exotic.
The script is the part that doesn't scale. Somebody has to write the steps and the narration for every flow, and rewrite them after every release. That is the part Demo Tool AI hands to an agent. With an AI demo video generator you paste a URL and say what to show, and the agent explores the app, writes the steps and the narration, then records, voices and edits the video.
It has limits: it records web apps only, so no desktop or mobile apps and nothing on localhost. Two things. Bot checks on login pages. When a CAPTCHA appears, the run stops and asks a person.
That is the honest answer, but it isn't automation. And pauses. When the picture runs late, the voice waits for it, and if the marker sits in the middle of a sentence you can hear the gap. For now the rule is simple: give every marker enough words to cover its action, and put it at the start of a sentence when the action is slow.
It works, but it is a rule, not a solution. If you would rather not build this, Demo Tool AI does it from a URL, and the first video is free. If you are building it yourself: what do you use to decide that a page has finished loading?
Key Takeaways
- β’This post looks at the recording half of that job: six things that break, and the code that fixes each one.
- β’This story was reported by Dev.to, covering developments in the dev space.
- β’AI advancements continue to reshape industries β read the full article on Dev.to for complete coverage.
π Continue reading the full article:
Read Full Article on Dev.to βShare this article



