zoom
Zoom into a UI element during a test step for the demo video.
What it does
zoom(locator, level) measures the element's center as viewport-relative coordinates and emits a marker-prefixed test.step() directly into the Playwright trace zip. During video generation, subtitlesFromTrace() picks up these markers automatically and attaches the zoom to the narration playing at the call site, or else to the next one - so zoom() can come just before the narrate() it illustrates. With a voiceover, "playing" means its audio: a narration spoken over a wait does not keep a zoom set after it has finished. No separate .enrichZoomFromReport() call needed.
Test steps scope this:
Where zoom() runs | Goes to |
|---|---|
A top-level test.step() that narrates | Only that step's narrations. After its last one, it stays with that line while the cue is open, else it is dropped with a log line. |
| The test body, or a step without narration (a page-object helper) | Any narration: a helper step between two narrating steps zooms into the next one. |
The zoom starts at the helper's call site (not at the start of the surrounding narration) and runs until the end of the subtitle, so the camera kicks in only once the target is visible. Called before its narration, it starts with it.
Usage
import { zoom } from 'playwright-recast'
const sidebar = page.locator('.sidebar-panel')
await zoom(sidebar, 1.3)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
locator | Locator | (required) | Playwright Locator pointing to the element to zoom into |
level | number | 1.5 | Zoom level: 1.0 = no zoom, 2.0 = 2x closer |
opts.text | string | true | - | Zoom onto text inside the element instead of its box: a substring of its text content (it may span child elements), or true for all of it |
opts.align | 'center' | 'start' | 'center' | 'start' keeps the start of a target wider than the zoomed frame in view |
opts.hold | boolean | false | Keep the zoom through the pause after its narration, until the next narration starts |
Holding a zoom through a pause
A zoom ends with its narration. hold: true keeps it through the silence after that narration, until the next one starts. A pause meant for watching the target stays zoomed, and consecutive lines about one area hold the camera instead of zooming out and back in between them:
await zoom(sidebar, 1.6, { hold: true })
await narrate('The sidebar lists every project.')
await waitForNarration()
await page.waitForTimeout(1500) // still zoomed
await zoom(sidebar, 1.6, { hold: true })
await narrate('A click opens one.')The hold ends one transition before the next narration, so the camera arrives at that narration's zoom, or at full frame, as it starts. With autoZoom({ containInCue: true }) it ends as the next narration starts, after zooming out, since that mode keeps each zoom's transitions inside its own window.
Zooming onto text
A block element's box spans its container even when its text is short - a line of code spans the whole editor. text zooms onto the text itself, and align: 'start' keeps a long line's beginning in frame instead of centring it:
const line = page.frameLocator('iframe').locator('.cm-line').filter({ hasText: 'MATCH' })
await zoom(line, 1.8, { text: true, align: 'start' })Text is measured inside the element's own frame and placed in page space, so it works inside iframes.
How it works
- Gets the element's bounding box via
locator.boundingBox() - Computes the center as viewport-relative fractions (0.0--1.0)
- Writes a marker-prefixed
test.step()carrying{ x, y, level }into the trace zip (and pushes a matchingzoomannotation ontotestInfofor legacy reporters) subtitlesFromTrace()finds the marker step at parse time and attaches the zoom to the subtitle covering the call site, or else the next one, scoped by test step as above- The renderer applies the crop-and-scale zoom from the call-site time until the end of that subtitle, with smooth easing transitions
Zoom coordinates
The stored coordinates use viewport-relative fractions:
| Field | Range | Description |
|---|---|---|
x | 0.0--1.0 | Center X (0 = left edge, 1 = right edge) |
y | 0.0--1.0 | Center Y (0 = top edge, 1 = bottom edge) |
level | 1.0+ | Zoom factor (1.0 = no zoom) |
Example in a BDD step
import { When } from './fixtures'
import { narrate, zoom } from 'playwright-recast'
When('the user opens the sidebar', async ({ page }, docString?: string) => {
narrate(docString)
const sidebar = page.locator('.sidebar-panel')
await zoom(sidebar, 1.3)
await sidebar.click()
})When the user opens the sidebar
"""
Let's take a closer look at the sidebar navigation.
"""The video will zoom into the sidebar element during this step, then smoothly zoom back out when the next step begins.