Skip to content

Drivers

github-actions[bot] edited this page Nov 28, 2026 · 3 revisions

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.

The interface

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

The three protocols

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: ScrollAndStitch scrolls 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.

Failure handling

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.

Tests

  • Unit tests on recorded transcripts. replaytest replays JSONL transcripts from testdata/drivers/transcripts as a fake WebSocket endpoint for the CDP and BiDi drivers; Classic has its own replay test (replay_test.go), including TestElementScreenshotStitchesHTML for the element-screenshot path. No browser needed.
  • The conformance suite (TestConformance, browsers tag): the same scenarios against every engine family installed: the viewport is exactly 1280×800, extract.js gives the same geometry tree on a layout-neutral page, the scripts survive a page that overrides DOM methods, the full-page screenshot is docW × docH, element screenshots stitch a three-viewport page in place with its text painted (sessions with ElementScreenshotter), typed errors, and honest capabilities. Harnesses are named by family and protocol (blink (cdp), gecko (bidi), and the WebKit ones per OS). CI sets OMNISNAP_EXPECT_ENGINES per runner so a missing engine fails instead of skipping.

Where the code differs from the spec

Nothing found: the interface, the typed errors and the per-protocol table in the spec match the code.

Spec

specs/SPEC-drivers.md.

Source files

Clone this wiki locally