Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Robot Framework’s built-in ${TEST NAME} variable as the identity in a SeleniumLibrary failure filename, and add {index} when more than one screenshot can be produced. A dependable pattern is ${TEST NAME}_FAILURE_{index}.png, captured from a test teardown or a run-on-failure hook.

The filename pattern that works

SeleniumLibrary’s Capture Page Screenshot keyword accepts a filename. Put the test name in that argument and mark the artifact as a failure screenshot:

*** Settings ***
Library    SeleniumLibrary
Test Teardown    Capture Failure Screenshot

*** Keywords ***
Capture Failure Screenshot
    Run Keyword If Test Failed    Capture Page Screenshot    ${TEST NAME}_FAILURE_{index}.png

${TEST NAME} is replaced with the current Robot Framework test case name. SeleniumLibrary expands {index} to a running number beginning at 1, so repeated captures become distinct files rather than overwriting one another. You can format the number, for example {index:03}, to produce 001, 002, and so on.

Choose when the screenshot is captured

Test teardown: one decision after the test

A test teardown runs after the test body. The example above captures only when the test has failed, which normally gives you one final-state image per failed test. This is predictable and keeps artifact volume low.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
*** Test Cases ***
Checkout rejects an expired card
    Open Browser    https://shop.example/checkout    chrome
    Input Text    id=card    4000000000000069
    Click Button    id=pay

*** Settings ***
Test Teardown    Capture Failure Screenshot

If the browser should be closed after capture, use a suite or test teardown that calls your capture keyword before Close All Browsers, or put the close operation in a separate teardown sequence after the screenshot.

Run-on-failure: capture after any failed keyword

SeleniumLibrary uses Capture Page Screenshot as its default run-on-failure keyword. You can configure it explicitly when importing the library:

*** Settings ***
Library    SeleniumLibrary    run_on_failure=Capture Page Screenshot

This captures at the point a SeleniumLibrary keyword fails, which is useful for diagnosing the exact state that caused the error. It can also create several images during one test, so retain {index} in a custom naming wrapper if you need test-name-based files.

A wrapper for descriptive automatic names

Register a keyword that supplies your filename, then make that wrapper the run-on-failure handler:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
*** Settings ***
Library    SeleniumLibrary

*** Keywords ***
Capture Named Failure
    Capture Page Screenshot    ${TEST NAME}_FAILURE_{index}.png

*** Test Cases ***
Example
    Register Keyword To Run On Failure    Capture Named Failure
    Open Browser    https://example.com    chrome

In a real suite, register the handler during suite setup or in a resource file so every test uses the same policy. A run-on-failure hook may run more than once, including while cleanup keywords execute; the indexed suffix prevents collisions.

Where the files are written

If you do not configure a screenshot directory, SeleniumLibrary writes the image where the Robot Framework log is written and returns the absolute path of the created file. Set a dedicated directory in continuous integration so the artifact collector knows exactly what to upload.

Robot Framework’s built-in Screenshot library follows the same general default (the log directory) and supports an explicit location through screenshot_directory or the Set Screenshot Directory keyword. Do not mix relative paths from different working directories in CI; resolve the directory once and pass the same path to every capture keyword.

*** Settings ***
Library    SeleniumLibrary
Suite Setup    Create Directory    ${OUTPUT DIR}/screenshots
Test Teardown    Capture Failure Screenshot

*** Variables ***
${SCREENSHOT DIR}    ${OUTPUT DIR}/screenshots

*** Keywords ***
Capture Failure Screenshot
    Run Keyword If Test Failed    Capture Page Screenshot    ${SCREENSHOT DIR}/${TEST NAME}_FAILURE_{index}.png

Create the directory before the first capture (for example with OperatingSystem’s Create Directory) and configure your CI system to publish ${OUTPUT DIR}/screenshots.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make names safe and useful

Keep the identity, marker and uniqueness separate

  • Identity: ${TEST NAME} ties the file to the test case.
  • Failure marker: a stable token such as _FAILURE makes filtering easy.
  • Collision policy: {index} handles retries, multiple hooks and multiple screenshots.
  • Extension: use .png unless your capture configuration requires another format.

Sanitize test names for the target filesystem

Test names can contain slashes, colons, question marks or other characters that are illegal in filenames on some operating systems. The SeleniumLibrary keyword documentation specifies filename and index handling but does not prescribe one universal sanitization algorithm. Choose one policy and apply it consistently: replace forbidden characters with _, collapse repeated spaces, and cap the length if your CI artifact store has path limits. Preserve enough of the original name to identify the test, and use an additional stable ID if two tests become identical after sanitization.

If you need sanitization, compute a safe name in a user keyword or Python library, then pass that value to Capture Page Screenshot. Do not remove {index} after sanitizing; uniqueness is a separate concern.

Preventing overwritten screenshots

Situation Recommended filename Reason
Exactly one teardown capture ${TEST NAME}_FAILURE.png Deterministic and easy to locate, but a later run can replace an earlier artifact.
Retries or repeated failures ${TEST NAME}_FAILURE_{index}.png SeleniumLibrary assigns increasing suffixes beginning at 1.
Fixed-width sorting required ${TEST NAME}_FAILURE_{index:03}.png Alphabetical listings remain in numeric order.
Parallel workers ${SUITE NAME}_${TEST NAME}_${WORKER ID}_FAILURE_{index}.png Add a worker or shard identifier so separate processes cannot write the same path.

Parallel execution also requires separate output directories or worker-specific prefixes. An index is local to the capture process; it is not a distributed lock.

Robot Framework Browser alternative

The Browser library documents a default-style failure filename of ${TEST NAME}_FAILURE_SCREENSHOT_{index}. Its failure keyword is Take Screenshot. You can register it with Register Keyword To Run On Failure and provide a custom prefix when your suite needs a different convention. Apply the same three principles: test name for identity, a failure token for filtering, and {index} for uniqueness.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Built-in Screenshot library versus SeleniumLibrary

Use SeleniumLibrary’s Capture Page Screenshot when Selenium owns the browser and you need its run-on-failure integration. Use Robot Framework’s built-in Screenshot library when your suite calls its keywords directly or you want its screenshot_directory setting and Set Screenshot Directory keyword. In either case, pass an explicit filename if the default name is not descriptive enough, and keep the output directory under the CI artifact path.

Troubleshooting

The file is always called the same thing

You probably omitted {index} or are writing every worker to one shared path. Add the index and include a worker or process identifier for parallel runs.

No image appears after a failure

Check that a browser is still open when teardown runs, that the capture keyword is actually registered, and that the output directory exists and is writable. A run-on-failure hook cannot capture a page after the browser has already been closed.

The name contains invalid-character errors

Sanitize ${TEST NAME} before passing it as a filename. Replace operating-system-reserved characters and keep the resulting path within your CI system’s maximum length.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Several images are produced unexpectedly

Both a teardown and a run-on-failure hook may be active, or one failed test may trigger multiple failed keywords. Decide whether you want one final-state image or point-in-time images, then disable the unused trigger. Keep indexed names if multiple captures are intentional.

The artifact collector cannot find the files

Print or inspect the absolute path returned by Capture Page Screenshot, then configure the collector to publish that directory. Relative paths are resolved from the process working directory, which may differ between local runs and CI.

Operational and cost considerations

Capturing only in a test teardown minimizes storage and browser overhead. Run-on-failure captures are more diagnostic but can generate many files when a page repeatedly fails. In CI, use a dedicated directory, expire old artifacts according to your retention policy, and include suite, test and worker identifiers in parallel jobs. The screenshot filename does not change test execution timing materially; the browser image capture and file upload do.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

ScreenshotNeo can capture a URL with one request when you need an independent page image rather than a screenshot from the already-running test browser. Before capture it accepts the cookie or consent banner and removes more than 60 known consent platforms, newsletter popups and chat widgets; each step can be turned off. Bot checks, CAPTCHAs, blank pages, timeouts, failed loads and cache hits are not billed, and the response reports the result in X-Page-Verdict and X-Billed headers. Its MCP server provides take_screenshot, get_page_info and capture_pdf tools for Claude, Cursor and other MCP clients.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

See the parameter reference in the ScreenshotNeo documentation. cURL:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Python:

import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)

Node.js:

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots, and every feature is included on every plan. Create a free ScreenshotNeo account to try it.

Frequently Asked Questions

Does ${TEST NAME} include the suite name?

It is the Robot Framework test-case name. Add suite or worker variables yourself when artifacts from multiple suites or parallel processes share a directory.

Can I use a different image format?

Pass the extension and capture settings supported by the library you use; the naming approach is unchanged.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Should retries reuse the original screenshot filename?

Use {index} or a retry identifier when you need every attempt retained; use a deterministic name only when replacement is intentional.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.