https://github.com/testdriverai/demo-exec-js
https://github.com/testdriverai/demo-exec-js
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/testdriverai/demo-exec-js
- Owner: testdriverai
- Created: 2025-03-28T01:33:42.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-28T01:57:30.000Z (about 1 year ago)
- Last Synced: 2026-01-29T21:26:11.005Z (2 months ago)
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Execute JS In TestDriver.ai
Let's say you want to use some custom JS within your test to hit an API.
As of `5.1.0` you can execute any NodeJS script complete with NPM support using the `exec` command!
This repo is a demo of running custom npm packages in TestDriver.
## Running this demo
Clone this repo, then run:
```
npm install
testdriverai run testdriver/testdriver.yaml
```

## How it works
### Params
- `js` - The NodeJS script to run. Output must be defined as `result`
- `output` - The variable name available as `${OUTPUT.var}` in future steps
```yaml
version: 5.1.0
session: 67e57d614dc25283aa0872a9
steps:
- prompt: Log in with the totp
commands:
- command: exec
output: totp
js: |
const { TOTP } = require("totp-generator");
let otp = TOTP.generate("JBSWY3DPEB3W64TMMQQQ").otp;
result = otp;
- command: type
text: ${OUTPUT.totp}
```
### Protips
- Don't overwrite `result`
- Make sure to `npm install` locally (and within `prerun` when using the GitHub action)
- NodeJS code executes in the same context as the calling process, on the host machine. It uses [VM](https://nodejs.org/api/vm.html) internally.
### Gotchas
- This can only be deployed on hosted Windows runners, hosted Linux runners do not support `prerun` yet
## Using NPM Modules From Scratch
```
npm init
npm install totp-generator --save
```