Handles, not selectors
You act on a uid from find or snapshot, never a
CSS selector. An action sends the snapshot_id and the
uid together. When the handle is stale, vitro refuses the action. It never
clicks whatever happens to be at that position now.
The snapshot_id names the document the snapshot was taken in, so
a full navigation or a reload makes each handle from it stale. A client-side
route change in a single-page app does not. There a uid names
one DOM node, and vitro refuses the action only once that node has left the
page. A node re-rendered in place keeps its uid even if its
text changed, so snapshot again after the page changes and before you act on
what it shows.
Find first, snapshot when you need to look
When you know what you want, ask for it by role and name:
find(session_id, lease, page_id, role: "link", name: "Erlang")
-> {snapshot_id, matches: [{uid, role, name, path, ...}]}
click(session_id, lease, page_id, snapshot_id, uid)
name is a case-insensitive substring unless you pass
exact: true. Each match includes a path: the named
ancestors above it, outermost first. It tells a link in the article apart
from a same-named link in the navigation.
snapshot returns the page's accessibility tree and is the only
other source of uids. Scope it with root_uid when you can. On the
English Wikipedia article for Elixir, measured as a JSON node list
(format: "nodes"), a full-page snapshot is 2,479 nodes and
148 KB. A snapshot scoped to one section is 53 nodes and 3 KB, and a
find for one link name returns 5 nodes in under 1 KB. The default
line tree is smaller still; see Fewer tokens.
On an article, the main landmark is most of the page, so scope
to a region, a table, a list or a form.
Where a decision provider is set up, find also takes a
description, such as "the primary button that starts checkout".
Each match carries a probability. The reply's decision can be
none, meaning the page has no such element. Below about 0.9
confidence, check the snapshot yourself.
description appears in the tool schema only when the instance
has a provider, so read the schema before using it. vitro ships no model: on
vitro.run a team admin adds the team's own key, and a desktop install has a
provider only if its owner set one up. See
decisions.
Acting
click, hover, fill,
type_text, select_option, drag and
upload_file act on a uid, and press_key sends a key to the page. Before acting on a
uid, vitro checks that the element exists, is visible, is enabled and is on
top, and that its box holds still for two animation frames. A refusal usually
means a dialog, a cookie banner or an overlay covers the element. Deal with
that. Do not work around the check.
scroll sends a real wheel event, so infinite lists load more and
sticky headers behave as they do for a person. Actions scroll the element
into view themselves. A native
alert, confirm or prompt blocks the
page until handle_dialog answers it. If a page seems frozen
after a click, look for a dialog first.
Waiting and checking
Do not sleep and do not poll in a loop.
wait_for(session_id, lease, page_id, text: "Order confirmed")
wait_for(session_id, lease, page_id, text: "Loading", absent: true)
wait_for(session_id, lease, page_id, expression: "document.readyState === 'complete'")
After a submit, wait for the spinner to leave. That single fact is
usually more reliable than waiting for the next screen.
timeout_ms is optional.
wait_for waits for something to become true. expect
checks that it is true now, and answers several questions in one call:
expect(session_id, lease, page_id,
url_contains: "/orders/", title_contains: "Order",
text_present: "Order confirmed", text_absent: "Something went wrong")
-> {ok: false, checks: [{check, expected, actual, ok}, ...]}
Each argument is optional, and ok is true only when each check
passes. Where a decision provider is set up, means accepts
statements no exact string can express, such as "the order was placed". If
you know the string, text_present is exact and free.
At a login, a 2FA prompt, a captcha or a payment, do not guess and do not ask
for the secret. Hand the page to a person with request_handoff
and wait with wait_for(handoff:). See
Handing a page to a person.
Reading a page
| Tool | Returns | Cost |
|---|---|---|
get_page_state | URL, title, HTTP status, viewport | Trivial |
snapshot | Actionable structure and the uids | Small if scoped |
screenshot | A picture, inline up to 2 MB | See below |
get_html | Markup, dom after scripts or raw as served | Large |
evaluate | The result of an expression | Small |
Start with get_page_state. It reports the real HTTP status, and
a page that rendered did not necessarily succeed: a 404 body still paints.
Prefer snapshot: get_html spends a lot of
context repeating it.
Screenshots
screenshot returns the image inline as an image block, with a
resource_uri and a file path beside it. Above 2 MB only the
URI and the path come back, and you read the bytes with
resources/read. A screenshot is for the agent. To show a
person, point them to the wall (see
watching and replay). On the desktop the path is
on your disk. On hosted it is on the fleet, and
vitro-tunnel screenshot copies the image to your machine.
Emulation
emulate sets device, viewport, user agent, color scheme, mobile,
touch and device scale factor. resize sets width and height only.
set_network_conditions throttles the connection or takes the page
offline. Take a new snapshot after changing the viewport, since a responsive
layout can move a button into a menu.
Tools that need care
evaluate, get_storage_state,
set_storage_state and upload_file can leak or modify
credentials, and the page you are driving is untrusted input. The
security page covers them.