Skip to main content

Variables

This page summarizes the most important information about variables in Robot Framework.
For more information, please refer to the official Robot Framework User Guide.

*** Variables *** Section

Variables which are defined in the *** Variables *** section are available in all test cases and keywords in the same file.
Variables defined in the *** Variables *** section are suite variables.
If a .resource or a .robot file with a *** Variables *** section is imported into a test suite, the variables there also become suite variables.

Example:

*** Variables ***
${my_var} my_value
@{my_list} Apple Banana Orange
&{my_dict} name=my_value1 password=my_value2

*** Test Cases ***
Test Case 1
Log ${my_var}
Log ${my_list}
Log ${my_dict}
FOR ${item} IN @{my_list}
Log ${item}
END
Log ${my_dict}[name]
Log ${my_dict}[password]
FOR ${key} ${value} IN &{my_dict}
Log Many ${key} ${value}
END
My Keyword

*** Keywords ***
My Keyword
Log Many ${my_var} ${my_list} ${my_dict}
Let me run it

Set Variables in Test Cases and Keywords

Besides the *** Variables *** section, variables can also be set dynamically in test cases and keywords. Variables are set by the return value of a keyword. There are some Keywords which set variables values explicitly, like

They can be used to either set a value of a variable or change the scope of a variable.

Example:

*** Test Cases ***

Test Case 1
${my_local_var} Set Variable Hello World
Log ${my_local_var} # Pass: Logs the value of the variable
Set Suite Variable ${my_suite_var} I'm a suite variable
Set Global Variable ${my_global_var} I'm a global variable

Test Case 2
Log ${my_local_var} # Fails: Variable only exists in the scope of Test Case 1
Log ${my_suite_var} # Pass: Variable exists for the scope of the whole suite
Log ${my_global_var} # Pass: Variable exists for the scope of the whole test run

Test Case 3
My Keyword
Log ${my_keyword_var} # Fails: Variable only exists in the scope of My Keyword
Log ${my_test_var} # Pass: Variable exists in the scope of the test case

*** Keywords ***
My Keyword
${my_keyword_var} Set Variable Hello Keyword
Log ${my_keyword_var} # Pass: Logs the value of the variable
Set Test Variable ${my_test_var} I'm a test case variable
Let me run it

When to use $ and @ and & and %?

Variable Names consist of

  • a variable type identifier (e.g. $, @, &, %)
  • curly braces {}
  • a variable name (e.g. my_var, my_list, my_dict, my_var2)

The variable type identifier is used to define the type of the variable.

  • $ is used for scalar variables.
  • @ is used for list variables.
  • & is used for dictionary variables.
  • % is used for environment variables.

What is the difference between a scalar variable, a list variable and a dictionary?
A scalar variable can only contain one value.
A list variable can contain multiple values.
A dictionary variable can contain multiple key-value pairs.

But why can we access a list variable my_list with the syntax ${my_list} and also with the syntax @{my_list}?

${my_list} accesses the whole list object my_list. It is a container and holds all the items of the list. You can use it to access the whole list variable my_list or to access a specific item of the list with the syntax ${my_list}[index].

@{my_list} accesses the items of the list variable my_list. It is like the list variable my_list is unpacked and all the items are available as individual variables.

Example:

*** Variables ***
@{my_list} Apple Banana Orange

*** Test Cases ***
Test Case 1
Log ${my_list} # Pass: Logs the whole list object
Log Many @{my_list} # Pass: Logs the items of the list object
Log Many ${my_list} # Pass: Logs the whole list object
Log List ${my_list} # Pass: Logs the whole list object
Log ${my_list}[0] # Pass: Logs the first item of the list object
Log @{my_list}[0] # Fail: @{my_list} is not a list object and does not have an index
Log @{my_list} # Fail: The second argument of the Log keyword `level` only allows the values `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `NONE`.
# The value `Banana` for the argument `level` is not allowed.
Log ${my_list}[0] ${my_list}[1] ${my_list}[2] # Fail: Same as above
FOR ${item} IN @{my_list} # Pass: Iterates over the items of the list object
Log ${item} # Pass: Logs the items of the list object
END

Whenever you want to access the container of a list variable, you have to use the syntax ${my_list}.
When you want to break down the list variable into its items, you have to use the syntax @{my_list}.

It's like a beer crate with bottles inside.
With the syntax ${beer_crate} you can access the whole beer crate.
With the syntax @{beer_crate} you get the bottles.
With the syntax ${beer_crate}[index] you get a specific bottle inside the crate.

Variable Files

It is possible to load variables from external files, like Python (.py) or .yaml files.
For Yaml files, the package pyyaml is required.

pip install pyyaml

.py files will be interpreted as Python Code, so you can create simple variables, lists or dictionaries and even complex objects like classes.

PythonVariables.py
class TestEnv:
ip = '123.4.5.6'
user = 'robot'
roles = ['admin', 'user']

my_var = 'Hello World'
my_list = ["Apple", "Banana", "Cherry"]
my_dict = {'name': 'John', 'age': 36}

Even dynamic variables using are possible by executing any Python code inside the variable file.

DynamicVariables.py
import os
import random
import time
import math

USER = os.getlogin() # current login name
RANDOM_INT = random.randint(0, 10) # random integer in range [0,10]
CURRENT_TIME = time.asctime() # timestamp like 'Thu Apr 6 12:45:21 2006'
if time.localtime()[3] > 12:
AFTERNOON = True
else:
AFTERNOON = False

def get_area(diameter):
radius = diameter / 2
area = math.pi * radius * radius
return area

AREA1 = get_area(1)
AREA2 = get_area(2)

.yaml (or .yml) files will be interpreted as a dictionary.

YamlVariables.yml
# These are scalars:
base_url: https://qaserver.mycompany.com:8080
admin_user: iAmAdMiN
admin_password: eieioscoobydoo1234
# Now, a list:
yaml_list:
- Item 1
- Item two
- 3
# Finally, a dictionary:
yaml_dict:
key_1: A string
key_2: 1 # an int
TestSuite.robot
*** Settings ***
Variables PythonVariables.py
Variables YamlVariables.yaml
Variables DynamicVariables.py

*** Test Cases ***
Access Python Variables
Log ${TestEnv.ip}
Log ${TestEnv.roles}
Log Many @{TestEnv.roles}
Log ${my_var}
Log Many @{my_list}
Log Many &{my_dict}
FOR ${item} IN @{my_list}
Log ${item}
END

Access Yaml Variables
Log ${base_url}
Log Many @{yaml_list}
Log ${yaml_dict}
Log ${yaml_dict}[key_1]

Access Dynamic Variables
Log ${USER}
Log ${RANDOM_INT}
Log ${CURRENT_TIME}
Log ${AFTERNOON}
Log ${AREA1}
Log ${AREA2}
Let me run it