Skip to navigation

Capture screenshots automatically

Doc Detective builds a visual record of every test without a screenshot step in sight. By default, it saves an image after every step that drives a browser, a native app, or a device. New to capturing screenshots? Start with Capture screenshots.

Before you begin

Make sure you have the following:

  • Doc Detective installed.
  • A test with steps that drive a browser, a native app, or a device, such as goTo, click, find, or type.

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.

What you get by default

With no setting at all, Doc Detective saves a screenshot after every step that drives a UI:

  • Browser steps capture the page, headed or headless.
  • Native app steps, on Windows and macOS, capture the app window.
  • Device steps, on Android and iOS apps or mobile web, capture the device screen.

Steps that drive no UI, such as runShell, httpRequest, and wait, get no screenshot. Neither does a step with its own screenshot, since it already makes one. A step that fails still gets one, because the failure frame is usually the most useful one for debugging.

login.spec.json
{
"tests": [
{
"steps": [
{ "goTo": "http://localhost:8092" },
{ "goTo": "http://localhost:8092/enhanced-elements.html" }
]
}
]
}

Screenshots land in the run folder, on a stable path for each step:

<outputDir>/.doc-detective/runs/<runId>/specs/<specId>/tests/<testId>/contexts/<contextId>/screenshots/01-goTo-<stepId>.png

The number is the step’s place in the test, so the first step you wrote is 01-…. The IDs come from file paths and content hashes, not random values. So the same step lands on the same relative path in every run. Diff two run folders to spot UI changes over time.

While screenshots are on, the runFolder reporter runs even if reporters leaves it out. See Reporters and artifacts.

To look through the screenshots, open testResults.html in the run folder. The run prints its path after Run report:. The report loads the images from the run folder, so in CI, upload the whole folder. See Find the HTML report and Collect the run folder and artifacts.

Each reporter points at the images its own way:

ReporterWhere the screenshot shows up
html, and the run folder’s testResults.htmlEvery step’s media strip, labeled Automatic.
terminalA Screenshot: line with the absolute path, on a step that failed or warned.
markdown, and the run folder’s testResults.mdA Screenshot: path, relative to the summary file, on a step that failed or warned.
junitA [[ATTACHMENT…]] line holding the path, relative to junit.xml, for the first step in a test case that failed or warned.
json, and the run folder’s testResults.jsonThe step’s autoScreenshotPath field, relative to the run folder, which the report names in runDir.

Like screenshot steps, automatic browser screenshots blur elements whose text or form value holds a secret environment variable value. The blur appears in the saved image. Text inside iframes, shadow DOM, or a canvas isn’t blurred, and neither are native app and device screenshots. See Screenshots and recordings.

Automatic screenshots don’t compare against a reference. To gate on drift, add a screenshot step with maxVariation, as described in Detect visual regressions.

Turn them off

Set autoScreenshot to false at the config, spec, or test level. A test’s setting overrides a spec’s, which overrides the config’s. A step can’t set it: autoScreenshot on a step is an unknown key, so the spec fails validation.

.doc-detective.json
{
"autoScreenshot": false
}

For a single run, use the flag:

doc-detective --no-auto-screenshot

The flag sets the config level, so a spec or test that sets autoScreenshot to true still takes screenshots. To keep screenshots for one test and drop them everywhere else:

{
"autoScreenshot": false,
"tests": [
{
"autoScreenshot": true,
"steps": [{ "goTo": "https://www.example.com" }]
}
]
}

Verify it works

  • Each UI step in the results has an autoScreenshotPath.
  • The run folder holds a screenshots/ folder per context.

Troubleshooting

A step has no automatic screenshot

Cause: the step drives no UI, it defines its own screenshot, or autoScreenshot is false at some level.

Solution: this is expected. Check the setting at the test, spec, and config levels, in that order.

No step has an automatic screenshot

Cause: Doc Detective couldn’t install the image packages it needs, and printed Warning: Not taking automatic screenshots: couldn't install sharp (…).

Solution: run doc-detective install runtime sharp pngjs pixelmatch, then run the tests again. Or set autoScreenshot to false to stop trying.

What’s next