The Variables feature allows you to temporarily store values from a UI for later reference. For example, you can store the text of a UI element as a variable and then check if it matches the text of another UI element on a different page.
Table of Contents
- Store values as variables
- Refer to variable values
- Special variables
- Secret variables
- Share variables between test cases
- Date format for the "Date calculation” command
- Order in which variables are set
1. Store values as variables
You can use different commands to store values as variables.
- Store value: Stores element values on the page as variables (see Figure 1).
- Store current second based unique value: Stores a unique value each time the test is executed. This is useful in many cases where a different value needs to be used every time a test is run (ex. tests that require different IDs for each user registration).
- Store fixed value: Stores specified values as variables.
Figure 1. The Store value command.
2. Refer to variable values
Refer to stored variables using the format ${variable name} (see Figure 2).
Figure 2. Refer to variables.
Note that:
- The stored variables can only be referenced within same test case.
- When storing variables, name them without using ${...}. Use this symbols when referring to them later in the test case.
- Variables can also be referenced from locators using the ${variable name} format. Learn more about adding and editing locators here.
3. Special variables
MagicPod defines special variables that can be referenced directly using ${variable name} without needing to store them first.
ENVIRONMENT | Mobile app/browser | Test execution environment. Any of “local_pc”, “magic_pod”, “browserstack”, “headspin”, “remote_testkit”, or “saucelabs”. If “Cloud” is selected, this is “magic_pod” |
OS | Mobile app/browser | For the mobile app, this is either “ios” or “android”, and for the browser, this is either “windows”, “mac”, or “linux.” |
VERSION | Mobile app | OS version “13.3” or “10”, etc. |
MODEL | Mobile app | Model. “iPhone 8”, “Nexus 5X”, etc. |
DEVICE_TYPE | Mobile app/browser | Device type. For the mobile app, this is either “simulator”, “emulator”, “arm64_v8a_emulator”, or “real_device”, and for the browser, this is either “desktop” or “mobile_emulation_by_chrome” |
DEVICE_REGION | Mobile app | Device region. Either “Default”, “AU”, “BR”, “CA”, “CN”, “FR”, “DE”, “IN”, “ID”, “IT”, “JP”, “MX”, “NL”, “RU”, “SA”, “KR”, “ES”, “CH”, “TW”, “TR”, “GB”, or “US” |
DEVICE_LANGUAGE | Mobile app | Device language. Either “default”, “en”, “ja”, or “ko” |
APP_PACKAGE | Mobile app | Android app package name. Can be used only with Android. “io.appium.android.apis”, etc. |
BROWSER | Browser | Browser name. Either “chrome”, “firefox”, “internet_explorer”, “safari”, or “edge” |
BASE_URL | Browser | “Base URL” specified in the test execution settings |
- Special values may differ from actual device information if specified in test execution settings. Values such as VERSION, MODEL, DEVICE_TYPE, DEVICE_REGION, and DEVICE_LANGUAGE are the values specified in the test execution settings, and may differ from the information of the device that is actually used. For example, when specifying the model for external cloud services as iPhone, the MODEL value is also iPhone rather than the exact model name (i.e., iPhone 7).
- These special values cannot be referenced if a variable if overwritten by a variable save command or by a secret variable as described below.
4. Secret variables
To keep sensitive information like API tokens and passwords hidden, use secret variables.
- Register secret shared variables in the Advanced Settings tab of the test run settings panel.
- Enter the variable name and value, then click the lock symbol to mark it as secret (see Figure 3).
- Reference secret shared variables using the same notation as variables ${...}.
Figure 3. Set up secret shared variables.
Figure 4. Define authentication tokens as shared variables with Web API call.
Secret shared variable values are masked on the test result logs as “********”.
Please note that masked values may still be visible in certain scenarios, such as if text entries are made outside password fields.
5. Share variables between test cases
Use shared variables when running batch tests
You can register common values like login information as shared values.
Open the test run settings panel and select the Advanced settings tab and register the variables (see Figure 5).
Figure 5. Set up shared variables.
Use shared variables from the command line
There are two ways to run test cases with shared variables from the command line.
1. Directly in the magic_pod_config.json file
For example, let’s assume that the test was originally run on a JSON file such as this one.
{
"owner": "SampleCompany",
"project": "SampleApp",
"capabilities": {
"platformName": "iOS",
"platformVersion": "10.3",
"deviceName": "iPhone 7 Plus",
"app": "${HOME}/magicpod_demo_app.app",
"automationName": "XCUITest"
},
"sendMail": true,
"workDir": "${HOME}/magicpod-work"
}
This is somewhat complicated, but if you add the following configuration, you can pass shared variables for execution. In this example, the two variables USERNAME and PASSWORD (PASSWORD is a secret shared variable) are set.
{
"owner": "SampleCompany",
"project": "SampleApp",
"capabilities": {
"platformName": "iOS",
"platformVersion": "10.3",
"deviceName": "iPhone 7 Plus",
"app": "${HOME}/magicpod_demo_app.app",
"automationName": "XCUITest"
},
"sendMail": true,
"workDir": "${HOME}/magicpod-work",
"testCondition": {
"shared_variables": [
{
"key": "USERNAME",
"value": "test_user",
"secret": false
},
{
"key": "PASSWORD",
"value": "secret_password",
"secret": true
}
]
}
}
Use the same commands to run the test as for normal command line execution.
2. Set up using environment variables
When you want to manage secret shared variable values in a stricter way without leaving them in a file, you can also set them using environment variables. In this case, the description of the magic_pod_config.json file is as follows.
{
"owner": "SampleCompany",
"project": "SampleApp",
"capabilities": {
"platformName": "iOS",
"platformVersion": "10.3",
"deviceName": "iPhone 7 Plus",
"app": "${HOME}/magicpod_demo_app.app",
"automationName": "XCUITest"
},
"sendMail": true,
"workDir": "${HOME}/magicpod-work",
"testCondition": {
"shared_variables": [
{
"key": "USERNAME",
"value": "test_user",
"secret": false
},
{
"key": "PASSWORD",
"value": "${PASSWORD}",
"secret": true
}
]
}
}
In this case, the actual value must be given at the time of execution. Therefore, the command to be run is as follows.
PASSWORD=secret_password "MagicPodDesktop.app/Contents/MacOS/MagicPodDesktop" run \
Full path to --magic_pod_config=magic_pod_config.json
6. Date format for the "Date calculation” command
When using the Date calculation command, there are cases where you cannot achieve what you need to do by using only Date type provided from the start (for example, if you want to get a description of a month that includes zeroes, such as 07(July)). In such a case, you can specify various Date format by setting the Date type to Custom date format. For example, if you want to store the month value in the form of a zero-padded variable, you can specify MM in the Date format.
Figure 6. Date format for displaying the month with zeroes included
In addition, by writing EEE, MM/dd/yyyy, hh:mm:ss, you can save the date and time, as Thu, 07/14/2022, 10:55:00, in a variable. The library called date-fns can be used to describe Date format. See here for a more detailed method of description.
You can also use variables in the Date format input area. For this reason, you can specify, for example, the string MM to be stored in advance in the variable MONTH_FORMAT and ${MONTH_FORMAT} for the date format.
The Date calculation command is in the System category. Take note of this.
Figure 7. The Date calculation command
7. Order in which variables are set
When there are multiple data pattern values and variables, variables are set in the following order:
- Shared variables
- Multi-lang data pattern
- Data pattern
- Command for saving variables in a test script
Variables that have been set can be referenced using ${...}. If a variable with the same name is set again, the old value will be overwritten.