Run and debug JavaScript
Run JavaScript in flows with inline expressions, evalScript, or runScript and debug with console.log.
Execute JavaScript within your flows and use logging to debug your scripts.
Execution Methods
Astra Scout provides three ways to run JavaScript, depending on the complexity of the logic you need to perform.
1. In-line expressions
For simple logic or dynamic values, use the ${} syntax directly inside existing commands. This is ideal for string concatenation or basic math.
- launchApp
- inputText: ${'User_' + Math.floor(Math.random() * 10000)} # Generates a dynamic username
- tapOn: "${maestro.platform === 'ios' ? 'Allow' : 'While using the app'}" # Conditional logicINFO
Learn how to generate synthetic data using JavaScript.
2. The evalScript Command
Use evalScript for logic-only steps that do not directly interact with a UI element, such as setting a variable or performing a calculation.
- evalScript: ${output.timestamp = new Date().getTime()} # Store data for later use
- evalScript: ${console.log('Test execution started')} # Inline logging3. The runScript Command
For complex logic, reusable functions, or long scripts, use runScript to execute an external .js file.
You can pass environment variables to your script using the env attribute, in the same way you pass parameters to subflows. First, use runScript to define the JavaScript file and the variables to be shared with it:
- runScript:
file: setupUser.js
env:
userRole: 'admin' # Pass a parameter to the scriptThe JavaScript file can then read the environment variable directly to perform necessary actions:
// Access the passed parameter directly by its name
const role = userRole;
console.log('Setting up user with role: ' + role);No template literals in script files
Before it runs a .js file, the runner replaces every ${...} in it with the value of that expression, as it does in the YAML. A JavaScript template literal such as `Role: ${role}` is therefore replaced before the script starts and breaks it. Use string concatenation in runScript files.
Logging and debugging
You can use console.log, console.warn, and console.error to debug your scripts and track flow execution.
When logging from JavaScript, keep these points in mind:
- Multiple arguments work:
console.log('My variable is', variable)prints all of its arguments. Concatenation is a clear way to write it. - Output goes to the runner's output:
console.logwrites to the runner's standard output. Astra Scout does not write a separatemaestro.logfile for it, and the text may not appear in the step list of a run.
TIP
If you need a value to show up in the results, use it in a step that has a label or assert on it, instead of relying on the log alone.
The following snippet builds a message with concatenation:
console.log('Value: ' + myVar)Logging with evalScript command
If you want to log something inline, you can use evalScript to output it to the console without creating a separate file.
- evalScript: '${console.log("Value: " + myVar)}'Don't use template literals in evalScript
Standard JavaScript template literals (using backticks ``` and ${}) will not work inside evalScript because the command itself is already wrapped in a ${...} block. The same applies to .js files run with runScript.
# Example of incorrect use
- evalScript: console.log(`Value is ${myVar}`)Use string concatenation instead.
Logging in external files
When using runScript, you can organize your logs within your JavaScript files, with multiple statements and variables. Build messages with string concatenation, because template literals do not work in these files.
The following Flow runs a script which will log a status message:
- runScript:
file: script.js// script.js
const status = 'Success';
console.log('Operation status: ' + status); // Outputs 'Operation status: Success'Next steps
Now that you already know how to run and debug JavaScript code in flows, access the following guides:
- Manage data and states: Learn how to use the
outputobject. - Generate synthetic data: Create random test data with the random input commands and JavaScript.
- Test reports and artifacts: See what Astra Scout records for each run.
