Variables in Robot Framework are broadly similar to Python variables because RB is Python-based.
I will start with the basic variable types in RB.
Variable types
Robot Framework mainly uses three types: Scalar, List and Dictionary.
Scalar variables
A scalar variable is simply one variable containing one value, much like assigning a value to a Python name with a = 5.
Scalar syntax uses $ before the variable name.
*** Variables ***
${URL} http://google.com
${BROWSER} chrome
Robot Framework can assign strings or numbers to a scalar. Do not put quotes around a string:
*** Variables ***
${URL} 'http://google.com'
${BROWSER} "chrome"
In this example, both quote characters become part of the strings. Remove them to get the values you originally intended.
Once a scalar variable name has been declared, everything after = (or after two or more spaces) is treated as its value. The = is optional; whitespace between the name and value is enough.
*** Variables ***
${URL} http://google.com
${BROWSER} = chrome
If you use =, it must be separated from the value by two spaces:
*** Variables ***
${a}= 5
${b} = 6
${c} 7
This works, but I recommend the style used for b and c because it is easier to read. I personally lean towards c: one less = to type.
Be careful when assigning numbers:
*** Variables ***
${a} 5
${b} ${5}
Both a and b receive a number, but a is converted automatically while b is explicitly forced to a number.
The Evaluate keyword performs calculations:
*** Test Cases ***
Test
${c} Evaluate ${a} + 1
Log ${c}
${d} Evaluate ${b} + 1
Log ${d}
Both c and d receive the number 6.
With ${a} 5.0, a becomes a float. With ${a} 5.p, it becomes a string. And if 5.0 is passed into a string-oriented keyword such as:
*** Test Cases ***
Test
${c} Set Variable ${a} + '1'
Log ${c}
then c is the string 5.0 +'1'.
In short, scalar variables are automatically converted between integers, floats and strings where appropriate.
List variables
A Robot Framework list resembles a Python list, but the syntax is different: use @ instead of $.
*** Variables ***
@{BROWSERS} chrome firefox safari
List elements can be any kind of data: strings, numbers, lists, dictionaries and so on. You cannot declare a pure Python expression such as [1,2,3] or {'a':1,'b':2} directly; those values are treated as strings.
*** Variables ***
@{BROWSERS} chrome 5 [1, 2, 3] {'a': 1, 'b': 2}
To see the difference, include one list while declaring another:
*** Variables ***
@{a} chrome 5 [1, 2, 3] {'a': 1, 'b': 2}
@{c} [1, 2, 3] @{a}
*** Test Cases ***
Test
Log @{c}
The log from @{c} contains:
[1, 2, 3]as a string;chromeas a string;5as an integer;[1, 2, 3]as a string;{'a': 1, 'b': 2}as a string.
The original value of @{c} is [1,2,3],@{a}; list @{a} is unpacked inside @{c}.
To access one list element, use ${list}[index]: the selected value is treated as a scalar.
*** Variables ***
@{a} chrome 5 [1, 2, 3] {'a': 1, 'b': 2}
@{c} [1, 2, 3] @{a}
*** Test Cases ***
Test
Log ${a}[0]
For a nested list such as the one in @{c}, keep using scalar syntax and go deeper:
*** Test Cases ***
Log ${c}[1][1]
Dictionary variables
A Robot Framework dictionary is similar to a Python dictionary, just as a Robot list is similar to a Python list, but its declaration uses & instead of @.
*** Variables ***
&{a} chrome=5 firefox=6 safari=7
Access a dictionary key with ${dictionary}[key]:
*** Test Cases ***
Test
Log ${a}[chrome]
To summarise: Robot Framework uses @ for lists, & for dictionaries and $ for scalars. Individual values in lists and dictionaries use scalar syntax. Nested lists and dictionaries can be traversed in the same way.
Typical question: What is the difference between Scalar, List and Dictionary Variables?
Variable scope
Robot Framework has three main scopes: Global, Test Suite and Test Case.
Test case variables
If a variable is set in a test case with Set Variable, it exists only in that test case and cannot be accessed from the suite or another test case.
*** Test Cases ***
Test
${a} Set Variable 5
Test 2
Log ${a}
Running this reports that variable a cannot be found in Test 2. Likewise, a variable set inside a keyword cannot be accessed directly from a test case.
You can also use Set Test Variable, whose syntax is slightly different:
*** Test Cases ***
Test
Set Test Variable ${b} 5
Test 2
Log ${b}
In this situation the IDE does not complain, but execution still reports that b cannot be found in Test 2.
Test suite variables
A variable set in a suite can be accessed by every test case in that suite, but not by another suite. A variable declared in a suite's Variables section is a Suite Variable by default.
To declare one from inside a test case, use Set Suite Variable:
*** Variables ***
${a} 5
${B} 5
*** Test Cases ***
Test
Set Suite Variable ${c} 5
Set Suite Variable ${D} 5
Test 2
Log ${a}
Log ${B}
Log ${c}
Log ${D}
Everything in this example logs normally.
Global variables
A variable set at Global scope can be accessed from every test case in the suite, its child suites and later suites. The only way to declare one at Global scope is the Set Global Variable keyword.
*** Variables ***
${a} 5
${B} 5
*** Test Cases ***
Test
Set Global Variable ${c} 5
Set Global Variable ${D} 5
c and D are available to every test case in the suite, child suites and subsequent suites. ${a} and ${B} are not available to child or later suites.
Typical question: What is the difference between Global, Suite and Test Case Variables?
Variables from a variable file
When a Python file is imported in Settings, its variables are treated as Suite Variables.
*** Settings ***
Variables variables.py
Suppose the Python file contains:
a = 5
b = [1,2]
c = {'a':1,'b':2}
After importing the file in Settings, a, b and c are Suite Variables. Use them in the Robot file like the other variables:
*** Test Cases ***
Test
Log ${a}
Log ${b}
Log ${b}[1]
Log ${c}[a]
Typical question: How do you use variables from a variable file?