Variables
Robot Framework variables are useful when:
- strings change often in the tasks. With variables you only need to make these changes in one place.
- creating system-independent tasks (
${RESOURCES}
instead ofc:\resources
,${HOST}
instead of10.0.0.1:8080
). Variables can be overwritten using command-line switches (--variable HOST:10.0.0.2:1234 --variable RESOURCES:/opt/resources
) or environment variables. - there is a need to have objects as arguments for keywords.
- different keywords need to communicate. Assign a return value from one keyword to a variable and pass it as an argument to another.
- values in the task data are long or complicated (
${URL}
is clearer thanhttp://long.domain.name:8080/path/to/service?foo=1&bar=2&zap=42
).
Readability and flexibility
*** Settings ***
Documentation Using variables for readability and flexibility.
Library RPA.Browser
Suite Teardown Close All Browsers
*** Variables ***
${I_FEEL_LUCKY_SEARCH_BUTTON} css:.FPdoLc > center:nth-child(1) > input:nth-child(2)
${SEARCH_URL} https://www.google.com/search?q=
${SEARCH_TERM} cute cat
*** Tasks ***
Search using hard-coded search URL and term
Open Available Browser https://www.google.com/search?q=cute+cat
*** Tasks ***
Search using variables
Open Available Browser ${SEARCH_URL}${SEARCH_TERM}
*** Tasks ***
Search using environment variables
Open Available Browser %{SEARCH_URL}%{SEARCH_TERM}
# No modifications needed when changing the search URL or the term.
# Configure the variables in Robocorp Cloud.
# Use devdata/env.json file for local testing.
*** Tasks ***
Search using local variables
${search_url} Set Variable https://duckduckgo.com/?q=
${search_term} Set Variable Robocorp
Open Available Browser ${search_url}${search_term}
*** Tasks ***
Click an element using a hard-to-understand locator
Open Available Browser https://www.google.com/
Click Element css:.FPdoLc > center:nth-child(1) > input:nth-child(2)
*** Tasks ***
Click an element using a well-named locator variable
Open Available Browser https://www.google.com/
Click Element ${I_FEEL_LUCKY_SEARCH_BUTTON}
Read more about using environment variables with Robocorp.
Different types of variables
Variable name consists of the type identifier ($
, @
, &
, %
), curly braces ({
, }
) and the variable name between the braces. Use capital letters with global variables in the *** Variables ***
section (${SEARCH_URL}
). Use small letters with local variables that are only available in certain tasks or user keywords (${search_url}
).
- Scalar:
${var}
- List:
@{var}
- Dictionary:
&{var}
- Environment:
%{var}
*** Settings ***
Documentation Using different kinds of variables.
*** Variables ***
${STRING} cute cat
${INT_AS_STRING} 1
${INT_AS_INT} ${1}
${FLOAT} ${3.14}
@{LIST} one two three
&{DICTIONARY} string=cat int=${1} [email protected]{LIST}
${ENVIRONMENT} %{PATH}
*** Tasks ***
Log variables
Log Many
... ${STRING} # cute cat
... ${INT_AS_STRING} # 1
... ${INT_AS_INT} # 1
... ${FLOAT} # 3.14
... ${LIST} # ['one', 'two', 'three']
... ${LIST}[0] # one
... ${LIST}[1:] # ['two', 'three']
... ${DICTIONARY} # {'string': 'cat', 'int': 1, 'list': ['one', 'two', 'three']}
... ${DICTIONARY}[string] # cat
... ${DICTIONARY.int} # 1
... ${DICTIONARY}[list][0] # one
... ${ENVIRONMENT} # entrypoints:bin:/Users/<username>/Projects/venv/bin...
Assigning variables
*** Settings ***
Documentation Assigning variables.
Library Collections
# You can create variables in a Python file and import them:
#Variables variables.py
*** Keywords ***
What Does The Cat Say
[Return] Meow!
*** Tasks ***
Assign variables
${string} Set Variable Hello, world! # ${string} = Hello, world!
@{list} Create List a b c # @{list} = [ a | b | c ]
&{dict} Create Dictionary key1=val1 key2=val2 # &{dict} = { key1=val1 | key2=val2 }
${a} ${b} ${c} Create List a b c # ${a} = a, ${b} = b, ${c} = c
${cat_says} What Does The Cat Say # ${cat_says} = Meow!
${evaluate} Evaluate datetime.date.today() # ${evaluate} = 2020-09-08
${inline_evaluation}
... Set Variable
... ${{datetime.date.today() + datetime.timedelta(1)}} # ${inline_evaluation} = 2020-09-09
Expressions are evaluated using Python's
eval
function. All Python built-in functions are available. All unrecognized Python variables are considered to be modules that are automatically imported. It is possible to use all available Python modules, including the standard modules and any installed third party modules.
Built-in variables
Variable | Description |
---|---|
{CURDIR} | The path to the task data file directory. |
${EMPTY} | Like the ${SPACE} , but without the space. Used to pass empty arguments. |
${EXECDIR} | The path to the task execution directory. |
${False} | Boolean False . |
${None} | Python None . |
${null} | Java null . |
${SPACE} | ASCII space (\x20 ). |
${TEMPDIR} | The path to the temporary directory. |
${True} | Boolean True . |
${/} | The directory path separator. / in UNIX-like systems and \ in Windows. |
${:} | The path element separator. : in UNIX-like systems and ; in Windows. |
${\n} | The line separator. \n in UNIX-like systems and \r\n in Windows. |
Runtime variables
Variable | Description |
---|---|
${DEBUG_FILE} | Debug file. |
${KEYWORD_MESSAGE} | The error message of the current keyword. |
${KEYWORD_STATUS} | The status of the current keyword, either PASS or FAIL . |
${LOG_FILE} | Log file. |
${LOG_LEVEL} | Log level |
${OUTPUT_DIR} | Output directory. |
${OUTPUT_FILE} | Output file. |
${PREV_TEST_MESSAGE} | The error message of the previous task. |
${PREV_TEST_NAME} | The name of the previous task, or an empty string if no tasks have been executed yet. |
${PREV_TEST_STATUS} | The status of the previous task: PASS , FAIL , or an empty string when no tasks have been executed. |
${REPORT_FILE} | Report file. |
${SUITE_DOCUMENTATION} | The documentation of the current task suite. |
${SUITE_MESSAGE} | The full message of the current task suite, including statistics. |
&{SUITE_METADATA} | The metadata of the current task suite. |
${SUITE_NAME} | The full name of the current task suite. |
${SUITE_SOURCE} | The path to the suite file or directory. |
${SUITE_STATUS} | The status of the current task suite, either PASS or FAIL . |
${TEST_DOCUMENTATION} | The documentation of the current task. |
${TEST_MESSAGE} | The message of the current task. |
${TEST_NAME} | The name of the current task. |
${TEST_STATUS} | The status of the current task, either PASS or FAIL . |
@{TEST_TAGS} | The tags of the current task in alphabetical order. |
I want to learn more!
Read more about variables in the Robot Framework User Guide.