Repository navigation
Drivers
Role. One interface that capture uses for every engine, over three protocols spoken directly, with no proxy driver (no chromedriver, no geckodriver). Module drivers, package internal/drivers.
drivers.Session is one page (tab) in a running browser:
| Method | Does |
|---|---|
Navigate |
loads a URL and waits for the load event |
WaitNetworkIdle |
returns once no request has been in flight for idle, or ErrTimeout after limit
|
Evaluate |
runs an expression in the page, awaiting a Promise; the injected scripts return JSON strings |
AddPreloadScript |
runs a script before any page script on every navigation |
SetViewport |
sets the CSS viewport and returns the size actually obtained |
Screenshot |
a PNG of a clip in document coordinates |
FailedRequests |
the requests that failed since the last navigation, with the engine's error text (net::ERR_BLOCKED_BY_CLIENT…) |
Capabilities |
which optional features work; capture adapts to them |
Close |
closes the page |
A session may also implement ElementScreenshotter: ElementScreenshot returns the same clip through element screenshots of <html>. Classic sessions have it. Capture uses it only to retake a screenshot that holds none of the page's text (backlog B17, Capture pipeline).
Typed errors (errors.go): ErrTimeout, ErrNavigation, ErrScript (carrying the page's own message), ErrCrashed, ErrUnsupported. Capture's failure policy is written against these: a timeout or navigation error on the baseline makes the page unreachable, and ErrCrashed triggers the retry in a fresh browser.
Capabilities are reported honestly per session:
| CDP (Blink) | BiDi (Gecko) | Classic (WebKit) | |
|---|---|---|---|
Preload |
yes | yes | no |
NetworkIdle (and failed requests) |
yes | yes, once network events are subscribed | no |
FullPageScreenshot |
yes | yes, unless the build lacks origin: document
|
no: scroll and stitch |
NavigationErrors |
yes | yes | no: WebKitGTK shows its own error page and the command succeeds |
| Package | Engine | How |
|---|---|---|
cdp |
Blink: Chrome, Chromium, Edge, Brave, Opera, Vivaldi | Chrome DevTools Protocol over WebSocket. Auto-attaches child targets (Target.setAutoAttach, flattened), so a cross-site iframe or worker is in the same ordered event stream and network idle covers it; without that a cross-site iframe kept the page from ever going idle. Full-page screenshots with captureBeyondViewport (screenshot.go). |
bidi |
Gecko: Firefox, Firefox ESR | WebDriver BiDi, which Firefox serves on --remote-debugging-port (its CDP is gone since Firefox 129). Preload with script.addPreloadScript; network events only when subscribed; screenshots with origin: document. Firefox ESR builds that answer "unsupported operation" fall back to scroll-and-stitch (screenshot.go). |
classic |
WebKit: WebKitGTK, WPE, Safari | WebDriver Classic: plain HTTP, no driver library. No network events and no preload, so capture uses fixed settle waits. The viewport is set through the window rect with a measure-and-correct loop, because window chrome varies and resizing is asynchronous (session.go). Screenshots are viewport-only and stitched in Go. ElementScreenshot stitches GET /element/{id}/screenshot of <html> the same way: safaridriver's viewport screenshot captures the window, while its element screenshot is painted by the web process. On GitHub's macOS runner VMs, Safari 27.2's window drew no text but its element screenshots did (diagnosis run 46165615017). |
Two shared pieces:
-
wsrpc: the WebSocket JSON-RPC transport under CDP and BiDi. One goroutine owns the reads; calls are matched to replies by id; events go to subscribers by route and method. Each protocol plugs in a codec for its message format. -
stitch.go:ScrollAndStitchscrolls the page a viewport at a time, takes a shot of each, and stitches them in Go (fixed and sticky elements are hidden after the first frame so they appear once). The spike did this in JavaScript and it made WebKit captures about 2.5× slower.
A closed connection (the browser died) turns every pending and later call into ErrCrashed (wsrpc/conn.go), and Close into a no-op; CDP also watches Inspector.targetCrashed for a crashed page. A call past its context deadline is ErrTimeout.
-
Unit tests on recorded transcripts.
replaytestreplays JSONL transcripts fromtestdata/drivers/transcriptsas a fake WebSocket endpoint for the CDP and BiDi drivers; Classic has its own replay test (replay_test.go), includingTestElementScreenshotStitchesHTMLfor the element-screenshot path. No browser needed. -
The conformance suite (
TestConformance,browserstag): the same scenarios against every engine family installed: the viewport is exactly 1280×800,extract.jsgives the same geometry tree on a layout-neutral page, the scripts survive a page that overrides DOM methods, the full-page screenshot isdocW × docH, element screenshots stitch a three-viewport page in place with its text painted (sessions withElementScreenshotter), typed errors, and honest capabilities. Harnesses are named by family and protocol (blink (cdp),gecko (bidi), and the WebKit ones per OS). CI setsOMNISNAP_EXPECT_ENGINESper runner so a missing engine fails instead of skipping.
Nothing found: the interface, the typed errors and the per-protocol table in the spec match the code.
-
internal/drivers/driver.go:SessionandCapabilities: the one interface capture uses -
internal/drivers/errors.go: the typed errors capture's failure policy branches on -
internal/drivers/stitch.go: scroll-and-stitch full-page screenshots -
internal/drivers/scale.go:ScalePNG -
internal/drivers/wsrpc/conn.go: the websocket transport CDP and BiDi share -
internal/drivers/cdp/conn.go: CDP's message codec -
internal/drivers/cdp/session.go: the CDP session -
internal/drivers/cdp/targets.go: the child targets attached to a session -
internal/drivers/cdp/network.go: CDP network tracking -
internal/drivers/cdp/preload.go: CDP preload scripts -
internal/drivers/cdp/screenshot.go: CDP screenshots -
internal/drivers/bidi/conn.go: BiDi's message codec -
internal/drivers/bidi/session.go: the BiDi session -
internal/drivers/bidi/network.go: BiDi network tracking -
internal/drivers/bidi/preload.go: BiDi preload scripts -
internal/drivers/bidi/remote.go: BiDi remote values to JSON -
internal/drivers/bidi/screenshot.go: BiDi screenshots -
internal/drivers/classic/client.go: the WebDriver Classic HTTP client -
internal/drivers/classic/session.go: the Classic session -
internal/drivers/replaytest/replay.go: replays recorded transcripts for the unit tests
This wiki is published from docs/internals/ on main. Edits made here are overwritten on the next publish: change the pages there, in a pull request. The specs are normative; where a page and a spec disagree, the page says what the code does.
Start
Contracts
Capture side
Analysis side
Output
Quality