exec
Execute custom shell or Node.js scripts within your tests.
Description
The exec
command allows you to execute custom Node.js scripts within your TestDriver tests. This is useful for tasks like generating dynamic data, interacting with APIs, or performing custom logic during a test. The output of the script can be stored in a variable for use in subsequent steps. It’s important to note that the output from exec
must be a string
.
Arguments
Argument | Type | Description |
---|---|---|
lang | string | The language of the script to execute. Supported values are shell and js . |
output | string | The variable name to store the result of the script. This variable can be accessed as ${OUTPUT.<var>} in future steps. |
silent | string | Defaults to true . This is useful for suppressing unnecessary or private output in the test logs. If set to false , the command will print the output of the script. This is useful for debugging. |
linux | string | The script to execute on Linux systems. For js , the script must define the output as result . |
windows | string | The script to execute on Windows systems. For js , the script must define the output as result . |
mac | string | The script to execute on macOS systems. For js , the script must define the output as result . |
Example usage
This example demonstrates how to use the exec
command to generate a TOTP (Time-based One-Time Password) using the totp-generator
library.
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 (for example the machine where your CI/CD runs, or your computer if using the local agent).shell
code is executed in the shell on target runner (which can be the cloud runner, local sandbox, or local machine, depending on where you run your tests).- Note: You can also use
shell
inprerun
scripts to install npm packages if you need them. - Otherwise, the
shell
code can be used within test steps to launch applications or perform simple commands (like writing text to a file on the machine to perform a simple file upload).
- Note: You can also use
- Code specified in
linux
,mac
, andwindows
is executed based on the platform of the runner machine. - The
output
argument is assigned automatically by settingresult = somestringvalue
in the script you run.
Protips
- The
result
variable is already available in your script, overwrite it to store the output as shown in the examples. - Node.js code executes in the same context as the calling process on the host machine, using the VM module internally.
- Do any handling of arrays or nested objects within your
js
script:- ✅
result = users[1].profile.firstName
- ✅
result = data.length > 0 ? data[0].userEmail : 'no user found'
if no data is found the value of output will benull
- ✅
result = someTestUserEmail
- ✅
result = someTextToAssert
- ✅
result = someDescriptionOfAnImageToScrollTo
- ✅
- Don’t try to pass any non-string values to
output
:- ❌
result = [...users, ...values]
- ❌
result = {name: "Dale Earnhardt", starts: 676, wins: 76}
- ❌
result = [{user1: ...}, {user2: ...}]
- ❌
Ways to use exec
Here is an example using both shell
and js
contexts within a prerun.yaml
script:
Using exec
shell commands in a test file
In a test file, you can use the shell
context directly:
One more option
You can also save reusable snippets (like launching the calculator) to be inserted into a script later with the run
command. That version would look something like this:
Then in the test:
Don’t try to run js
within a test field
This example will fail at runtime, so don’t try to execute js
context directly in a test file. Remember - use this in prerun
to setup your test!
This example will produce errors in the TestDriver output or CLI since the runner won’t have access to the Node.js VM context.