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
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 everystep(CONNECT_POLL, 50 ms, by default) until it answers, at mostlimit. A tool that measures a program's first screen gives a finerstep, 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 withtoolbox.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: setc.session).targets(port)lists them. The WebSocket istrame: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):paramsis a JSON object as text. The result is in the temporary allocator. A failure prints "method: message (code)" once and is kept inc.failure;c.quiet = truekeeps 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), asBoxvalues.wait_for,wait_name(withgone),wait_idle: the window waits; nothing in the tool sleeps.wait_name'stextasks for the box of that name whose text holds it (a cell that shows its value).wait_idleends 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 forstill, at mostlimit. 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_partsgives the key, its modifiers and the text it types.demo_state(c, name): enters the program's demo statename(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, andc.failurelists 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>anddebug-press <path>.screenshot(the PNG's bytes) andshot(a file), optionally aClipin CSS pixels.window_size.json.quote(text)oftrame:json(json.md) writes a string as JSON for a call's parameters.number,integerandstrread a JSON value.stringifywrites a value asJSON.stringifydoes, whole numbers without.0.
Screencasts
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 secondsScripts
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.