Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 36 ยท Keywords and Variables

Scalar, List and Dictionary Variables

Learn Robot Framework variables: ${scalar}, @{list}, &{dict} and %{ENV} syntax, the Variables section, VAR, item access, slicing, inline Python and typed values.

Variables hold values you want to reuse or compute. In Robot, the sign in front of the braces tells you what kind of variable it is: ${...} for a single value, @{...} for a list expanded into separate arguments, &{...} for a dictionary expanded into named arguments, and %{...} for an operating-system environment variable. This lesson covers creating, reading and converting them.

The four kinds

Syntax Kind Example
${name} Scalar: any single value (string, number, list, object...) ${user}
@{name} List expansion: use a list as separate arguments Log Many @{colors}
&{name} Dictionary expansion: use a dictionary as key=value arguments Create User &{profile}
%{NAME} Environment variable %{HOME}

Names are case-insensitive, and spaces and underscores are ignored, so ${My Var}, ${my_var} and ${MYVAR} are the same variable. By convention, constants in the Variables section are ${UPPER_CASE} and variables inside keywords are ${lower_case}.

Creating variables

In the Variables section (available to the whole file):

ROBOTFRAMEWORK
*** Variables ***
${URL}          https://example.org
${TIMEOUT}      ${30}
@{COLORS}       red    green    blue
&{USER}         name=Ada    role=admin    age=${36}
${TOTAL}        ${TIMEOUT} seconds

Inside a test or keyword:

ROBOTFRAMEWORK
*** Test Cases ***
Make variables
    ${name}=      Set Variable    Ada
    @{colors}=    Create List     red    green
    &{user}=      Create Dictionary    name=Ada    role=admin

    VAR    ${greeting}    Hello, ${name}!
    VAR    @{numbers}     1    2    3
    VAR    &{point}       x=1    y=2

VAR is available from Robot 7.0. It creates a variable of the right type without a keyword call, and takes an optional scope= (TEST, SUITE, GLOBAL, LOCAL). In older versions use the keywords Set Variable, Create List, Create Dictionary.

Strings or real numbers?

A value written in a cell is a string. To get a different type, use a variable-style literal:

Write You get
${42} integer 42
${3.14} float 3.14
${True}, ${False} booleans
${None} Python None
${EMPTY} empty string
${SPACE} one space (${SPACE * 4} for four)
${NULL} None-like empty value, rarely needed

For example ${count} ${5} is a number, ${count} 5 is the text "5". Comparisons such as ${count} > 3 work in expressions either way, but keywords like Should Be Equal do not convert.

Reading list and dictionary items

ROBOTFRAMEWORK
*** Test Cases ***
Access items
    @{colors}=    Create List    red    green    blue
    &{user}=      Create Dictionary    name=Ada    age=${36}

    Should Be Equal    ${colors}[0]     red
    Should Be Equal    ${colors}[-1]    blue
    Should Be Equal    ${user}[name]    Ada
    Should Be Equal    ${user.name}     Ada
    Should Be Equal As Integers    ${user}[age]    36
    ${slice}=    Set Variable    ${colors}[0:2]
    Log Many    @{colors}
  • ${list}[index] uses zero-based indexes; negative indexes count from the end; ${list}[1:3] is a slice.
  • ${dict}[key] reads a dictionary value; ${dict.key} works when the key is a plain word.
  • Items can be nested: ${data}[users][0][name].
  • @{list} and &{dict} expand only where you use them as arguments. Use the ${list} scalar form to pass the whole list as one argument.

Computed values with inline Python

Wrap Python in ${{ ... }} for quick calculations or conversions:

ROBOTFRAMEWORK
*** Test Cases ***
Inline Python
    ${total}=    Set Variable    ${{ 2 + 3 * 4 }}
    ${upper}=    Set Variable    ${{ 'robot'.upper() }}
    ${items}=    Set Variable    ${{ [1, 2, 3] }}
    Should Be Equal As Integers    ${total}    14

The expression runs with a few modules available (math, os, re, datetime and others). For anything longer, use Evaluate or put the logic in a library.

Environment variables

%{USERNAME} reads an environment variable and fails if it does not exist; give a default with %{USERNAME=guest}. Keywords Set Environment Variable, Get Environment Variable and Remove Environment Variable (from OperatingSystem) change them for the rest of the run.

Stand-in 1: resolving items

The blocks below are not Robot Framework. This stand-in resolves ${name}, ${name}[index], ${name}[key] and ${name.attr} against a dictionary of variables, with Robot's name normalization.

Python Stand-in: variable lookup

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in 2: strings versus numbers

The most common beginner surprise: 5 is text, ${5} is a number. The stand-in imitates how a cell is converted, and what Should Be Equal would say about each pair.

Python Stand-in: ${5} versus 5

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

(Real Should Be Equal compares with ==, so ${5} and 5 fail because an integer is not equal to a string. The stand-in also checks types to make the point visible.)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does @{COLORS} do when used as an argument?

    @{list} spreads the items as individual arguments; ${list} passes the list itself.
  2. 2. How do you read the last item of ${colors}?

    Indexes are zero-based; negative ones count from the end.
  3. 3. What is the type of ${count} after: ${count} Set Variable 5 ?

    A plain cell is a string. Write ${5} (or convert it) to get an integer.
  4. 4. Which are the same variable?

    Robot ignores case, spaces and underscores in variable names.

Summary

  • ${} is a scalar, @{} expands a list, &{} expands a dictionary, %{} reads the environment.
  • Create variables in the Variables section, with Set Variable, Create List, Create Dictionary or VAR (Robot 7).
  • Cells are strings; ${42}, ${True}, ${None} and ${EMPTY} create other types.
  • Read items with ${list}[0], ${dict}[key] and ${dict.key}; compute with ${{ expression }}.
  • Variable names ignore case, spaces and underscores.

Next: Variable Scopes and Built-in Variables