Skip to content

Tutorial: your first SAP test ​

In this tutorial you display a sales order in VA03, check one of its values, and record what you did as a test. Then you replay it on another order, break it on purpose, read why it failed and fix it. Last, you keep it as TypeScript and look at its trace.

It runs on your own SAP system, and it only reads. VA03 displays orders and changes nothing, and writes stay off for the whole tutorial (SAP_WRITE=off): an action that may save is refused before it reaches SAP. You see that happen in step 3.

You run the steps as shell commands. An agent does the same task with the MCP tools: With an agent gives the prompt and the tool for each command.

What you need ​

  • Node.js 22.18 or later.
  • An SAP session you logged on to yourself: SAP GUI for Windows with scripting allowed (SAP GUI for Windows), or SAP GUI for HTML (Web GUI) in Chrome (Chrome). The server never logs on for you.
  • Display authorisation for VA03, and two sales orders of two different sold-to parties. VA05 (List of Sales Orders) shows some.
  • A test system, or on production a user with display authorisations only: SAP_WRITE=off is a safety net, not a guarantee (Safety model).

The outputs on this page are samples: what the commands print for SAP GUI for Windows, with made-up data. The system is DEV, client 100, and there are two orders: 12345 for sold-to party 100001 and 12346 for sold-to party 100002. Type your own order numbers and sold-to parties where the page has these. Your outputs differ in their numbers, names, times and some fields; on Web GUI the session line differs too. Long screens are cut on this page, where a line says ….

1. Set up ​

Make a folder and install the package:

sh
mkdir sap-tutorial && cd sap-tutorial
npm init -y
npm pkg set type=module
npm install --save-dev sap-ui-automation
export SAP_WRITE=off

On Windows, in PowerShell, the last line is $env:SAP_WRITE = "off". type=module lets Node load the test you write in step 8 as the module it is, without a warning.

Log on to SAP yourself and leave the session on SAP Easy Access:

  • SAP GUI for Windows: log on as usual.

  • Web GUI: by default the package works in a Chrome window of its own, the SAP window. Open it, go to your Web GUI address there and log on:

    sh
    npx sap-ui-automation browser

    To work in your own Chrome instead: Choose a browser mode.

Then check what the package finds:

sh
npx sap-ui-automation doctor

doctor checks each kind of session and ends with Ready. once it has read your SAP screen. Until then, its → step says what to do next.

2. Look ​

Which SAP sessions are open?

sh
npx sap-ui-automation sessions
text
1.   sapgui DEV (100) YOUR_USER    SMEN     "SAP Easy Access"  [SAP GUI DEV, session 1]  id=sapgui:/app/con[0]/ses[0]

Each line is a session: its surface (sapgui for SAP GUI for Windows, webgui for Web GUI in Chrome), system, client and user, transaction, window title, where it is, and its id. With several open, the commands act on the first; npx sap-ui-automation use <number> chooses another. Read its screen:

sh
npx sap-ui-automation look --print
text
TRANSACTION SMEN  PROGRAM SAPLSMTR_NAVIGATION  SCREEN 101
WINDOW wnd[0]  "SAP Easy Access"
mbar  menubar  Menu {menu[0]}, Edit {menu[1]}, Favorites {menu[2]}, Extras {menu[3]}, System {menu[4]}, Help {menu[5]}
okcd  command-field
btn[3]  button  Back   (F3)
btn[15]  button  Exit   (Shift+F3)
btn[12]  button  Cancel   (F12)
btn[16]  button  User menu   (Shift+F10)
btn[17]  button  SAP menu   (Ctrl+F10)
cntlIMAGE_CONTAINER/shell  tree  9 nodes shown
  [+] Favorites  {node 1}
  [-] SAP menu  {node 2}
    [+] Office  {node 3}
    [+] Cross-Application Components  {node 4}
    [+] Logistics  {node 5}
    [+] Accounting  {node 6}
    [+] Human Resources  {node 7}
    [+] Information Systems  {node 8}
    [+] Tools  {node 9}

This is the view an agent reads (The screen view): a header with the transaction, program and screen number, the window and its title, then one line per control. Each line starts with the control's id, which actions name: okcd is the command field, btn[3] is Back (F3). Without --print, look writes the screen to a file and prints only the header and the file's path: that keeps whole screens out of an agent's context until it needs them.

look started a background process, the daemon, that keeps the session attached between commands. It ends after 30 minutes without a command, or with npx sap-ui-automation daemon stop.

3. Act ​

Open VA03. okcode runs a command in the command field: /nVA03 leaves the current transaction and starts VA03.

sh
npx sap-ui-automation okcode /nVA03
text
okcode /nVA03

TRANSACTION VA03  PROGRAM SAPMV45A  SCREEN 102
WINDOW wnd[0]  "Display Sales Order: Initial Screen"
mbar  menubar  Sales document {menu[0]}, Edit {menu[1]}, Goto {menu[2]}, Environment {menu[3]}, System {menu[4]}, Help {menu[5]}
okcd  command-field
btn[3]  button  Back   (F3)
…
ctxtVBAK-VBELN  field  Order = ""  (edit)
boxRV45S-SUCH  group  Search Criteria
…
screen: .sap-gui/screens/1-act.txt

VA03 may show the last order you displayed: SAP keeps it for you. Type your first order number into the Order field, by its id:

sh
npx sap-ui-automation act set ctxtVBAK-VBELN 12345
text
set ctxtVBAK-VBELN = "12345"

CHANGED 1 of 22 lines on VA03 · "Display Sales Order: Initial Screen" (same screen; sap_look shows all of it):
~ ctxtVBAK-VBELN  field  Order = "12345"  (edit)
screen: .sap-gui/screens/2-act.txt

set only types. SAP takes the value when you press Enter, which is key 0:

sh
npx sap-ui-automation act key 0
text
key 0

TRANSACTION VA03  PROGRAM SAPMV45A  SCREEN 4001
NUMBERS 1,234.56  (decimal point: "1,000" is one thousand, "1.000" is one)
WINDOW wnd[0]  "Display Standard Order 12345: Overview"
…
ctxtVBAK-VBELN  field  Standard Order = "12345"  (read-only)
txtVBAK-NETWR  field  Net value = "1,250.00"  (read-only)
ctxtVBAK-WAERK  field  Net value = "EUR"  (read-only)
txtVBKD-BSTKD  field  Purch. Order No. = "PO-4471"  (read-only)
…
ctxtKUAGV-KUNNR  field  Sold-To Party = "100001"  (read-only)
txtKUAGV-TXTPA  field  Sold-To Party = "Customer A / Street 1 / 00000 City"  (read-only)
…
screen: .sap-gui/screens/3-act.txt

The order is on screen. Its sold-to party is the line ctxtKUAGV-KUNNR field Sold-To Party = "100001": the id ctxtKUAGV-KUNNR is what a check names in step 4. The NUMBERS line says how your user writes numbers, so that 1,250.00 is read right. Every action and its fields: Actions.

Now see the guard refuse a write. Save is key 11 (Ctrl+S):

sh
npx sap-ui-automation act key 11
text
Refused: this action may write to SAP (key 11: Save (Ctrl+S)) and writes are off. Tell the user what you would have saved; do not look for another way. Preserve pre-existing edits. If you are unsure what leaving would discard, stay on the screen and ask the user. If you opened the transaction and only your own disposable input would be lost, leave without saving to release the lock. Use {"do":"okcode","value":"/n"} or Back (F3) and confirm the loss of that input. Only the user can allow writes (SAP_WRITE=on).
screen: .sap-gui/screens/4-act.txt

Nothing reached SAP. Every action passes the guard first, and with SAP_WRITE=off an action that may save is refused (Safety model). The guard fails closed: Save, Post, Yes on a question, and any button it does not know as safe count as writes.

/n alone leaves the transaction without saving and goes back to SAP Easy Access, where the recording will start:

sh
npx sap-ui-automation okcode /n
text
okcode /n

TRANSACTION SMEN  PROGRAM SAPLSMTR_NAVIGATION  SCREEN 101
WINDOW wnd[0]  "SAP Easy Access"
…
screen: .sap-gui/screens/5-act.txt

4. Record, with a check ​

A test is the steps and what must be true after them. Start a recording, then do the steps again. The three actions can go in one call, as a list:

sh
npx sap-ui-automation record start "display order"
text
Recording "display order" on sapgui DEV/100, to ~/sap-tutorial/sap-tests/display-order.saptest.json. Every sap_act that works is now a step. Begin with {"do":"okcode","value":"/n<transaction>"} so the replay starts clean.
sh
npx sap-ui-automation act '[{"do":"okcode","value":"/nVA03"},{"do":"set","id":"ctxtVBAK-VBELN","value":"12345"},{"do":"key","key":0}]'
text
Ran 3 actions.
1. okcode /nVA03
2. set ctxtVBAK-VBELN = "12345"
3. key 0
code:
await sap.okcode("/nVA03");
await sap.set("ctxtVBAK-VBELN", "12345");
await sap.key(0);

TRANSACTION VA03  PROGRAM SAPMV45A  SCREEN 4001
NUMBERS 1,234.56  (decimal point: "1,000" is one thousand, "1.000" is one)
WINDOW wnd[0]  "Display Standard Order 12345: Overview"
…
ctxtVBAK-VBELN  field  Standard Order = "12345"  (read-only)
txtVBAK-NETWR  field  Net value = "1,250.00"  (read-only)
ctxtVBAK-WAERK  field  Net value = "EUR"  (read-only)
txtVBKD-BSTKD  field  Purch. Order No. = "PO-4471"  (read-only)
…
ctxtKUAGV-KUNNR  field  Sold-To Party = "100001"  (read-only)
txtKUAGV-TXTPA  field  Sold-To Party = "Customer A / Street 1 / 00000 City"  (read-only)
…
screen: .sap-gui/screens/6-act.txt

While a recording is on, each answer has code: lines: the SDK calls that repeat the actions. You see them again in step 8.

Now the check. A check is tried on the screen at once, so a recording cannot hold one that was false when you made it. Try a wrong value first:

sh
npx sap-ui-automation record assert '{"check":"field","id":"ctxtKUAGV-KUNNR","equals":"100009"}'
text
Not recorded: this check does not hold on the screen now: "Sold-To Party" (wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/subPART-SUB:SAPMV45A:4701/ctxtKUAGV-KUNNR) is not "100009" (it is "100001").

Refused, with the value the field has. The right one:

sh
npx sap-ui-automation record assert '{"check":"field","id":"ctxtKUAGV-KUNNR","equals":"100001"}'
text
Check added to step 3: "Sold-To Party" (wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/subPART-SUB:SAPMV45A:4701/ctxtKUAGV-KUNNR) is "100001".

The order and the sold-to party change from one run to the next, so make them variables. A variable replaces its value wherever it stands alone in the recording, in what was typed and in what is checked:

sh
npx sap-ui-automation record variable '{"name":"order","value":"12345"}'
text
${order} = "12345" by default; 2 recorded step(s) now use it, and so will later ones. A replay can give another value.
sh
npx sap-ui-automation record variable '{"name":"soldTo","value":"100001"}'
text
${soldTo} = "100001" by default; 1 recorded step(s) now use it, and so will later ones. A replay can give another value.

Stop the recording. It is written to sap-tests/:

sh
npx sap-ui-automation record stop
text
Recorded "display order" (3 steps) to ~/sap-tutorial/sap-tests/display-order.saptest.json
Variables: ${order} = "12345", ${soldTo} = "100001"
1. {"do":"okcode","value":"/nVA03"}  -> VA03 SAPMV45A/102  (414 ms)
full answer: .sap-gui/screens/7-record.txt

The CLI prints the first lines of a long answer and writes the whole of it to the file it names (full answer:). sap-tests/display-order.saptest.json holds the three steps, the screen each one ran on and led to, the check and the two variables (File formats). It holds what was typed: treat recordings like the system's data.

5. Replay ​

Run the test again, with no model:

sh
npx sap-ui-automation replay sap-tests/display-order.saptest.json
text
PASS  display order  on sapgui DEV/100  (3/3 steps, 1.2 s)
ok   1. {"do":"okcode","value":"/nVA03"}  (499 ms; recorded 414 ms)
ok   2. {"do":"set","id":"ctxtVBAK-VBELN","value":"12345"}  (17 ms; recorded 16 ms)
ok   3. {"do":"key","key":0}  (600 ms; recorded 434 ms)
       ok: "Sold-To Party" (wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/subPART-SUB:SAPMV45A:4701/ctxtKUAGV-KUNNR) is "100001"
Variables: ${order} = "12345", ${soldTo} = "100001"

The recording begins with /nVA03, its own clean start, so it does not depend on the screen the session is on; like any /n, it leaves that screen without saving. Each step goes through the same guard as an agent's action, then the replay checks three things: SAP showed no error, the screen is the one recorded, and every check holds. The exit code is 0 when all steps passed.

6. Replay on another order, and read the failure ​

Replay it for your second order:

sh
npx sap-ui-automation replay sap-tests/display-order.saptest.json --var order=12346
text
FAIL  display order  on sapgui DEV/100  (2/3 steps, 1.8 s)
ok   1. {"do":"okcode","value":"/nVA03"}  (605 ms; recorded 414 ms)
ok   2. {"do":"set","id":"ctxtVBAK-VBELN","value":"12346"}  (19 ms; recorded 16 ms)
FAIL 3. {"do":"key","key":0}  (527 ms; recorded 434 ms)
       failed (check): "Sold-To Party" (wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/subPART-SUB:SAPMV45A:4701/ctxtKUAGV-KUNNR) is not "100001" (it is "100002")
Variables: ${order} = "12346", ${soldTo} = "100001"

Screen at the failure:
TRANSACTION VA03  PROGRAM SAPMV45A  SCREEN 4001
NUMBERS 1,234.56  (decimal point: "1,000" is one thousand, "1.000" is one)
WINDOW wnd[0]  "Display Standard Order 12346: Overview"
…
ctxtVBAK-VBELN  field  Standard Order = "12346"  (read-only)
txtVBAK-NETWR  field  Net value = "480.00"  (read-only)
ctxtVBAK-WAERK  field  Net value = "EUR"  (read-only)
txtVBKD-BSTKD  field  Purch. Order No. = "PO-4502"  (read-only)
…
ctxtKUAGV-KUNNR  field  Sold-To Party = "100002"  (read-only)
txtKUAGV-TXTPA  field  Sold-To Party = "Customer B / Street 2 / 00000 City"  (read-only)
…

Read a failure from the top:

  1. FAIL and 2/3 steps: step 3 failed, and the run stopped there. The exit code is 1, so a CI job fails.
  2. failed (check): the step ran and landed on the recorded screen, but a check did not hold. The other phases are act (the action itself failed), landing (SAP showed an error, or another screen than recorded) and refused (the guard said no).
  3. What was expected and what came: the sold-to party is not 100001, it is 100002.
  4. Variables: the values the run used. ${soldTo} is still 100001, its default.
  5. The screen at the failure, as an agent would read it.

After a failure, replay leaves the transaction with /n, so the session is ready for the next run; --stay keeps it on the failing screen.

So the test is right and its data is wrong: order 12346 has another sold-to party. A replay fails at landing instead when SAP refuses the data, for an order that does not exist:

sh
npx sap-ui-automation replay sap-tests/display-order.saptest.json --var order=99999999
text
FAIL  display order  on sapgui DEV/100  (2/3 steps, 2.0 s)
ok   1. {"do":"okcode","value":"/nVA03"}  (598 ms; recorded 414 ms)
ok   2. {"do":"set","id":"ctxtVBAK-VBELN","value":"99999999"}  (21 ms; recorded 16 ms)
FAIL 3. {"do":"key","key":0}  (584 ms; recorded 434 ms)
       failed (landing): SAP reported an error in the status bar: SD document 99999999 is not in the database or has been archived
       failed (landing): landed on another screen: VA03 SAPMV45A/102 "Display Sales Order: Initial Screen", recorded VA03 SAPMV45A/4001
Variables: ${order} = "99999999", ${soldTo} = "100001"

Screen at the failure:
TRANSACTION VA03  PROGRAM SAPMV45A  SCREEN 102
WINDOW wnd[0]  "Display Sales Order: Initial Screen"
…
ctxtVBAK-VBELN  field  Order = "99999999"  (edit)
…
sbar  status  E: SD document 99999999 is not in the database or has been archived
…

7. Fix it ​

Give the sold-to party that goes with the second order:

sh
npx sap-ui-automation replay sap-tests/display-order.saptest.json --var order=12346 --var soldTo=100002
text
PASS  display order  on sapgui DEV/100  (3/3 steps, 1.3 s)
ok   1. {"do":"okcode","value":"/nVA03"}  (598 ms; recorded 414 ms)
ok   2. {"do":"set","id":"ctxtVBAK-VBELN","value":"12346"}  (17 ms; recorded 16 ms)
ok   3. {"do":"key","key":0}  (573 ms; recorded 434 ms)
       ok: "Sold-To Party" (wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/subPART-SUB:SAPMV45A:4701/ctxtKUAGV-KUNNR) is "100002"
Variables: ${order} = "12346", ${soldTo} = "100002"

The run passes. When the screen changed rather than the data (a field moved, a new popup), the failure says landing or act, and the replay can take a repair for that one step and go on: see Repairing one step.

8. Keep it as code ​

The recording can become a TypeScript test for the test runner:

sh
npx sap-ui-automation export sap-tests/display-order.saptest.json --to sap-ts
text
sap-tests/display-order.sap.ts
ts
// Recorded on sapgui DEV/100, 2026-11-30.
import { expect, test } from "sap-ui-automation/test";

test("display order", async ({ sap }) => {
  const vars = {
    order: "12345",
    soldTo: "100001",
  };
  await sap.okcode("/nVA03");
  await sap.set("ctxtVBAK-VBELN", vars.order);
  await sap.key(0);
  await expect(sap.field("wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/subPART-SUB:SAPMV45A:4701/ctxtKUAGV-KUNNR")).toHaveValue(vars.soldTo);
});

Run it:

sh
npx sap-ui-automation test
text
  ✓ sap-tests/display-order.sap.ts › display order (1.9s) [3 unverified actions added to the lock]

  1 passed (1.9s)

The first pass wrote a lock file next to the test, sap-tests/display-order.sap.lock.json: what each screen looked like on a run that passed. Later runs compare every action with it and fail with Drift where SAP answers otherwise. A second run is checked against it:

sh
npx sap-ui-automation test
text
  ✓ sap-tests/display-order.sap.ts › display order (1.9s)

  1 passed (1.9s)

The test runs with the same settings as the commands, SAP_WRITE=off included. Writing tests with the SDK goes on from here: steps, locators, assertions that wait, and screens SAP shows for some data only.

9. Open the trace ​

The runner kept a trace of the run: each step with its timing and the screen after it. Turn it into one HTML page:

sh
npx sap-ui-automation show-trace sap-results/sap-tests-display-order-display-order/display-order.saptest.json
text
~/sap-tutorial/sap-results/sap-tests-display-order-display-order/display-order.html

Add --open to open it in your browser. The page needs nothing from the network (Trace viewer). Like the recording, the trace holds the screens it ran on: keep it where you keep the system's data.

With an agent ​

An agent does the same task through MCP. Give it the server with writes off; in Claude Code:

sh
claude mcp add sap-ui-automation --scope project -e SAP_WRITE=off -- npx -y sap-ui-automation

Other clients take SAP_WRITE=off in their env block (Install in your client). Then ask:

In VA03, display sales order 12345 and tell me its sold-to party. Record that as a test named "display order", with a check on the sold-to party and variables for the order and the sold-to party, then stop. Replay it for order 12346 and tell me why it fails. Then replay it so that it passes.

Each command of this page is one tool call:

CommandMCP tool
sessionssap_sessions
use <number>sap_use
look --printsap_look
okcode /nVA03sap_act {"do":"okcode","value":"/nVA03"}
act set ctxtVBAK-VBELN 12345sap_act {"do":"set","id":"ctxtVBAK-VBELN","value":"12345"}
act '[...]'sap_act {"actions":[...]}
record start "display order"sap_record {"op":"start","name":"display order"}
record assert '{...}'sap_record {"op":"assert",...}
record variable '{...}'sap_record {"op":"variable",...}
record stopsap_record {"op":"stop"}
replay <file> --var order=12346sap_replay {"name":"display order","variables":{"order":"12346"}}
export <file> --to sap-tssap_record {"op":"export","name":"display order","to":"sap-ts"}

A failed sap_replay stays on the failing screen and ends with how to go on: resume at the step with a repair (from, repair). All tools: MCP tools.

Next ​

MIT licence. Independent open-source project, not affiliated with, endorsed by or sponsored by SAP SE. SAP and SAP Cloud ALM are trademarks of SAP SE.