Manage data and states

Share data between scripts using the global output object, namespaces, and the maestro.copiedText property.

Sharing data between UI elements and JavaScript scripts is essential for creating dynamic, resilient tests. This guide covers how to use the global output object, manage namespaces, and capture UI text for use in your logic.

The output object

Maestro provides a single, global JavaScript object called output that persists throughout the entire execution of a Flow. Any data assigned to this object in one script or expression is immediately accessible to all subsequent scripts and Maestro commands.

You can store strings, numbers, booleans, or complex objects within output. You can access or add information to the output object using standard JavaScript notation. In the following example, the value 'Hello World' is assigned to result (an arbitrary key name):

// myScript.js
output.result = 'Hello World'

Once the Flow runs myScript.js, the value is assigned to the global object. You can then use that value in subsequent commands, such as inputText:

- runScript: myScript.js
- inputText: ${output.result}

Output Namespacing

Because the output object is global, scripts can accidentally overwrite each other’s data if they use the same variable names. To prevent this, use namespaces, sub-objects named after your specific script or feature.

Because the output object is global, different scripts can accidentally overwrite each other’s data if they use the same variable names. To prevent this, use namespaces (sub-objects named after your specific script or feature).

For example, instead of assigning variables directly to the root of output, group them by context:

output.auth = {
    token: "abc-123",
    expiry: 3600
};

You can then access this data using namespaced notation:

Shared Functions

The output object can store functions as well as data. This is a powerful pattern for defining reusable logic, such as API helpers or complex string formatters, at the start of a Flow so they can be called from any subsequent script.

In this example, we define a token generation function in apiUtils.js and make it globally available via the output.utils namespace:

By loading the script in onFlowStart, the generateToken function becomes a reusable utility for the rest of the test execution:

The maestro Object

The maestro object is a built-in utility that provides information about the current test environment and captured UI data.

Property
Description

maestro.copiedText

Contains the text retrieved by the most recent copyTextFrom command.

maestro.platform

Identifies the OS: - ios - android - web This is useful for conditional cross-platform logic.

Capturing UI Text

To move text from your application into your JavaScript logic:

  1. Use the copyTextFrom command to store the content in the maestro.copiedText variable.

  2. Access that content in your JavaScript code or Maestro commands using maestro.copiedText.

The following example copies content from a userName element and uses it to send a dynamic message:

Next steps

Now that you already knows how to manage data when using JavaScript in Maestro Flows, access the following guides:

Last updated