# Variables

<div class="view-markdown-wrapper">
<ViewMarkdown />
</div>

Variables are a core mechanism for storing and reusing dynamic values throughout a test execution.
They allow your tests to adapt to changing data, reduce duplication, and remain stable across different runs.

This section explains how variables work across different actions and steps, including their types, scope, persistence, and usage syntax.

## Table of Contents

1. [Variable Types](#variable-types)
   - [Pre-defined Variables](#pre-defined-variables)
   - [Dynamic Variables](#dynamic-variables)
2. [Creating Variables During Execution](#creating-variables-during-execution)
   - [Via Extract Action](#via-extract-action)
   - [Via Natural Language Instructions](#via-natural-language-instructions)
3. [Using Variables](#using-variables)
   - [In Natural Language (AI Mode)](#in-natural-language-ai-mode)
   - [In Code Blocks (JavaScript)](#in-code-blocks-javascript)

## Variable Types

### Pre-defined Variables

Pre-defined variables are configured before a test runs and are typically used for static or configuration-level values.

- Defined in [Settings > Variables](/cloud/settings#_4-variables)
- Persist across all test runs
- Available globally or within specific environments
- Shared across tests in your organization

### Dynamic Variables

Dynamic variables are created during test execution and are used to store values discovered or generated at runtime.
They are commonly generated by actions such as Extract, or implicitly through natural language instructions.

- Created on-the-fly during a test run
- Only exist for the duration of the current execution
- Reset at the start of each run and discarded when the test completes
- Pass data between test steps

## Creating Variables During Execution

In addition to configuring variables in Settings, variables can be created dynamically during test execution.

### Via Extract Action

The Extract action captures content from the page and stores it in a dynamic variable for later use.
This approach is recommended when values need to be explicitly extracted from the UI.

For details on how to configure extraction, see [Extract](/cloud/test-editor/actions#_6-extract).

### Via Natural Language Instructions

Variables can also be created implicitly through natural language instructions, without configuring an explicit Extract action.

Examples:

```
Save the order number to orderId
Save the total price to totalAmount
Save the count of items to itemCount
Extract the confirmation message and save to confirmText
```

## Using Variables

### In Natural Language (AI Mode)

There are two ways to reference variables in natural language statements, and they behave differently:

#### 1. Mustache Syntax: <code v-pre>{{ variableName }}</code> (Value Replacement)

Use double curly braces when you want the **current value** to be substituted **before** the AI generates the action. The generated action will cache the literal value.

```text
Navigate to {{ baseUrl }}/dashboard
Type {{ userEmail }} in the email field
```

**Example:** If `userEmail = "test@example.com"`, the statement <code v-pre>Type {{ userEmail }} in the email field</code> becomes `Type test@example.com in the email field` before being sent to the AI. The generated action caches `input_text test@example.com`.

**Use this when:**

- The value is static or configuration-based (URLs, API endpoints)
- You want the action to always use the value from generation time
- The variable won't change between test runs

#### 2. Plain Variable Name (Dynamic Placeholder)

Use the variable name directly (without curly braces) when you want the AI to recognize it as a **placeholder** for dynamic runtime values. The agent knows your environment variable names and will generate an action that references the variable.

```
Type userEmail in the email field
Enter the password variable in the password field
```

**Example:** If `userEmail = "test@example.com"`, the statement `Type userEmail in the email field` tells the AI that `userEmail` is a variable. The generated action caches <code v-pre>input_text {{ userEmail }}</code>, which resolves to the actual value at runtime.

**Use this when:**

- The value may change between test runs (different environments, test accounts)
- You want the same cached action to work with different variable values
- The variable is environment-specific (credentials, user data)

#### Summary

| Syntax                             | When Value is Resolved | Cached Action Example                         | Best For                   |
| ---------------------------------- | ---------------------- | --------------------------------------------- | -------------------------- |
| <code v-pre>{{ userEmail }}</code> | Generation time        | `input_text test@example.com`                 | Static config values       |
| `userEmail` (plain)                | Runtime                | <code v-pre>input_text {{ userEmail }}</code> | Dynamic/environment values |

::: tip Clearing Cached Actions
To regenerate a cached action, toggle the step between AI action and non-AI action mode. This clears the cache and will generate fresh action data.
:::

### In Code Blocks (JavaScript)

```javascript
// Access variables using testContext
console.log(testContext.userEmail);
const price = testContext.expectedPrice;

// Assign to a variable that can be used in future steps
testContext.actualPrice = "9.99";
```
