Automation: scenarios and the API
How a scenario does in a profile what a person would, and how scripts on this Mac drive profiles through the local API.
Scenarios
A scenario is JavaScript that does in a profile what a person would: opens pages, clicks, types, waits, scrolls.
- Open File › Scenarios (⇧⌘E). The window opens while the vault is unlocked.
- Press + (New Scenario), name the scenario and write its code. The editor is plain monospaced text: no smart quotes or replacements, which would break JavaScript. Beside the code, the Commands panel: click a command to add it on a line of its own at the scenario’s end.
- At the foot, choose the Profile and press Run (⌘R). While the scenario runs, «Running…» shows beside a Stop button.
// Shop search: step by step, as a person would.
await page.goto('https://shop.example/');
await page.click('text=Catalog');
await page.type('#q', 'headphones');
await page.press('Enter');
await page.waitFor('.results');
await page.scroll(600);
log('Found:', await page.count('.item'));A scenario runs in a JavaScriptCore of its own, on a thread of its own - never inside the page. It has page (the commands below, each returning a promise), log(…) for the log, sleep(ms) and params - the run’s parameters, read-only. Commands act one at a time, in the order called, even when the script didn’t await the one before.
A command that fails rejects its promise with the reason - try/catch can catch it; one left uncaught ends the run. Stop ends the run at once: a wait - sleep, waitFor, a page still loading - ends, and a pressed mouse button or key is let go.
Scenarios are kept in the vault, under a key of their own, on this Mac only: this version does not sync them.
Commands
The names follow Playwright’s, so a script moves over quickly; full compatibility is not promised.
| Command | What it does |
|---|---|
goto(url) | Loads the address as the address bar does and waits for the page |
back(), forward(), reload() | As the toolbar’s buttons; wait for the page |
waitFor(selector) | Waits until an element appears, shows, hides or goes; a timeout is an error |
click(selector) | Waits for the element, scrolls it into view with the wheel, moves the pointer to a point where it is on top, presses and lets go. An element covered there is an error naming what covers it |
hover(selector) | Moves the pointer over it |
type(selector, text) | Clicks the field and types key after key, with Shift around capitals. A character the keyboard layout has no key for is an error |
press('Enter') | A key or a combination: 'Shift+Tab', 'Meta+a' |
scroll(n) | Wheel scrolling where the pointer is |
textContent, innerText, inputValue, getAttribute, count, isVisible, url, title | Read the page |
evaluate(function) | Runs code in the scenario’s own world, which the page cannot see. In the page’s world only when asked for by name - and then the page can see it |
screenshot() | A picture of the view or of an element |
pause(…) | A pause of random length |
Selectors are CSS, or text=…: the smallest visible element whose text holds it.
As a person
A scenario clicks, types and scrolls with the app’s own pointer, wheel and keyboard. There is no debugging protocol, so a page finds no navigator.webdriver (it is false), no protocol objects and no untrusted events.
- The input events are real:
isTrustedistrue. - Before every press the pointer moves to the element, and the button is held, as a finger holds it.
- Keys go one at a time, with pauses and Shift around capitals; a double click is a real double click.
- The scenario reads the page in its own world: the page’s scripts see neither its commands nor what it reads.
Lumen’s self-test holds it to this: the commands run on its pages are checked by the same rules, and the browser’s fingerprint and the page’s globals are the same after a run as before it.
The run’s log
Under the code is the log: each line with its time, commands plain (goto …, click …, press Enter), the scenario’s own lines (log(…)) brighter, errors and the outcome in their colours: Finished, Failed, Stopped.
The log never holds what was typed: only how many characters - type 10 characters into #q.
The local API
A script on this Mac - curl, Python, anything - can do with a profile what the Profiles window does: list the profiles, start one with its window or without, open a page in it, read and write its cookies, stop it, run a scenario.
Turn it on in Settings › Automation API: the switch «Let scripts on this Mac drive the profiles». There too are the Port (50777 by default) and the Token - with Show, Copy and New Token. While the API is on, «Status» reads «Listening on 127.0.0.1:50777». It listens only while the vault is unlocked.
It can be reached only from this Mac (127.0.0.1), only with the token, and never from a web page: a request carrying Origin or a Sec-Fetch-* header is refused before the token is looked at. Anyone who has the token can start your profiles and read their cookies: keep it like a password.
Answers show a profile’s proxy without its username or password. Cookies come only from their own request.
Requests
Every request carries Authorization: Bearer <token>. A profile in the path is its id or its exact name; below, … stands for /v1/profiles/{profile}.
| Request | What it does |
|---|---|
GET /v1/profiles | Every profile |
GET … | One profile |
POST …/start | Starts it; {"window": false} - without a window |
POST …/stop | Stops it; its data is sealed before the answer |
POST …/open | A new window of the running profile on {"url": "https://…"} |
GET …/cookies | All its cookies: playwright, netscape or extension |
POST …/cookies | Imports a cookie file |
POST …/scenario | Runs a scenario: the text {"source": "…"} or a saved one {"scenario": "Name"} |
GET …/scenario | The last run: whether it runs, its outcome and its log’s lines |
POST …/scenario/stop | Stops the scenario |
export PB_API_TOKEN='…' # Settings › Automation API › Copy
curl -s -H "Authorization: Bearer $PB_API_TOKEN" localhost:50777/v1/profiles
curl -s -X POST -H "Authorization: Bearer $PB_API_TOKEN" -d '{"window": false}' \
localhost:50777/v1/profiles/Northwind/start
curl -s -X POST -H "Authorization: Bearer $PB_API_TOKEN" -d '{"scenario": "Shop search"}' \
localhost:50777/v1/profiles/Northwind/scenarioErrors come as {"error": "<code>", "message": "<text>"}: no token or a wrong one - 401, the profile not running - 409 not_running, a scenario already running - 409 busy.
Which plans
Scenarios and the API are part of the Professional and Team plans. On Personal you can write scenarios but not run them, and the API answers any request with 403 automation_not_in_plan. If a plan without automation begins in the middle of a run, the step under way finishes, no other starts, and the log says why.
Found a mistake, or is something missing? Write to us.