This feature is available to users subscribed to the following plans:
- Enterprise Plan
- New Standard Plan 2025 (Refs)
Table of Contents
- Overview
- How to use
- Configuration Examples
- Available Libraries and Modules
- Utilize generative AI
- Q&A
- Restrictions
Overview
The "Run TypeScript code" command is a command that executes arbitrary JavaScript or TypeScript code within a test case and stores the result in a variable.
It can be used in cases such as the following:
- Generating random values
- Performing calculations or string manipulation
- Calculating and formatting dates and times
How to use
Using MagicPod Autopilot, you can easily configure the "Run TypeScript code" command.
Please check here for more details.
Here, we explain how to add a step manually.
- Add the "Run TypeScript code" command.
2. Click the "Edit" button to open the TypeScript code editing screen.
3. Write your JavaScript or TypeScript code and configure the input/output values for the function.
In the code editor, an empty function named magicpodCustomStep is created. Write the necessary processing inside this function.
⚠️ Please use the function name magicpodCustomStep without changing it. Note that you can define additional helper functions and call them from within the magicpodCustomStep function.
function magicpodCustomStep(args: Args): Output { return []; }
In the "Inputs" area of the editing screen, you can configure the input values (args variable) for the magicpodCustomStep function.
When configured as shown above, the values can be accessed within your code as args.name or args.email.
⚠️ Input values are treated as strings. If you need to convert them to numbers, please use the Number function.
Example: Number(args.number)
In the "Outputs" area, you configure the variable in which the output value of the magicpodCustomStep function will be stored.
Configuration Examples
Here, we introduce some specific configuration examples. Note that simple examples are used for illustration purposes, but for straightforward calculations like these, the "Arithmetic operation" command is convenient.
1. Output the sum of a + b
As a simple example, let's create a step that outputs the sum of input values a and b.
You can configure the inputs (the args variable) for this function from the edit dialog.
Set the values for args.a and args.b.
Update the code to use the input values:
function magicpodCustomStep(args: Args): Output { const sum = Number(args.a) + Number(args.b); return []; }
Update the code to calculate the sum of args.a and args.b.
⚠️ Input values (a, b) are strings. Please use the Number function to convert them to numbers.
Similarly, you can configure the output values of the function from the edit dialog.
Save the output to a "sum" variable.
Update the code to return the sum:
function magicpodCustomStep(args: Args): Output { const sum = Number(args.a) + Number(args.b); return [sum]; }
Update to return [sum].
- The output value is an array.
- The elements of the array are variables of type number, string, or boolean.
2. Output the quotient and remainder of x ÷ 100
As a second example, let's create a step that takes ${variable} as input and returns multiple values.
For example, if the input x is 1234, the quotient will be 12 and the remainder will be 34.
Configure inputs and outputs from the edit dialog.
The code will be as follows:
function magicpodCustomStep(args: Args): Output { const x = Number(args.x); const q = Math.floor(x / 100); const r = x % 100; return [q, r]; }
Return multiple values.
Available Libraries and Modules
Your code can use some modules from date-fns (v4.1.0).
'date-fns''date-fns/locale'
Example usage:
import { format } from 'date-fns'; import { enUS } from 'date-fns/locale'; function magicpodCustomStep(args: Args): Output { // "Today is a Thursday" return [format(new Date(), "'Today is a' eeee", {locale: enUS})]; }
Utilize generative AI
Here, we introduce how to create a "Run TypeScript code" step using generative AI.
MagicPod Autopilot
Using MagicPod Autopilot, you can generate an "Run TypeScript code" step.
Since it also handles the input and output configuration, this is a recommended and easy-to-use approach.
Example prompt:
Insert a step to generate a random filename using the 'Run TypeScript code' command.
Other generative AI
When using other AI tools, you can generate TypeScript code compatible with the "Run TypeScript code" command by using a prompt such as the one below.
In this case, inserting the "Run TypeScript code" step and configuring the input and output settings must be done manually.
We will extend the following magicpodCustomStep function:
- The Args type can have properties of type string.
- The Output type is a tuple of number | string | boolean types.
- The Args and Output types are defined automatically.
```ts
function magicpodCustomStep(args: Args): Output {
return [];
}
```
Create a function that generates a random filename.
Q&A
Q.Is it possible to run only the TypeScript code to verify it?
Please use the partial execution feature for test steps.
Note that by including console.log() in your code, you can output any values or processing results to the execution log. Please make use of this for verifying code behavior and debugging.
Q.Is it possible to use the TypeScript code I created in multiple places?
You can copy the "Run TypeScript code" step or use shared steps.
Q.Can the "Run TypeScript code" command be used within a shared step?
Currently, you cannot connect shared step connectors to the input values of "Run TypeScript code."
Please use it via the "Store fixed value" command.
Restrictions
Accessing UI elements
Currently, you cannot access UI elements from the "Run TypeScript Code" command. Please use it for calculations that do not involve UI operations (DOM manipulation).
If you would like access to UI elements, please vote here:
Manipulate UI elements (DOM) using the Run TypeScript Code command