> ## Documentation Index
> Fetch the complete documentation index at: https://docs.testdriver.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom Code in TestDriver

> Learn how to integrate custom Node.js scripts into your TestDriver workflows for dynamic testing.

TestDriver allows you to execute custom **Node.js** scripts and shell scripts within your test workflows using the [`exec`](/commands/exec) command. This feature, introduced in version `5.1.0`, enables you to integrate custom logic, such as generating one-time passwords (OTPs), hitting APIs, or performing other dynamic operations, directly into your tests.

## Key features

1. **Run Node.js Scripts**:

* Execute custom JavaScript code within your test steps.
* Use NPM modules to extend functionality.

2. **Dynamic Outputs**:

* Store the result of your script in a variable for use in subsequent steps.

3. **NPM Support**:

* Install and use NPM packages in your scripts.

## Updated example: One-time password (OTP) validator

This example demonstrates how to generate a one-time password (OTP) using the `totp-generator` NPM package and use it in a test.

```yaml verify-otp.yaml theme={null}
version: 6.0.0
steps:
  - commands:
      - command: exec
        lang: pwsh
        code: |
          npm install totp-generator
      - command: exec
        lang: js
        output: totp
        code: |
          const { TOTP } = require("totp-generator");
          let otp = TOTP.generate("JBSWY3DPEB3W64TMMQQQ").otp;
          console.log(otp);
          result = otp;
      - command: type
        text: ${OUTPUT.totp}
```

## Additional details

* The [`exec`](/commands/exec) command now takes a `lang` argument with supported values `js` or `pwsh`.
* `js` code is executed in a Node.js [VM](https://nodejs.org/api/vm.html) module internally.
* `pwsh` code is executed in the PowerShell on the runner.

<Note>
  The `result` variable is already available in your script, overwrite it to store the output as shown in the example.

  The `output`argument is assigned automatically by setting `result = somestringvalue` in the script you run.
</Note>

## Protips

* Always assign the output of your script to the `result` variable.
* Ensure all required NPM packages are installed locally and in the `prerun` script when using GitHub Actions.
