beetlebug▾
/

Why ztk?

This page explains, with proof, why you might pick ztk. Each number and picture on it comes from the build of this site: docs/why/measure.tcl runs in ztk --headless each time the site builds, so the page cannot go out of date. The numbers below come from the build ReleaseFast, x86_64-linux-gnu.

An app that an AI agent can test fully, in seconds

A coding agent changes code, runs it, and checks the result, many times an hour. For a user interface, the check is the hard part: most toolkits need a display, a browser, or a device before a test can see a window. ztk was built so that an agent, or a person, can check a window like any other output of a program.

A window in one process, with no display

ztk --headless draws the real widgets with the CPU renderer into a picture. It needs no window server, no browser, and no device.

A sign-up form with a title, two entries, a check button, and a button

ztk --headless --exit-after-frames 1 --screenshot form.png docs/why/form.tcl

Each variation as a picture

One loop makes eight kinds of widget, each in five states, and takes a picture of each with ztk::snapshot. The 40 pictures took 792.6 ms in all. The pages of the widget reference are made the same way.

Forty widgets in a grid: buttons, check buttons, radio buttons, entries, combo boxes, scales, and progress bars, each in five states

foreach w $cells {
    set png [binary decode base64 [ztk::snapshot $w]]
}

The widget tree as data

ztk::tree gives each widget with its class, its box, and whether it is shown. The form above has 8 widgets; these are the first lines:

. Ztk 0 0 299 211 1
.title TLabel 16 16 191 27 1
.nl TLabel 16 61 37 17 1
.name TEntry 85 55 198 29 1
.el TLabel 16 98 37 17 1
.email TEntry 85 92 198 29 1

An agent can read the layout as a list and find a widget of zero size, a widget that is not shown, or a text that does not fit. It does not need a picture for that. docs/why/layout-check.tcl is such a check in 30 lines. Here is its output on a window that is too short for its two buttons:

.t.load (TButton): 220x14, asks for 121x31

The same check found a real fault while this page was written. In the vector example, at its own window size, the buttons Save and Load of the side panel had no room, and Export SVG lost the bottom of its border. The screenshot showed only that something was missing; the tree showed which widgets, and why. The fix is in ztk#769.

Input as script

ztk::inject sends pointer, key, and text events through the same hit test and focus as a real backend. This part of measure.tcl clicks the Name entry, types a name, and clicks Sign up:

lassign [center .name] x y
ztk::inject . press 1 $x $y
ztk::inject . release 1 $x $y
ztk::inject . text "Ada"
lassign [center .go] x y
ztk::inject . press 1 $x $y
ztk::inject . release 1 $x $y

The command of the button got the name Ada. A drag works the same way: the test dnd: the frame in the middle of a drag shows the dragged rows of a list at the pointer in src/tests/dnd.zig stops a drag halfway and checks the pixels under the pointer. See Drive a window.

The same pixels each time

The 7 runs above wrote the same picture each time: yes (SHA-256 26e81e4d357887e2…). So a picture compare needs no retry and no tolerance on one machine.

The limits are real:

Style diagnostics

ztk::style diagnostics gives the problems that the parser found in a style sheet, with their lines. For a sheet with a typo, button { colr: red; }:

2: unknown property colr

Today the diagnostics name an unknown property. A value that a property cannot take, such as padding: wide, has no diagnostic yet (ztk#771).

Accessibility as data

ztk draws its own widgets, so it makes the accessibility tree itself: a role, a name, a state, and a box for each widget. The backends give the tree to UI Automation on Windows, AT-SPI on Linux, and the accessibility API of macOS, and the web build gives it as JSON. A Tcl script cannot read the tree yet (ztk#770). Until it can, a check of names and roles reads tree of the web build from JavaScript.

One script on each platform

The same Tcl script runs in ztk --headless, in a desktop window, in the web build, and in an app on a phone. So the app that the headless check tested is the app that ships, with the same widgets and the same layout.

The loop of an agent

  1. Change the code of the app.
  2. Run it headless and write a picture: 87 ms.
  3. Read ztk::tree and run the layout check: in the same run.
  4. Compare the picture with the last one (ztk::debug mark and ztk::debug diff, see Visual diffs).
  5. Fix and run again.

The skill for agents has this loop with the commands.

Compared with the usual ways

ztk --headless A browser with a driver (Playwright) A native UI test framework (XCUITest, Espresso) A device farm (Firebase Test Lab)
Needs a display or a device No No: the browser runs headless (docs) Yes: a Simulator, an emulator, or a device (XCUITest, Espresso) Yes: devices in a data center (docs)
The test runs in the process of the app Yes No: the driver talks to the browser XCUITest: no, it drives the app from another process. Espresso: yes No
What the test reads The widget tree, the pictures, and the state of the script The DOM and the accessibility tree (ARIA snapshots) The accessibility elements (XCUITest) or the view hierarchy (Espresso) The results of the test framework on each device
Start, draw, and picture 87 ms, measured above Not measured here Not measured here Not measured here

This page measures only ztk. The other columns say what each project documents, with a link to its docs.