Capture screenshots
Doc Detective can capture a screenshot at any point in a test. This guide is the entry point to the Screenshots section. It covers a single capture: the full viewport, a cropped element, or an element with padding. The section’s other guides cover three related capabilities. Detect visual regressions compares a capture against a reference image. Capture screenshots automatically fires after every step. Annotate screenshots marks up an image. Look up every field in the screenshot action reference and the generated screenshot schema. Unlike recording, screenshots capture fine in a headless or CI context.
Before you begin
Make sure you have the following:
- Doc Detective installed.
- A page or site to capture. The examples below capture a local page.
- A directory where you can create a
.spec.jsonfile.
The steps in this guide work the same in every authoring style. Write them in a standalone .spec.json, inline in a doc source file, or let Doc Detective detect them from prose.
Capture a screenshot
The screenshot action takes three shapes, from simplest to most controlled.
Set screenshot: true to capture the full viewport with default settings:
To choose where the image lands, pass a file path as a string:
By default (overwrite: "aboveVariation"), a target path that already holds a file gets compared against the new capture. Doc Detective replaces the file only when the two differ by more than maxVariation, which defaults to 5%. Otherwise it leaves the existing file in place. The Detect visual regressions section covers this behavior in full. Set "overwrite": "true" to always replace the file. Set "overwrite": "false" to keep the existing file and skip the capture, which reports SKIPPED.
For more control, use the object form. Set crop to a CSS selector to capture only that element:
crop accepts the same selector grammar as find, meaning a CSS or XPath selector. To disambiguate a selector by its visible text or add breathing room, use the object form of crop:
padding accepts a single number for all four sides, or an object { "top": …, "right": …, "bottom": …, "left": … } to pad each side independently. For the full list of fields, see the screenshot action reference and the generated screenshot schema.
Verify it works
- A step that captured an image reports
PASS. - When the target path already holds a file and you set
"overwrite": "false", the step keeps the existing file and reportsSKIPPED.
Captures land in the run’s output directory and the per-run artifact tree. To see where they land and how to consume them in CI, see Reporters and artifacts.
What’s next
- Detect visual regressions by comparing a capture against a reference image.
- Capture screenshots automatically after every browser-driven step.
- Annotate screenshots to mark up an image.
- Follow the worked screenshot example end to end.
- Record video to capture a flow as a video and narrate it.
- Run in CI and at scale.