Trame

cdp: a client of the DevTools protocol for tools

src/cdp (#import "trame:cdp", or cdp :: #import "<toolkit>/src/cdp") drives a toolkit window over the Chrome DevTools Protocol, or a browser's pages. A window serves the protocol when it runs with TRAME_DEVTOOLS=<port> (devtools.md). The package is for tools and tests: trame cdp, trame screenshots, trame timeline_test, site.check, and the tools of the programs built on the toolkit. It names no product.

One connection serves a whole run. A call waits for its answer. The events that come before the answer are handled on the way: the frames of a screencast are written as they come.

A window

Formula
c, ok := cdp.connect(9222)                  // the first target of the port: a window
if !ok { return }                           // nothing listens there yet: try again later
defer cdp.close(&c)
cdp.wait_for(&c, "Lazy column", 5 * time.Second)
cdp.click_text(&c, "Lazy column")
cdp.key(&c, "ctrl+f")
cdp.type_text(&c, "trame")
cdp.wait_idle(&c, 5 * time.Second)
cdp.shot(&c, "out.png")
  • connect_wait(port, limit, program, step): connect, tried every step (CONNECT_POLL, 50 ms, by default) until it answers, at most limit. A tool that measures a program's first screen gives a finer step, since the wait adds up to one step to what it measures. program (optional): the process that serves the port; once it has ended, the wait fails at once. A tool that starts its program uses it with toolbox.free_port (toolbox.md, "A program driven by its tool").
  • connect(port): the WebSocket of the first target of /json/list. open(port, path): the WebSocket at a path (a browser's own, /devtools/browser/..., whose pages are sessions of one connection: set c.session). targets(port) lists them. The WebSocket is trame:http's (http.md): WinHTTP on Windows; on Linux a TCP connection of its own to 127.0.0.1, so no libcurl is needed (a tool's Nix shell without curl drives a window).
  • set_read_limit(c, limit): how long a call waits for its answer (READ_LIMIT, 75 s, unless told). A call whose answer is late fails; the connection stays, and the late answer is dropped when it comes.
  • call(c, method, params): params is a JSON object as text. The result is in the temporary allocator. A failure prints "method: message (code)" once and is kept in c.failure; c.quiet = true keeps it silent for a caller that says it itself.
  • send(c, method, params): a call nobody waits for (an acknowledgement).
  • find, find_name, has: the boxes whose text holds a text, or with a name (Toolkit.find), as Box values.
  • wait_for, wait_name (with gone), wait_idle: the window waits; nothing in the tool sleeps. wait_name's text asks for the box of that name whose text holds it (a cell that shows its value). wait_idle ends after three builds in a row with no animation and no posted call.
  • wait_still(c, still, limit): until the window's picture has not changed for still, at most limit. Use it for what the box tree does not show and the window does not count as moving: a terminal's grid, an icon decoded on a chore. It compares the PNG bytes of successive captures. !is_still: the picture still changed at the limit.
  • A tool never sleeps a fixed delay: it waits for one of these conditions, with a bound. What no wait can see (a history of samples that must grow) is the program's to make ready, for example at once in its demo mode.
  • click_name, click_text, type_text, key: input. A key is a name: Enter, Tab, Escape (esc), ArrowDown (down), PageUp, F3, a, ctrl+f, shift+Tab. key_parts gives the key, its modifiers and the text it types.
  • demo_state(c, name): enters the program's demo state name (Toolkit.demoState), a screen that needs an account, for a picture; wait for its box or for rest before the picture. An unknown name fails, and c.failure lists the names. demo_states(c) gives the names (devtools.md, "Demo states").
  • debug_set(c, path, value), debug_press(c, path), debug_panels(c): the program's debug panels (Toolkit.debug*, debug.md): a value set by its path as text ("1, 2, 3"), an act pressed, every value listed. In a script: debug-set <path> <value> and debug-press <path>.
  • screenshot (the PNG's bytes) and shot (a file), optionally a Clip in CSS pixels. window_size.
  • json.quote(text) of trame:json (json.md) writes a string as JSON for a call's parameters. number, integer and str read a JSON value. stringify writes a value as JSON.stringify does, whole numbers without .0.

Screencasts

Formula
sc := cdp.Screencast{dir = "frames"}
defer cdp.screencast_destroy(&sc)
cdp.screencast_start(&c, &sc, 0)     // 0: each frame drawn
// ... calls, or cdp.pump(&c, 100 * time.Millisecond) while nothing is asked
cdp.screencast_stop(&c)
// sc.names: frame-000000.png..., sc.times: each frame's time in seconds

Scripts

A script is a command per line, as the window's recorder writes it (Toolkit.startRecording). words(line) splits a line (JSON's quoted strings are one word). options(words) reads --name value and --name alone. act(c, command, args) runs an action: click, move, down, up, wheel, key, type, wait, wait-for, idle, press, demo <name>. read_script(file) reads a script, or the actions of a timeline.json with their times.

Timelines

record_timeline(c, {dir, stop, injected}) records what a person does until stop is set (from any thread). play_timeline(c, {source, dir, against, realtime, max_diff}) runs a script or a timeline.json again. Each takes a picture after each action and writes the folder that devtools.md describes ("Timelines"). Each returns the number of steps, those that differ from the reference, and the actions.

Pictures

png_decode and png_read give RGBA8 (Picture) whatever the PNG's format. png_encode and png_write write one, its rows filtered as fits each best (png.encode's .Small: png.md). compare(a, b, delta) gives the share of pixels that differ by more than delta of 255 on a colour channel, and a picture of where (red). shrink(p, width) makes a picture width pixels wide with a box filter (each pixel the mean of the area it covers).

Tests

src/cdp/cdp_test.fm tests what needs no window: the answers, the keys, the script's words and options, the PNG round trip, the comparison and the shrink, a script and a timeline read. trame timeline_test runs the timelines against examples/whatsnew --headless over a real socket.

Edit this page on GitHub