testdriver/exec-shell.yaml
Description
Theexec
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 pwsh 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. |
code | string | The script to execute on Windows systems. For js , the script must define the output as result . |
silent | string | Defaults to false . The command will print the output of the script. This is useful for suppressing unnecessary or private output in the test logs and it’s useful for debugging. |
Example usage
This example demonstrates how to use theexec
command to generate a TOTP (Time-based One-Time Password) using the totp-generator
library.
otp-generator.yaml
Additional details
- Supported
lang
values arejs
orpwsh
:js
code is executed in a Node.js VM internally on the host machine (for example the machine where your CI/CD runs, or your computer if using the local agent).pwsh
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
pwsh
in lifecycle scripts to install npm packages if you need them. - Otherwise, the
pwsh
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
- 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. - 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 pwsh
and js
contexts within a prerun.yaml
script which creates a temporary email account and automatically clicks links found in received emails.
./lifecycle/prerun.yaml
Using exec
pwsh commands in a test file
In a test file, you can use the pwsh
context directly:
calculator.yaml
One more option
You can also save reusable snippets (like launching the calculator) to be inserted into a script later with therun
command. That version would look something like this:
snippets/launch-calculator.yaml
calculator.yaml
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!
badtestfile.yaml