<!-- Canonical HTML: https://docs.didro.ai/sap-ui-automation/concepts/recording -->

# How recording and replay work

This page is the design behind [Recording and replay](https://docs.didro.ai/sap-ui-automation/guides/record-replay):
what is recorded and why, how a replay judges each step, how it finds a
control again after a screen changed, and what was checked on real
systems. The fields of the file are in [File formats](https://docs.didro.ai/sap-ui-automation/reference/file-formats).

## Where the recording lives

A tool, `sap_record`, with an environment variable for the directory and for
"record everything".

| | |
| --- | --- |
| `sap_record {"op":"start","name":"..."}` | starts a recording on the active session; off until then |
| `SAP_RECORD_DIR=./sap-tests` | where recordings and exports are written (default: `sap-tests` in the server's working directory) |
| `SAP_RECORD=on` | records every session from its first action, named `session <time>`, without `start` |

Why a tool and not only an environment variable:

- A test needs **checks**. Only the agent or the user knows that "the status
  bar says the order was saved" is the point of the run. Marking it needs a
  tool call at that moment, on that screen.
- A session holds exploration that is not part of the test. `start`, `stop`
  and `undo` let the agent keep the part that is.
- The variable alone would record everything, always, including typed
  values, without the user choosing it per run.

`SAP_RECORD=on` stays for the other case: keep a trace of whatever the agent
did, and decide afterwards.

The tools take a **name**, never a path. Recordings and exports are read and
written in the one directory, and the name is reduced to letters, digits and
dashes, so an agent cannot name a file elsewhere. A link in that
directory (a cloned or unpacked folder can bring one) does not lead out of
it either: recordings and exports are never written through a link, and
`sap_replay` reads a recording only from a regular file. The command line
takes paths, because the user types them.

## What is recorded

The server calls the recorder around every action it runs
([`src/server.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/server.ts), one line in `perform`). While a recording is on, for the
recorded session:

1. read the full view of the screen (before);
2. run the action through the driver, as always;
3. read the full view again (after);
4. add a step: the action as it ran, the control it was done on (full SAP id,
   type, label), the guard's risk class (`read`, `navigate`, `commit`), the
   screen before (transaction, program, screen number, window title) and the
   screen and status bar after.

Rules:

- **Only actions that worked are steps.** An action that failed or was
  refused by the guard is not recorded.
- **Recording never breaks the action.** If a screen cannot be read, the step
  has a gap; the action's answer is unchanged.
- **The file is written after every change**, so a crash loses nothing.
- **One session per recording.** Actions on other sessions run and are
  counted, not recorded.
- **Values are recorded as typed.** Passwords cannot be typed through this
  server at all, but a recording holds business data: treat the file like the
  system's data.

Cost while recording: two extra reads of the screen per action. On Web GUI
(an ECC demo system) a full read took 30 to 50 ms; an `okcode` took about 1.2 s with or
without recording.

## Marking checks, variables and captures

| Operation | Does |
| --- | --- |
| `{"op":"assert","check":"status","contains":"has been saved"}` | the status bar after the last step |
| `{"op":"assert","check":"field","id":"txtVBAK-NETWR","equals":"2.500,00"}` | a control's value. Without `equals`, `contains` or `matches`: its value now |
| `{"op":"assert","check":"title","contains":"Overview"}` | also `transaction`, and `text` (anywhere on the screen) |
| `{"op":"variable","name":"order","value":"19097"}` | a value that changes between runs |
| `{"op":"capture","name":"order","from":"status","pattern":"Order (\\d+)"}` | a value SAP produced at the last step; also from a `field` or the `title` |
| `{"op":"note","text":"..."}` | what the last step is for (used by the exports) |
| `{"op":"undo"}` | drops the last step from the recording; SAP is not undone |
| `{"op":"status"}`, `{"op":"stop"}`, `{"op":"list"}`, `{"op":"export","to":"..."}` | |

- **A check is tried on the screen as it is now and refused if it does not
  hold**, with what is there instead. A recording cannot contain a check that
  was false when it was made.
- A check or capture belongs to the last step: it describes the screen that
  step led to. A field check stores the control's full SAP id and its label.
- **A variable** is written `${name}` wherever its value stands alone: in what
  was typed, in what is expected, in the recorded status bar and titles, in
  steps already recorded and in later ones. "Alone" means not inside a longer
  word or number: `19097` is replaced in `Order 19097 saved`, not in `219097`.
- **A capture** is a variable whose value is read from the screen at every
  replay. From the step that captures it on, its recorded value is the
  variable; the step that produced it still types what it typed.
- `stop` answers with hints: typed values that are still fixed, a document
  number in a status bar that later steps reuse without a capture, no check
  at all.

## Why JSON

JSON, not YAML: the server has no YAML dependency, the actions inside are
already JSON (the same objects `sap_act` takes, so a step can be pasted into
a tool call), and SAP ids contain `\`, `[`, `:` and `#`, which YAML quotes in
ways people get wrong. The file is validated with a schema on every load, and
every action in it must be one the server accepts today.

## How a replay runs a step

Replay uses no model. It runs on a session the user has open (it never logs
on), through the same driver as `sap_act` and **the same guard**: the write
setting, the system list and the transaction lists apply to every step.

For each step:

1. fill in the variables. A variable without a value stops the run before
   the first step; it is never an empty string;
2. find the control ([below](#finding-a-control-again));
3. assess the action on the screen as it is now, ask the guard, then run
   it. The file's `risk`, its labels and its notes are the author's text:
   nothing in the replay trusts them;
4. read the screen and check it:
   - the status bar shows no error (`E`) or abort (`A`) message, unless the
     recording did at that step;
   - the screen is the recorded one: same transaction, program and screen
     number. `--loose` (`"screens":"loose"`) makes this a note, for a system
     where screen numbers differ;
   - every capture finds its value; then every check holds. Captures first,
     so a check may use a value captured at the same step.

A window SAP shows after a step that the recording does not have there is
handled by what it asks:

- a popup that only informs (text and Continue, no question, no input): the
  replay confirms it and notes it on the step;
- a screen that offers a choice (availability control: complete delivery,
  delivery proposal, continue without confirming): the step fails and names
  the ways on, and nothing is pressed, because the choice decides what is
  saved. `--continue` (`"choices":"continue"`) makes the replay press the
  neutral Continue instead, and say so;
- a question or a window that takes input: the step fails.

When the replay did anything the recording does not have, the first line of
the result says `PASS ... WITH NOTES` and names the steps.

## Finding a control again

Steps address controls by SAP scripting id, which SAP keeps stable. The id is
a path, and only its last segment names the control:

```
wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/txtVBAK-NETWR
           └ the subscreen it sits in ─────┘ └ the field ─┘
```

A support package, an enhancement or the move to S/4HANA can change the
subscreen or the tab around a field without changing the field. So the
replay looks for the control in this order, and takes a fallback only when
exactly one control matches:

1. the recorded id;
2. the same last segment anywhere on the screen (`txtVBAK-NETWR`);
3. the same field name with another control type (`ctxtVBAK-NETWR`).

A step that used a fallback passes with a note saying which id was found.
Controls that are numbered, not named (`btn[8]`, `menu[1]`, `lbl[1,13]`,
`shell`), are never looked for elsewhere: the same number in another toolbar
is another button. Labels are never used either: two fields can share a
label, and acting on the wrong one is worse than failing.

## Data

- **Values to change:** variables, with the recorded value as default.
  `--var name=value` or `"variables"`. A name no test has is an error.
- **Documents created by earlier steps:** captures. The replay of "create an
  order, then display it" creates a new order and displays that one, not the
  recorded number.
- **Tests that build on each other:** `replay a.saptest.json b.saptest.json`
  runs them in order on one session and hands every variable of `a`,
  captured ones included, to `b`. It stops at the first test that fails.
- **A clean start:** a recording that begins with `/n<transaction>` is its
  own clean start. Otherwise the replay first sends `/n` plus the transaction
  the recording started in, and stops if that does not open the screen the
  recording started on. `start` tells the agent to begin with an okcode.
- **A clean end:** after a failure the command line leaves the transaction
  with `/n` (unsaved input is dropped; nothing is saved), so the next test
  starts from a known place; `--stay` keeps the failing screen. On Fiori
  nothing is sent after a failure. `sap_replay`
  always stays on the failing screen, because the agent will want to look.
- **Writes:** a step that may write runs under `SAP_WRITE=on`, the
  default, and fails with that reason under `off`. A replay of a test that
  saves saves again: replay it on a test system.

What replay does not do: create the data a test needs (a customer, stock, an
open period), or restore the system afterwards. A test that needs a document
should create it in an earlier step or an earlier test.

## Output

Each step says how long it took, and how long it took when recorded (the
recording keeps `ms` per step). An action that took over twice as long as
recorded, and over a second more, gets a `slower:` line (in JUnit, the
case's `<system-out>`); it never fails the step.

Text (as in the [tutorial](https://docs.didro.ai/sap-ui-automation/getting-started/tutorial#_5-replay)), `--json` (the result objects), `--junit <file>` (JUnit XML, one
suite per test and one case per step, which CI servers read). Exit code 0
when every test passed, 1 otherwise.

## What was checked

Run on an SAP ERP 6.0 demo system through Web GUI in Chrome, read-only
(nothing was saved in SAP):

- **Record:** through the MCP tools, a five-step VA03 run: `/nVA03`, type
  order 19097, Enter, open the tab "Item overview", Back. Checks on the net
  value, the sold-to party, the window title and a table cell; the order as
  a variable; a deliberately false check, which was refused with the actual
  value.
- **Replay:** `sap_replay` and `sap-ui-automation replay` both pass, 5 of 5
  steps in 4.6 to 4.8 s. With `order=19096` it passes (another order, same
  values). With `order=99999999` it fails at step 3 with SAP's message ("SD
  document 99999999 is not in the database or has been archived"), skips the
  rest, shows the screen, exits 1 and leaves the transaction.
- **Export:** all six targets written from that recording.
- **The Playwright spec was run** with `@playwright/test` 1.65 against the
  same ECC: it passes in 9 s, and fails with SAP's message for
  `SAP_VAR_ORDER=99999999`. It ran in the browser that was already logged
  on, with the spec's log-off hook removed. Its own logon from a new browser
  was not verified: during the check the system sent every new logon of the
  test user back to the logon page without an error, for this repository's
  own [`test/dev/webgui-logon.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/test/dev/webgui-logon.ts) too.

Unit tests ([`test/record.test.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/test/record.test.ts))
run record, replay and the exports against a small SAP in memory
([`test/record-fakes.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/test/record-fakes.ts)), including
what the live check could not: a document created by one step and used by
the next, a field moved to another subscreen, a write refused by the guard,
and two tests in a row.

Not run:

- **The VBS export** was not executed. Record and replay on SAP GUI for
  Windows were run on SAP GUI 8.00 against ECC 6.0, by scripting
  ([SAP GUI for Windows](https://docs.didro.ai/sap-ui-automation/guides/sap-gui-windows#limits)) and by the network
  reader ([Network reader](https://docs.didro.ai/sap-ui-automation/guides/network-reader#limits)), but are not
  described step by step here.
- **Fiori.** Actions are recorded and replayed through the Fiori driver;
  from the Fiori view only the app, the toasts (as the status) and the whole
  text are read, so field checks do not work there yet. Nothing was run.
- **A replay that writes** on a real system (`SAP_WRITE=on`).
- **S/4HANA**, and so the claim that field-name fallbacks carry an ECC
  recording over.
- Creating the exported test case in a Cloud ALM tenant.

## Open questions

1. **Screens that differ by data.** A popup that appears only for some
   customers ("credit limit exceeded") makes a replay land on another
   screen: a failure, or a note with `--loose`. Code handles it: `sap.at`,
   `sap.popup` and `dismissInfo` make such a screen a branch, and a lock
   learns each path that passed ([Lock files](https://docs.didro.ai/sap-ui-automation/guides/sdk-tests#lock-files)).
   The recording format has no "if", and gets none until real cases ask
   for it.
2. **Trimming.** An agent explores, and the recording keeps every action
   that worked. `undo`, editing the file and the tidied code
   ([Tidy code](https://docs.didro.ai/sap-ui-automation/guides/record-replay#tidy-code-from-a-recording))
   are the tools today; `stop` names typed values still fixed and checks
   that are missing, but does not propose steps to drop.
3. **Dates and other computed values.** `${today}` and the like are not
   variables yet; a recorded date is a fixed value.
4. **Checks on tables and lists.** A cell is checked by position (`[1,0]`).
   "The row whose material is M-08" needs a way to name a row.
5. **Fiori.** Field checks need the reader of the file to understand the
   Fiori view, or the Fiori view to move towards the common format.
6. **Format of the view.** Replay reads facts from the text view
   ([`src/record/screen.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/record/screen.ts)).
   If the view changes, that file changes. A structured screen from the
   drivers would be firmer, and is a larger change to the `Driver`
   contract.

## Files

| | |
| --- | --- |
| [`src/record/format.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/record/format.ts) | the file format, its schema, variables |
| [`src/record/screen.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/record/screen.ts) | reads a screen's facts from its text view; finds a control again |
| [`src/record/checks.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/record/checks.ts) | evaluates checks and captures |
| [`src/record/recorder.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/record/recorder.ts) | the recorder |
| [`src/record/replay.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/record/replay.ts) | the replay, its text and JUnit output |
| [`src/record/repair.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/record/repair.ts) | a repair of one step: its rules, the capture, the proposed test |
| [`src/record/tools.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/record/tools.ts) | `sap_record` and `sap_replay` |
| [`src/record/tidy.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/record/tidy.ts) | tidy (the rules, the log, the TODO list) and `verifyTidy` |
| [`src/record/code-tools.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/record/code-tools.ts) | `sap_workflow_save` and `sap_workflow_run` (off unless `SAP_CODE_TOOLS=on`) |
| [`src/record/cli.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/record/cli.ts) | `replay` and `export` on the command line |
| [`src/record/export/`](https://github.com/DidroAI/sap-ui-mcp/tree/main/src/record/export) | `calm.ts`, `vbs.ts`, `playwright.ts`, `sap-ts.ts` (with `sap-ts-tidy.ts`), `gherkin.ts`, and `words.ts` (a step in a tester's words) |
| [`src/server.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/server.ts), [`src/cli.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/cli.ts), [`src/index.ts`](https://github.com/DidroAI/sap-ui-mcp/blob/main/src/index.ts) | the hook in `perform`, the two commands, the exports of the package |
