> For the complete documentation index, see [llms.txt](https://coldbox.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coldbox.ortusbooks.com/the-basics/testing-quick-start/browser-testing/artifacts-and-debugging.md). Agents can also search and read this documentation through its [MCP server](https://coldbox.ortusbooks.com/~gitbook/mcp).

# Artifacts & Debugging

Keep screenshots, traces and videos of failed browser specs, attach your own files, open traces and debug specs in a visible, slowed down browser.

A failing browser spec is only as useful as what it leaves behind. bx-playwright can record **screenshots**, **traces** and **videos**, and TestBox attaches the ones it keeps to the failed spec, so they show up in your reports and CI artifacts.

## Keep Artifacts of Failed Specs

Artifacts are **off** by default. Turn them on with a bx-playwright profile, for example the built-in `ci` profile, which runs headless and keeps a screenshot, a trace and a video of every failure:

```javascript
@appMapping( "/root" )
@browser
@browserProfile( "ci" )
class extends="coldbox.system.testing.BaseTestCase" {
	// ...
}
```

Or, without touching your specs, choose the profile with an environment variable. It applies to every bundle without a `browserProfile` annotation:

```bash
export BX_PLAYWRIGHT_PROFILE=ci
```

When the `browse()` callback throws, its browser contexts close as **failed**, so the artifact policies keep their files, and every kept file is attached to the running spec with its type: `screenshot`, `trace` or `video`. The original exception is rethrown unchanged, so the spec still fails with its real message.

| Policy                                  | Keeps                                  |
| --------------------------------------- | -------------------------------------- |
| `off`                                   | Nothing                                |
| `on`                                    | Always                                 |
| `only-on-failure` / `retain-on-failure` | Only when the context closes as failed |

You can set the policies yourself in the bx-playwright `artifacts` setting (`{ directory, screenshot, trace, video }`) or in a custom profile. Files go to `~/.boxlang/playwright/artifacts` unless you set `artifacts.directory`. See [bx-playwright Testing](https://bxplaywright.boxlang.io/testing/) and [Profiles](https://bxplaywright.boxlang.io/profiles/).

## Where Attachments Show Up

| Report or output                | Attachments                                                    |
| ------------------------------- | -------------------------------------------------------------- |
| JSON report                     | The `attachments` array of each spec                           |
| Simple (HTML) report            | Links under the spec                                           |
| JUnit and ANT JUnit reports     | A `<system-out>` with one `[[ATTACHMENT\|path]]` line per file |
| Text, console and stream output | Listed under each failed spec                                  |

## Attach Your Own Files

`attach( path, [type], [name] )` attaches any file to the running spec, passed or failed. Call it from a spec body or a `beforeEach()`, `afterEach()` or `aroundEach()`:

```javascript
it( "renders the invoice", () => {
	browse( ( page ) => {
		visitRoute( page, "invoices.show", { id : 1001 } )
		var file = expandPath( "/tests/results/invoice-1001.png" )
		page.screenshot( file )
		attach( file, "screenshot", "Invoice 1001" )
		expect( page ).toSee( "Total" )
	} )
} )
```

## Open a Trace

A trace records every action, network request, console message and DOM snapshot of a context. Open it in the Playwright trace viewer:

```bash
bxplaywright show-trace path/to/trace.zip
```

You can step through the spec action by action and see the page before and after each one: usually the fastest way to understand a failure that only happens in CI.

## Debug a Spec

Run the bundle with the `debug` profile to watch the browser: it opens a **visible** browser, slows every action down by 250 milliseconds and records every artifact.

```javascript
@appMapping( "/root" )
@browser
@browserProfile( "debug" )
class extends="coldbox.system.testing.BaseTestCase" {
	// ...
}
```

Other helpers:

* `@browserProfile( "headed" )` or `BX_PLAYWRIGHT_HEADLESS=false`: a visible browser at full speed.
* `page.snapshot()` prints the accessibility tree of the page, handy to find the right selector.
* `bxplaywright codegen http://127.0.0.1:8080` records your clicks as BoxLang code you can paste into a spec.
* `debug( page.url() )` or `debug( page.content() )` adds values to the TestBox debug output.
* Focus a single spec with `fit()` or `fdescribe()` while you work on it.

## Rerun Only What Failed

In the web runner, the HTML reports (`Simple`, `Min`, `Dot`, `Doc`) show a **Run Failed** button next to **Run All** when something failed. Its link reruns only the failed specs and is built from the report, so the runner keeps no state.

TestBox's BoxLang CLI runner has the same feature as `./testbox/run --failed`, for specs it can run: `BaseTestCase` specs need a web runner (see [Setup](https://coldbox.ortusbooks.com/the-basics/testing-quick-start/browser-testing/setup.md#run-your-application)). From code, `results.getFailedTargets()` returns the same bundles and spec ids on every engine. See [Run All and Run Failed](https://testbox.ortusbooks.com/digging-deeper/reporters#run-all-and-run-failed) in the TestBox docs.

## See Also

* [Continuous Integration](https://coldbox.ortusbooks.com/the-basics/testing-quick-start/browser-testing/continuous-integration.md)
* [Troubleshooting](https://coldbox.ortusbooks.com/the-basics/testing-quick-start/browser-testing/troubleshooting.md)
* [bx-playwright CLI](https://bxplaywright.boxlang.io/cli/)
* [TestBox attachments](https://testbox.ortusbooks.com/browser-testing/attachments)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://coldbox.ortusbooks.com/the-basics/testing-quick-start/browser-testing/artifacts-and-debugging.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
