---
title: Using Variables in Rapi
tags: Rapi Document English
description: Using Variables in Rapi
---
# Using Variables in Rapi
## Declaring Local Variables Using store Command Series
In Rapi, local variables can be declared and set with values by store command series. For example, given the following command:
`storeText | //input[@name='q'] | var_keyword`
After executing the command, the value of variable `var_keyword` will be set to the target element’s value, say “iphone”. After that, the variable can be used in another command's target or value via the form `${var_keyword}`.
## Declaring Global Variables Using Global Var Panel or storeGlobalVar Command

Besides, global variables can be declared and set with initial values via the `Global Var` panel. A user can either manually add variables or import the variables from a file of JSON or CSV format. After defining the variables, they can be exported as a file for further usage. Another way to create a global variable is using `storeGlobalVar` command. The following example shows that a global variable `GlobalVarA` will be created with an initial value `newValue` if `GlobalVarA` is not declared in `Global Var` panel in advance.
`storeGlobalVar | newValue | GlobalVarA`
## Mixed Usage of Local Variables and Global Variables
Mixed usage of local and global variables is not recommended, i.e., to use the same variable name for both local variable and global variable. If it is inevitable, please read the following rules carefully.
|v is predefined as ↓|A. store\|x\|v|B. storeGlobalVar\|x\|v| C. storeXXX\|x\|v|
|-|:-:|:-:|:-:|
|**1. local variable**|**Update** the local variable|**Create** a global variable|**Update** the local variable|
|**2. global variable**|**Create** a local variable|**Update** the global variable|**Update** the **global** variable|
|**3. local and global variables**|**Update** the local variable|**Update** the global variable|**Update** the local variable|
|**4. not yet defined**|**Create** a local variable|**Create** a global variable|**Create** a local variable|
A mixed usage example is as follow:
:::info
storeGlobalVar | globalStr | Var1
storeText | Google| Var1
storeText | Rapi | Var2
store | localStr | Var1
storeText | Rapi | Var1
:::
The execution process of the example is:
1. **Create** a global variable `Var1` with value `globalStr`. (rule `B4`)
2. **Update** the value of the global variable `Var1` to `Google`. (rule `C2`)
3. **Create** a local variable `Var2` with value `Rapi`. (rule `C4`)
4. **Create** a local variable `Var1` with value `localStr`. (rule `A2`)
5. **Update** the value of the local variable `Var1` to `Rapi`. (rule `C3`)
## Variable Types
Interpreting a variable in a command's target or value is a string replacement - replacing `${var_name}` by the variable’s runtime value. Therefore, using a variable as string type in a Javascript expression of a command (e.g., `runScript`) should be placed within quotation marks. For example, if you want to compare a variable to a string, use
`"${var_name}" == "Guest"`
If your string already contains both single and double quotation marks, consider using `String.raw()`
``String.raw`${var_name}` == "Guest"``
If you want to compare a variable to an integer, use
`${var_num} == 100`
In Rapi, if you use `storeEval` command to create a variable and the return value of Javascript expression is array type, it will be converted to a JSON string when it is interpreted. For example, given the following commands:
:::info
storeEval | return ["S","i","d","e","e","X"] | arr
storeEval | return \${arr}.length | length
store | 0 | index
WHILE | \$\{index\} < \$\{length\}
storeEval | return \$\{arr\}\[\$\{index\}\] | show
echo | \$\{show\}
storeEval | return \$\{index\} + 1 | index
END
:::
After executing the above commands, Rapi will iteratively echo each value from the array `arr`.
## Nested Variable Interpretation Format
In Rapi, interpreting variables can be nested. For example, given the following commands:
:::info
store | 1 | Ser
store | Hello Rapi | Msg1
echo | \${Msg${Ser}} |
:::
After executing the above commands, Rapi will echo `Hello Rapi` in Log. `${Ser}` will be set to `1` at first, so `Msg${Ser}` will be evaluated as `Msg1`. Then `${Msg1}` will be set to `Hello Rapi`.
## Importing a JSON File Containing Arrays and Objects for Data-Driven Testing
In Rapi, you can use `WHILE`, `INCLUDE`, and `Nested Variable` to complete Data-Driven Testing.
1. Go to Global Var panel and import a JSON file containing arrays and objects. The following example shows two records to be used as the data for step 2.
```jsonld=
{
"numberOfRecords":2,
"record":[
{
"userName":"User1",
"loginMsg":"Welcome"
},
{
"userName":"User2",
"loginMsg":"Please signup"
}
]
}
```
2. Use `Nested Variable` to refer to these records in test cases. (Variable `index` will be defined in step 3.)
:::info
**ExampleTestSuite.MainTestCase**
echo \| \${record.\${index}.userName} \|
echo \| \${record.\${index}.loginMsg} \|
:::
3. Create a leading test case to iteratively execute the main test case. In the following example, the main test case will be run twice, and the values of the two records will be echoed.
:::info
**ExampleTestSuite.LeadingTestCase**
store \| 0 \| index
WHILE \| \${index} < ${numberOfRecords} \|
INCLUDE \| ExampleTestSuite.MainTestCase \|
storeEval \| return \${index}+1 \| index
END
:::
## File Output
After the execution of test cases, the last values of the local and global variables can be obtained from the test reports. Please refer to [Rapi Test Report Specification](https://hackmd.io/@sideex/book-zh/%2F%40sideex%2Freportspec)。