4 Using Variables

Get an overview of variables and their supported operations in Oracle Communications Solution Test Automation Platform (STAP) Behavior-Driven Development (BDD) language.

Topics in this chapter:

Overview

Variables refer to pieces of data that are stored and used during the execution of a scenario. These variables can hold different types of information, such as numbers, text, or other data types, which are essential for the scenario's logic and flow.

Context refers to the storage of variable values saved during the execution of steps in a scenario.
  • A new context is created (or cleared) at the beginning of each scenario execution.
  • If the load context option is enabled in config.properties, the context is loaded for the scenario. The load context feature is only used at design time and not during the execution of scenarios in a pipeline.

Variable Lifecycle

  • Local variable: Local variables are available only for the duration of a scenario.
  • Global variables are prefixed with _ and are available from the time they are created until the end of the job.

For example, all variables defined using the Save keyword are local variables unless they begin with an underscore ( _ ).

In the example below, projectId and projectName are the variable values stored in the context.

Save:
#|  Property     |  Value  |
|  _projectId    |  id     |
|  projectName  |  name   |

projectId which is prefixed with _, is designated as Global variable and the context stores this variable value from the definition until the job execution completes ie., _projectId can be used in any scenario/case/step after its definition.

Note:

If the same variable name is used in the Save section of multiple steps, its value gets replaced.

To save and load the context, use config.properties context configuration. For more information, see "Context Folder".

Figure 4-1 shows the variables available during an execution job.

Figure 4-1 Loading Context Configurations



Using Array Variables

An array variable is a type of variable that stores multiple values in a single instance, making it useful for handling lists of data. When working with JSON path, an array variable helps in extracting and storing multiple values from a JSON structure.

The supported operations for array variables include:

  • Storing multiple values in a single variable.
  • Iterating over the array elements.
  • Accessing a single value from the array.

The examples below assume that you are starting with the following JSON:

{
  "subscriptions": [
    {
      "id": "1",
      "plan": "Premium",
      "status": "ACTIVE",
      "expiry": "2025-03-15"
    },
    {
      "id": "2",
      "plan": "Basic",
      "status": "EXPIRED",
      "expiry": "2024-01-01"
    }
  ]
}

Getting a single value from an array variable

To extract the plan value from the first element of the subscriptions array and append it to the users.plan array:

Save:
| $ARRAY{users.plan} | subscriptions[0].plan |
 

In this case, the users.plans array would have one element added to it: Premium.

Getting multiple values from an array variable

The following example shows how to get multiple values from an array variable:

Save:
| $ARRAY{users.plans} | subscriptions[*].plan |
 
#returns [Premium,Basic]

In this case, the users.plans array would have two elements added to it: Premium and Basic.

Adding a single value to an array variable

The following example shows how to add a single value to an array variable:
When add details, adding new subscription plan
Data:
| plan | Gold |
Validate:
| $status | 200 |
Save:
| $ARRAY{users.plan} | subscriptions[2].plan |
 
#returns Gold

Adding multiple values to array

The following example shows how to add multiple values to an array:
Then get mock response, read all values that are created above.
Validate:
| $status | 200 |
Save:
# Store a list of plans from the JSONPath *.plan into the array variable users.plans
| $ARRAY{users.plans} | subscriptions[*].plan |
In the above example , todos.id is the array created to save ids of all the tasks read.

Note:

If the todos.id array is already existing, the *.id array values are replaced. When we add an array to existing array indicates creating new array.

Using Dynamic Array Variable

Use ${index} to create dynamic array variable names. Only ${index} is allowed as a context variable or ID in array names.

Dynamic Array Variable Name

To use the index of an array to set the name of a variable:

 
RepeatTimes:
| $times | 2 |
Data:
| index | ${nextValue} |
| $urlSuffix | /getarray |
Validate:
| $status | 200 |
Save:
| $ARRAY{dynamicVariable_${index}} | subscriptions[?(@.status=='ACTIVE')].plan |

The following example shows how dynamic values are stored in the test context folder:

dynamicVariable_1=[Premium,Premium,Premium,Premium,Premium,Premium,Premium,Premium,Premium,Premium]
dynamicVariable_0=[Premium,Premium,Premium,Premium,Premium,Premium,Premium,Premium,Premium,Premium]

Using Array Variable Values

Arrays are used in controlled steps. Iteration happens for the number of times equivalent to length of the array.

To work with the indexes of an array variable,

  • Access the array value with index keyword ${index}. Index starts with 0.
  • ${nextValue} Gives the next element in the array. ${nextValue} Can be used in Data, Validate, or Save sections.

The following example shows how to add and read customer bill amounts using array variable values. First, create the array containing the variables:


Save:
| $ARRAY{bills1} | 25.213 |
| $ARRAY{bills1} | 20.378 |
| $ARRAY{bills1} | 21.643 |
| $ARRAY{bills1} | 24.211 |
| $ARRAY{bills1} | 22.113 |

Then set the code to iterate over the entire array:


RepeatTimes:
| $times | $ARRAY{bills1} |
Data:
| index | ${nextValue} |
Validate:
| $status | 200 |
| bills1[${index}] | $ARRAY{bills1[${index}]} |

For more information on array variables, see "Controlled actions".