Skip to main content
testdriver/exec-shell.yaml

Description

The exec command allows you to execute shell commands (Linux) or PowerShell commands (Windows) within your TestDriver tests. This is useful for launching applications, file operations, or performing system commands during a test.

Arguments

Example usage

This example demonstrates how to use the exec command to launch a calculator application.
calculator.yaml

Additional details

  • Supported lang values are sh or pwsh:
    • sh code is executed in the shell on Linux sandboxes.
    • pwsh code is executed in PowerShell on Windows sandboxes.
      • 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).
  • The output argument captures stdout from your script.

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 be null
    • 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 the run command. That version would look something like this:
snippets/launch-calculator.yaml
Then in the test:
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
This example will produce errors in the TestDriver output or CLI since the runner won’t have access to the Node.js VM context.