exec command runs TypeScript Playwright code against a live browser session. Use it to validate selectors, inspect page data, prototype individual steps, and experiment with interactions before encoding them in a workflow file.
Usage
- Single-line
- stdin (multi-line)
Pass the code as a quoted string argument:
Available globals
Inside the code you pass toexec, these variables are available without any imports:
| Global | Type | Description |
|---|---|---|
page | Page | The active Playwright page |
context | BrowserContext | The browser context for the session |
browser | Browser | The browser instance |
state | Record<string, unknown> | A mutable object scoped to this exec call |
fetch | typeof fetch | The global fetch function |
Buffer | typeof Buffer | Node.js Buffer |
networkLog | function | Read captured network log entries |
actionLog | function | Read captured action log entries |
Flags
The Playwright TypeScript code to execute, passed as the first positional argument. Pass
- to read from stdin.The session to execute against.
Target a specific page ID within the session. Use
npx libretto pages --session <name> to list page IDs when a popup or new tab is open.Enable ghost cursor and highlight visualization. Shows a visible cursor trace and element highlights as Playwright interacts with the page.
Return values
If the code returns a value,exec prints it to stdout:
- Strings are printed as-is.
- All other values are printed as pretty-printed JSON.
- If the code returns nothing (
undefined),execprintsExecuted successfully.
Rules
- Use
execfor focused inspection and short-lived interaction experiments. - Use
exec -(stdin) for complex multi-line scripts. - Validate a selector with
execbefore encoding it in a workflow file. - Use
execto prototype a single step, then move the working code into your.tsworkflow file.
Examples
snapshot
Use snapshot to identify selectors before running exec.
run & resume
Move validated exec code into a workflow file and run it end to end.