Writing Tests
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
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
- Run Node.js Scripts:
- Execute custom JavaScript code within your test steps.
- Use NPM modules to extend functionality.
- Dynamic Outputs:
- Store the result of your script in a variable for use in subsequent steps.
- 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.
Test file: testdriver/testdriver.yaml
Additional details
- The
exec
command now takes alang
argument and supports different operating systems (linux
,mac
, andwindows
). - Supported
lang
values arejs
orshell
:js
code is executed in a Node.js VM on the host machine (your computer).shell
code is executed in the shell on the runner.
- Code specified in
linux
,mac
, andwindows
is executed based on the platform of the runner machine.
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. - The script runs in the same context as the calling process and uses Node.js’s VM module internally.