Run and debug JavaScript
Run JavaScript in flows with inline expressions, evalScript, or runScript and debug with console.log.
This guide explains how to execute JavaScript within your flows and how to 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}`);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 or a template literal is still 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 shows two ways to build a message:
console.log('Value: ' + myVar) // concatenation
console.log(`Value is ${myVar}`) // template literalsLogging 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.
# 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 just as you would in a standard development environment. Here, template literals are fully supported.
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: Use
fakerto create dynamic test data. - Test reports and artifacts: See what Astra Scout records for each run.
