Run and debug JavaScript
Execute JavaScript in Flows using inline expressions, evalScript, or runScript with console.log debugging.
This guide explains how to execute JavaScript within your Maestro Flows and how to use logging to debug your scripts.
Execution Methods
Maestro 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 Maestro commands. This is ideal for string concatenation or basic math.
- launchApp
- inputText: ${'User_' + faker.name().firstName()} # Generates a dynamic username
- tapOn: ${maestro.platform === 'ios' ? 'Allow' : 'While using the app'} # Conditional logic2. 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:
The JavaScript file can then read the environment variable directly to perform necessary actions:
Logging and debugging
Maestro supports standard console.log statements to help you debug your scripts and track Flow execution.
When logging with JavaScript in Maestro, keep these points in mind:
Multiple arguments are not supported: Running
console.log('My variable is', variable)will only outputMy variable is.Use template literals or concatenation: To log variables alongside text, use template literals (in external files) or string concatenation.
Log Destination: Anything output via
console.logis captured in themaestro.logfile, prefaced withJsConsole.
The following code snippet shows examples of both methods:
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.
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.
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:
Next steps
Now that you already know how to run and debug JavaScript code in Maestro 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: Learn how to access the
maestro.logusing the--debug-outputflag to see your console logs.
Last updated