Skip to navigation

screenshot

The screenshot action captures a PNG image of the current browser viewport or a specific element. It can also perform visual regression testing by comparing the captured image against a previously saved reference image. Reference images can be local files or remote URLs, enabling teams to store reference images in cloud storage like S3.

You can specify the screenshot action in several ways:

  • Boolean Shorthand: Set screenshot: true to capture the full viewport with default settings, saved as <stepId>.png in outputDir. Set screenshot: false to skip the step.
  • String Shorthand: Provide a file path directly as the value for the screenshot key (for example, screenshot: "my_screenshot.png"). This saves the full viewport screenshot to the specified path. A relative path resolves against the spec file. The path can also be a $VAR reference loaded with loadVariables.
  • Object Format: Use an object for more control over the path, visual comparison, and cropping:
    • path: (Optional) The file path to save the screenshot to, or an http(s):// URL to a remote PNG image. It can include directories. A relative path resolves against directory if set, else against the spec file. If omitted, the file is <stepId>.png, in directory if set, else in outputDir. Given a URL, Doc Detective downloads the remote image and uses it as a read-only reference for comparison. It writes the new capture to the run folder instead of overwriting the remote image.
    • directory: (Optional) The directory where the screenshot will be saved. A relative directory resolves against the spec file. With no spec file, such as a step passed to the JavaScript getRunner() API, it resolves against outputDir. If a local path or a directory references an environment variable that isn’t set, such as $SHOTS_DIR, the step fails. The message names whichever of path or directory holds the variable. For example: Couldn't save the screenshot. `directory` (`$SHOTS_DIR`) has a variable that isn't set.
    • maxVariation: (Optional) The maximum acceptable visual difference (0 to 1) when comparing against a reference image. If the variation exceeds this threshold, the step returns WARNING. Comparison runs automatically when a reference image exists at path. Set this field to tune the tolerance (default: 0.05).
    • overwrite: (Optional) Controls when to overwrite the reference image. Ignored when path is a URL (remote storage is never mutated). Options:
      • "false": Never overwrite the reference image.
      • "true": Always overwrite the reference image with the new capture.
      • "aboveVariation": Overwrite only if the visual difference exceeds maxVariation (default).
    • crop: (Optional) Crop the screenshot to a specific element. Provide a CSS/XPath selector string (for example, "#myElement") or an object with at least one element-finding field:
      • selector: (Optional, one of several element-finding fields) The CSS selector or XPath selector of the element to crop to.
      • elementText: (Optional) Display text of the element. Doc Detective matches it against the element’s full visible text with whitespace normalized. Wrap the value in slashes (/pattern/) to match a substring by regular expression.
      • elementId: (Optional) ID attribute of the element. Supports exact match or a regex pattern using /pattern/ syntax.
      • elementTestId: (Optional) data-testid attribute of the element. Supports exact match or a regex pattern using /pattern/ syntax.
      • elementClass: (Optional) Class, or array of classes, the element must have. Each class supports exact match or a /pattern/ regex. The element must have all specified classes.
      • elementAttribute: (Optional) Object of attribute key-value pairs the element must have. Values can be strings (supporting /pattern/ regex), numbers, or booleans. Boolean true matches attribute presence, and false matches absence.
      • elementAria: (Optional) Computed accessible name of the element per the ARIA specification. Supports exact match or a /pattern/ regex.
      • padding: (Optional) Add padding around the element before cropping. Can be a single number (all sides) or an object { top, right, bottom, left }.
    • surface: (Optional) The browser session, window, or tab to capture, such as { "browser": "chrome", "tab": "cart" }. It can also be the native app surface to capture, such as { "app": "notepad" } or a specific app window { "app": "notepad", "window": -1 }. On macOS the capture is that window element, and on Windows the switched window. Omit it to capture the active surface, the most recently opened, focused, or explicitly targeted one, whether a browser tab or an app window. Specifying a surface switches the active surface for the steps that follow. See Test across multiple tabs, windows, and browsers, Target an app surface, and App windows.
    • annotations: (Optional) A list of visual annotations to draw onto the screenshot. See Annotations.

Where files go: a path you write resolves against the spec file you wrote it in. A spec loaded from a URL (--input https://…) has no folder of its own, so its relative paths resolve against outputDir. A file Doc Detective names, such as <stepId>.png, goes in outputDir, so it lands on the same path every run and later runs compare against it. outputDir defaults to ., which resolves against your config file’s folder. With no config file, that’s the current folder.

Annotations

Annotations mark up a screenshot as part of capturing it, so the callouts regenerate with the image instead of drifting from it. Each annotation names exactly one type, and the type’s value is what it points at:

{
"screenshot": {
"path": "login.png",
"annotations": [
{ "outline": "#username" },
{ "badge": "#password", "label": "2" },
{ "blur": ".account-id" }
]
}
}

Doc Detective composites annotations into the image instead of injecting them into the page. It never modifies the page under test. An annotated screenshot therefore can’t disturb a test or leak into a concurrent recording.

Types

TypeDraws
outlineA box around the target.
arrowAn arrow pointing at the target.
badgeA small marker on the target, showing label. Use for numbered steps.
calloutA text box with a leader line, showing label.
blurA blur over the target, to redact sensitive information.
textA standalone text box, showing label.

Targets

A target can be:

  • A string, a selector or display text, the same as crop accepts. For example, { "outline": "#submit" }.
  • An object, using the same element-finding fields as find. For example, { "outline": { "elementTestId": "submit" } }.
  • A position, a fixed spot for annotations that aren’t tied to an element. For example, { "text": { "position": "top-right" }, "label": "Demo data" }. Accepts a named region (top, bottom, left, right, center, top-left, top-right, bottom-left, bottom-right) or a point ({ "x": 640, "y": 220 }).

An element target waits up to 5000 milliseconds for its element to appear. Set timeout on the target to wait longer:

{ "outline": { "elementTestId": "dashboard", "timeout": 20000 } }

timeout works the same way as find’s. Like find’s, it needs the object form of the target, because the string shorthand has nowhere to put it.

Options

  • label: Text to display. Used by badge, callout, and text.
  • position: Where to place the annotation relative to its target. Accepts a named region, a point, or an offset ({ "offset": { "x": -80, "y": -60 } }).
  • style: Visual overrides for this annotation: color, background, strokeWidth, fontSize, fontFamily, opacity, radius, padding, maxWidth, and intensity (blur strength).
  • all: Annotate every matching element instead of only the first. Needs an object target, because a string could be a selector or display text.

Use all for redaction. { "blur": { "selector": ".secret" } } blurs only the first match. If the page shows several, the rest stay readable in an image that looks redacted.

Annotations resolve after crop, so you can crop to a panel and annotate elements inside it. An annotation whose target doesn’t resolve fails the step rather than quietly producing an unannotated image.

App surfaces

Annotations work on native app surfaces too, including element-anchored ones. Native surfaces match elements through the platform’s accessibility layer rather than the DOM, so only four fields have a native equivalent: elementText, elementId, elementTestId, and elementAria. selector, elementClass, and elementAttribute are browser-only, and a bare string target isn’t accepted on an app surface. Name the field explicitly instead:

{
"screenshot": {
"path": "settings.png",
"surface": { "app": "notepad" },
"annotations": [
{ "outline": { "elementText": "Save" } },
{ "text": { "position": "top-left" }, "label": "Step 1" }
]
}
}

Position-anchored annotations need no element lookup, so they work on every surface.

all isn’t supported on app surfaces yet, because the native element lookup resolves a single match. Rather than annotate the first match and leave the rest, the step fails. Give each element its own annotation instead.

Themes

Set annotationDefaults to style every annotation at once, so images across a doc set stay consistent. Set it on the config, a spec, or a test. A test’s theme overrides a spec’s, which overrides the config’s, and an individual annotation’s style overrides all of them.

{
"annotationDefaults": {
"color": "#E11D48",
"strokeWidth": 3,
"fontFamily": "Inter, system-ui, sans-serif",
"badge": { "background": "#E11D48", "color": "#FFFFFF" },
"blur": { "intensity": 14 }
}
}

Base properties apply to every type. A per-type block, such as badge or callout, overrides them for that type.

Annotations change the image’s pixels. If you compare against a reference image with maxVariation, changing a theme or an annotation invalidates existing reference images.

Visual Regression Testing: When a reference image exists at the target path, the action compares the new screenshot to it, and maxVariation sets the tolerance (default 0.05). If the difference is within maxVariation, the step passes. If it exceeds the threshold, the step returns WARNING, and behavior depends on the overwrite setting.

Size mismatches: a reference image from one machine can have different dimensions than a new screenshot on another. A new CI runner image, for example, can render the browser at a slightly different size. When the sizes differ, Doc Detective resizes the browser window so the viewport matches the reference image. It then takes the screenshot again and compares that one. The window returns to its original size when the step finishes, and $$resizedToReference reports the resize. Doc Detective doesn’t resize the window when:

  • The step uses crop. A cropped image’s size comes from the element, not the window.
  • overwrite is "true" on a local path, so there’s no comparison.
  • A recording is running, because the resize would show in the video.
  • The context’s viewport or window no longer matches the reference image, so a deliberate size change isn’t undone. A reference image within 16 pixels of the viewport still matches. For a window, the window can change by up to 5% to match.
  • Both dimensions differ by the same factor, which means a different pixel density rather than a different size.

If the browser can’t reach the reference image’s size, Doc Detective logs a warning and compares the first screenshot. A headed browser window can’t grow past the display, and no browser window can shrink below its minimum width. Each resize costs a second screenshot, so once an environment change is permanent, refresh the reference image with "overwrite": "true".

URL Paths: When path is an http(s):// URL pointing to a PNG image, Doc Detective downloads that image once per run. It uses the image as a read-only reference for comparison. The new screenshot is saved for inspection in the context’s screenshots/ folder in the run folder, the same folder automatic screenshots use. The overwrite setting is ignored, because remote storage is never mutated. The step result includes outputs.referenceUrl for traceability. This suits teams that store reference images in cloud storage like S3 without managing local files.

URL paths must end in .png (or .PNG) and can include query strings for signed URLs. The step fails if the URL is unreachable or returns invalid image data.

Local and private addresses: a reference URL can be on a local development server, in a Docker network, or on a private network, such as http://localhost:8080/home.png. To guard against server-side request forgery (SSRF), Doc Detective refuses a public host’s redirect to a private address. That includes loopback and link-local addresses, such as the 169.254.169.254 cloud-metadata endpoint. That fails the step with Couldn't follow the redirect from <url>: it leads to <address>, a private address, from a public one. To follow such a redirect anyway, set the DOC_DETECTIVE_ALLOW_LOCAL_URLS=1 environment variable. On a shared runner, set DOC_DETECTIVE_BLOCK_LOCAL_URLS=1 to refuse every private and loopback reference URL, including direct ones. See Refuse local URLs on shared runners.

Outputs: depending on which options you configure, the action exposes the following values in its outputs object. To capture any of them for later steps, use the step-level variables object with the matching expression. Each output is present only when its check applies:

  • $$width and $$height: the saved image’s actual pixel dimensions, after any crop. When the browser floors a requested viewport, these report the size the page rendered rather than the size you asked for. You can then reconcile a caption or report against the file. They’re present when Doc Detective can read the saved image’s dimensions. They’re omitted when the metadata read fails or the image has no readable size.
  • $$cropElementFound: whether the element to crop to was located (when crop is set).
  • $$fitsViewport: whether the cropped element fits within the viewport (when crop is set).
  • $$annotationTargetsFound: whether every annotation’s target resolved (when annotations is set). The step fails if any target is missing.
  • $$annotationsInBounds: whether every annotation landed inside the image (when annotations is set). The step returns WARNING if one falls outside, which usually means the crop left it out of frame.
  • $$aspectRatioMatches: whether the new capture and the reference image share an aspect ratio (when comparing against a reference image).
  • $$variation: the fractional difference between the new capture and the reference image (when comparing against a reference image).
  • $$resizedToReference: the pixel sizes of the first screenshot (from) and the reference image (to), when Doc Detective resized the window to match the reference image. See Size mismatches.

For comprehensive options, see the screenshot reference.

A browser screenshot blurs every element whose text or form value holds a secret environment variable value. A field that received a typed secret shows dots. Native app and device screenshots aren’t blurred. See Screenshots and recordings.

Examples

The docs tests run the two shorthand examples, the first two crop examples, and the annotated example. The other examples are illustration-only.

Basic full viewport screenshot (boolean shorthand)

Saves <stepId>.png in outputDir.

{
"tests": [
{
"steps": [
{
"description": "Take a basic screenshot.",
"screenshot": true
}
]
}
]
}

Full viewport screenshot to specific path (string shorthand)

{
"tests": [
{
"steps": [
{
"description": "Save screenshot to a specific file.",
"screenshot": "./output/images/login_page.png"
}
]
}
]
}

Full viewport screenshot with visual comparison (object format)

Compares against ./output/reference_images/home_page.png. Fails if difference > 5% (0.05). Overwrites reference if failure occurs.

{
"tests": [
{
"steps": [
{
"description": "Take screenshot and compare to reference.",
"screenshot": {
"path": "home_page.png",
"directory": "./output/reference_images",
"maxVariation": 0.05,
"overwrite": "aboveVariation"
}
}
]
}
]
}

Visual comparison against a remote URL

Compares against a reference image stored in S3 or other cloud storage. The new capture is saved in the run folder for inspection.

{
"tests": [
{
"steps": [
{
"description": "Compare against reference image in S3.",
"screenshot": {
"path": "https://my-bucket.s3.amazonaws.com/reference-images/home_page.png",
"maxVariation": 0.05
}
}
]
}
]
}

Screenshot cropped to an element (object format)

Captures only the element matching the #main-content selector.

{
"tests": [
{
"steps": [
{
"description": "Screenshot only the main content area.",
"screenshot": {
"path": "main_content_area.png",
"directory": "./output/element_shots",
"crop": "#main-content"
}
}
]
}
]
}

Screenshot cropped to an element with padding and visual comparison

Captures the element with text “Submit Button”, adds 10px padding, and compares against a reference image.

{
"tests": [
{
"steps": [
{
"description": "Screenshot a button with padding and compare.",
"screenshot": {
"path": "submit_button_padded.png",
"directory": "./output/reference_elements",
"crop": {
"elementText": "Submit Button",
"padding": 10
},
"maxVariation": 0.01,
"overwrite": "aboveVariation"
}
}
]
}
]
}

Annotated screenshot

Numbers the fields of a form and redacts an email address, all as part of the capture.

{
"tests": [
{
"steps": [
{
"description": "Capture an annotated screenshot of the sign-in form.",
"screenshot": {
"path": "sign_in_annotated.png",
"directory": "./output/images",
"annotations": [
{ "badge": "#username", "label": "1" },
{ "badge": "#password", "label": "2" },
{
"callout": "#submit",
"label": "Sends you a one-time code",
"position": "right"
},
{ "blur": { "selector": ".account-email" }, "all": true }
]
}
}
]
}
]
}

Crop to an element found by test ID with padding

crop accepts any of the find element-finding fields, not just a selector. Set padding per side with an object instead of one number for all sides.

{
"tests": [
{
"steps": [
{
"description": "Crop to the checkout button and pad each side.",
"screenshot": {
"path": "checkout_button.png",
"directory": "./output/element_shots",
"crop": {
"elementTestId": "checkout-button",
"padding": { "top": 8, "right": 16, "bottom": 8, "left": 16 }
}
}
}
]
}
]
}

Overwrite the reference image

overwrite controls when a comparison refreshes the reference image. "true" rewrites the reference on every run, which disables drift detection. Use it to seed a new reference image or reset an existing one, not in a gating run. To gate a run on drift instead, use "aboveVariation" with maxVariation (see Full viewport screenshot with visual comparison).

{
"tests": [
{
"steps": [
{
"description": "Rebaseline the reference image on this run.",
"screenshot": {
"path": "home_page.png",
"directory": "./output/reference_images",
"overwrite": "true"
}
}
]
}
]
}

Capture a specific browser tab or app window

Set surface to capture a surface other than the active one. Specifying a surface also switches the active surface for the steps that follow.

{
"tests": [
{
"steps": [
{
"description": "Capture the cart tab in Chrome.",
"screenshot": {
"path": "cart.png",
"surface": { "browser": "chrome", "tab": "cart" }
}
}
]
}
]
}

To capture a native app window, use the object form { "app": "<name>" } or target a specific window. App captures don’t support crop.

{
"tests": [
{
"steps": [
{
"description": "Capture the newest Notepad window.",
"screenshot": {
"path": "notepad.png",
"surface": { "app": "notepad", "window": -1 }
}
}
]
}
]
}

Draw an arrow and a caption

Beyond badge, callout, and blur, annotations include arrow (points at the target) and text (a standalone box at a fixed position). Set style to override the theme for one annotation.

{
"tests": [
{
"steps": [
{
"description": "Point out the submit button and caption the frame.",
"screenshot": {
"path": "annotated_sign_in.png",
"directory": "./output/images",
"annotations": [
{
"arrow": "#submit",
"position": { "offset": { "x": -80, "y": -60 } },
"style": { "color": "#E11D48", "strokeWidth": 3 }
},
{
"text": { "position": "top-right" },
"label": "Demo data",
"style": { "background": "#0F172A", "color": "#FDE047", "padding": 8 }
}
]
}
}
]
}
]
}

Automatic screenshots

By default, Doc Detective saves a screenshot after every step that drives a browser, a native app, or a device. App and device steps capture the app or device screen. This is useful for:

  • Building a visual record of test runs
  • Detecting UI regressions by comparing screenshots between runs
  • Debugging failures by inspecting the state at each step

To turn it off, set autoScreenshot to false at the config, spec, or test level (test overrides spec, which overrides config), or pass --no-auto-screenshot:

{
"autoScreenshot": false
}

For more, see Capture screenshots automatically.

Doc Detective saves auto screenshots under <outputDir>/.doc-detective/runs/<runId>/, nested in the REST-style resource tree. The path structure uses stable IDs derived from file paths and content hashes:

.doc-detective/runs/<runId>/ # <runId> is a sortable run ID, for example 2026-06-27T04-55-00-000Z
├── testResults.json
├── testResults.html
├── testResults.md
└── specs/
└── <specId>/
└── tests/
└── <testId>/
└── contexts/
└── <contextId>/
└── screenshots/
└── 01-goTo-s4f2a91c.png

The HTML report provides a human-readable view of the same results. Run folders therefore become self-contained artifacts you can share or compare without the standalone html reporter.

Doc Detective generates deterministic IDs based on content rather than random UUIDs, so the same test step produces the same relative path in every run. This lets you diff two run folders and spot UI changes over time.

Auto screenshot capture skips steps that already define an explicit screenshot, since those steps already produce an image. Doc Detective still captures failed steps, because the failure state is often the most useful frame for debugging.