Roc Solid
Markup in Roc · pages on a native server · components in WebAssembly

The ecosystem · packages/headless

Chromium, driven from Roc

A program in Roc starts Chromium's headless shell, opens tabs, and uses a page as a test or a renderer does: it goes to a URL, runs JavaScript, waits for something to be true, clicks, types, presses keys and takes photographs.

It speaks Chromium's DevTools protocol over the CLI platform's WebSocket. Nothing of Playwright's runs, and no Node. rocx render films videos with it.

Installing it#

The package names the CLI platform (cli/), the one rocx itself runs on, so a program on that platform uses it. An app's server doesn't: an app's tests drive a browser from Node, with the testing package.

main.roc's header
app [main!] { pf: platform "../cli/platform/main.roc", headless: "../packages/headless/main.roc" }

It needs Chromium's headless shell: $CHROME, or the newest one Playwright keeps in its cache. npx playwright install --only-shell chromium downloads one.

A first program#

Count once on a running app, and photograph the page:

main.roc
import pf.File
import headless.Chrome
import headless.Tab

main! = |_| {
	chrome = Chrome.launch!(Chrome.find!({})?)?
	tab = Chrome.open!(chrome)?
	photographed = counted!(tab)
	Chrome.close!(chrome)
	File.write_bytes!("counter.png", photographed?)?
	Ok({})
}

counted! : Tab => Try(List(U8), [Failed(Str)])
counted! = |tab| {
	Tab.goto!(tab, "http://localhost:8091/", 10_000)?
	Tab.click!(tab, "button.add")?
	Tab.wait!(tab, "document.querySelector('output').textContent === '1'", 2_000)?
	Tab.viewport!(tab, { width: 1280, height: 720, scale: 2 })?
	Tab.screenshot!(tab, Viewport)
}

Close the browser on every path. One the program leaves running is stopped when the program ends, but its profile stays in the temporary directory. An error main! doesn't handle is printed, and the program exits 1.

The browser#

FunctionWhat it does
Chrome.find!({})The headless shell's path: $CHROME, else Playwright's newest. NoChrome(how) says how to get one.
Chrome.launch!(binary)Starts it in a new profile in the system's temporary directory. It picks its own DevTools port, read from the profile. CantLaunch(why) with what it said.
Chrome.open!(chrome)A new tab at about:blank, on a socket of its own. Tabs share the profile's cookies.
Chrome.close!(chrome)Asks it to close, then makes it, and deletes its profile.

It runs as a laptop's browser would, whatever the machine's settings: motion as a page gets it when it asks for none, scrollbars that take no room, and no sandbox (as Playwright runs it, since a container's root may lack the namespaces it needs).

A tab#

A function that can fail fails with Failed(why): the protocol method and the browser's message, what the page threw, or what timed out. Clicks and keys go through the browser's input pipeline, as a person's would.

FunctionWhat it does
Tab.goto!(tab, url, ms)Goes to url and waits up to ms for its document to load. Fails with the browser's error when nothing serves it (net::ERR_CONNECTION_REFUSED).
Tab.eval!(tab, expression)The JavaScript's value as JSON (packages/tools' JsonValue). A promise is awaited; undefined is Null. Fails with what it threw.
Tab.wait!(tab, expression, ms)Waits until the expression is true, looking every 25 ms.
Tab.before_each_document!(tab, source)Runs source in every document the tab loads from then on, before the page's own scripts: a clock to freeze, a listener.
Tab.click!(tab, selector), Tab.click_at!(tab, x, y)A real click in the middle of what the CSS selector finds (scrolled into view first), or at a point.
Tab.type!(tab, selector, text)Focuses the element and inserts text at the caret, with an input event.
Tab.press!(tab, key)A key pressed and released: Enter (which submits a form), Escape, Tab, Backspace, Delete, Home, End, the arrows, " ", a letter or a digit, which types itself (its code a keyboard's: KeyA, Digit1).
Tab.viewport!(tab, { width, height, scale })Lays the page out at that size in CSS pixels, scale device pixels each.
Tab.screenshot!(tab, Viewport), Tab.screenshot!(tab, Clip({ x, y, width, height }))A PNG of the viewport, or of a box of the page (from the document's top left, past the viewport too), at the device's scale.
Tab.errors!(tab)What the page threw since this was last asked, oldest first.
Tab.call!(tab, method, params)Any method of the DevTools protocol, its params a JsonValue: its result.
Tab.close!(tab)Closes the tab.

The CLI platform's WebSocket keeps each message until the program takes it. A tab's events wait there between calls, so errors! reads the page's exceptions whenever it is asked, and no loop is needed.

Who uses it#

rocx render films a video package player into MP4, WebM or PNGs: it takes over the page's clock with before_each_document!, then moves it a frame at a time and photographs the player's stage. The package's own checks are a second user: a Roc program on a page made for them.

Its tests#

node --test 'packages/headless/test/*.test.mjs'

This runs the WebSocket's unit tests (cli/host/src/websocket.rs), then builds test/main.roc and runs it in the headless shell. Without a headless shell the browser checks skip. In CI, or with ROCX_BROWSERS=required, a missing one is an error.

Not yet#

Each is sketched in the package's PROPOSALS.md.