Author repeatable cached tests
Make an E2E test replay from cache on every run. Design a repeatable test, write a node's playbook, commit it, run it cache-only, repair the first failing step, and prove it with two clean cache-only runs. Includes the CLI loop and a prompt for your coding agent.
A test that replays from cache runs its recorded steps without an agent, so it is fast and cheap, and it still checks the same outcomes. This guide takes one test from first run to two clean cache-only runs, using the CLI. Cofactor also records and repairs caches on its own during normal runs. See Self-healing.
Make the test repeatable
A test that depends on yesterday's data fails for reasons the cache cannot fix. Before you touch the cache, check these.
-
Start from a known place. Name the application, environment and login. Navigate to a known page first.
-
Create what the test edits. Make your own record and delete only that. If the journey needs shared data, copy it first.
-
Use fresh values. Write
QA Test $RANDOM_STRING(6)or$EMAIL(), and carry the value to later steps with a variable. A rerun after a failed run then never touches the old leftovers. See Macros and variables. -
Clean up what you created. Inline cleanup can be skipped when a run fails hard. Use a seed with a teardown when cleanup has to run on both pass and fail. See Setup, seed & teardown.
-
Check the outcome a user sees. Write the assertion as the behavior the spec promises, and let each one run once. Do not check internals, such as an ARIA value or a number you picked for scrolling.
-
Keep the real interaction. Do not swap a click for a direct API call because the API is easier to replay. An API call is fine for setting up data.
Write and commit a playbook
-
Explore with the same login. Open a browser session with the credential the test uses and look at the real page.
bashcofactor session start --credential "<credential name>" cofactor session navigate --url https://app.example.com/items
Run
cofactor session --helpfor the other commands. Check the signed-in user and the controls you will target. Ask the person who owns the application which existing records are safe to rely on. A record that is visible today may be gone tomorrow. -
Get the test and its node ids.
bashcofactor workflow get <workflow id> --json cofactor workflow run <workflow id> --stream
You need one finished run of this test as evidence. Its id is the playbook's
sourceRunId. -
Write the playbook for one node. Save a JSON file with readable steps and one Playwright snippet per step. Run
cofactor workflow guide playbookfor the format, and see Caching for every field.json{ "sourceRunId": "<run id>", "playbookSteps": [{ "order": 1, "action": "click", "target": "Save button" }], "plannedActions": [ { "stepOrder": 1, "toolName": "browser_run_code", "actionType": "run_code", "args": {}, "code": "await page.getByRole('button', { name: 'Save', exact: true }).click();" } ], "gaps": [] }Use semantic locators you saw in the page. Preserve every configured assertion by its id, and keep macros and variables as references.
-
Validate it, then write it.
bashcofactor workflow cache commit <workflow id> --node <node id> --playbook node-playbook.json --dry-run cofactor workflow cache commit <workflow id> --node <node id> --playbook node-playbook.json
A dry run checks the file and writes nothing. The real commit prints whether it installed your playbook or kept the one already there.
Run, repair, repeat
-
Run cache-only.
bashcofactor workflow run <workflow id> --cache-only --environment <environment name>
A cache-only run is a real run. It makes the same changes in your application as the test does. It waits for the run to finish and exits with an error if the run fails. A step that cannot replay fails the run instead of calling an agent.
-
Read the first failure.
bashcofactor workflow debug overview <run id> cofactor workflow debug materialize <run id> --out ./cache-evidence
-
Fix that step. Edit its playbook and commit it again. The usual causes are a missing playbook, a locator that no longer matches, timing, or a bad starting state. If an assertion failed, compare the application with the spec and fix the application or the test. Keep the check.
-
Repeat until the run passes. Keep using cache-only runs while you prove the playbook. A cache-only run never falls back to an agent for a test step, so each failure points at the playbook.
Prove it
After your last change, run cache-only twice in a row on the unchanged test. Open both runs and confirm:
- Every step you cached replayed from the cache.
- Every configured assertion ran once and passed.
- Values such as emails came from the current run.
- Cleanup left the application ready for another run.
Login steps and steps set to always use the agent can still use an agent.
Check it worked
cofactor workflow cache status <workflow id> --jsonshows an active cache for each node you authored.- Two consecutive
--cache-onlyruns passed after your last change. cofactor workflow debug cache <run id> --misses-onlylists no step you meant to cache.
Go deeper
- Caching lists every commit option, the playbook fields and the limits.
- Self-healing explains replay, repair and test health.
- Macros and variables covers fresh data.
- Assertions covers the checks a replay must keep.
- Run
cofactor workflow guide assertionsfor assertion semantics.