Skip to content

Variables

Chapter lab: Variables and user keywordsch04-variables-keywords.zip · 3 KB

This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.

Download ch04-variables-keywords.zip

Or fetch and unpack it from a terminal in VS Code:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch04-variables-keywords.zip && unzip -q ch04-variables-keywords.zip && rm ch04-variables-keywords.zip

Open the folder in VS Code with code rfe-ch04-variables-keywords (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 3.13 or newer is required; uv installs it when it is missing.

Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.

What's inside
  • Directoryrfe-ch04-variables-keywords/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • imports.robot
    • Directoryresources/
      • common.resource
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

Variables come in three shapes, each with its own prefix:

  • Scalar — ${name} holds one value.
  • List — @{name} holds an ordered sequence.
  • Dictionary — &{name} holds key/value pairs.

Declare them in *** Variables ***, or create them inline with VAR (Robot Framework 7+) inside a test or keyword. Access a list item with ${list}[0] and a dictionary value with ${dict}[key].

Task 1 — Declare scalar, list, and dictionary variables

Section titled “Task 1 — Declare scalar, list, and dictionary variables”
Edit

Goal: create checks/variables.robot with one scalar, one list, and one dictionary variable, and a test that reads all three.

Here is the same shape over different data — a city, some days, and a few ages. Adapt the names and values to the shop.

Worked example — adapt it, don’t copy it

*** Variables ***
${CITY} Helsinki
@{DAYS} mon tue wed
&{AGES} ada=31 grace=45
*** Test Cases ***
Values Are Set
Should Be Equal ${CITY} Helsinki
Length Should Be ${DAYS} 3
Should Be Equal ${AGES}[ada] 31

Done when

checks/variables.robot defines ${SHOP}, @{PRODUCTS}, and &{PRICES}, and the assertions pass.

Hint 1

Declare a scalar ${SHOP}, a list @{PRODUCTS}, and a dictionary &{PRICES} in the *** Variables *** section.

Hint 2

Read them in a test: Should Be Equal ${SHOP} RF Demo Shop, Length Should Be ${PRODUCTS} 3, and dictionary access ${PRICES}[backpack].

Hint 3

Create one more variable inline with VAR ${note} checked inside a test.

Solution
*** Variables ***
${SHOP} RF Demo Shop
@{PRODUCTS} backpack lamp mug
&{PRICES} backpack=79 lamp=34 mug=19
*** Test Cases ***
Variables Are Defined
Should Be Equal ${SHOP} RF Demo Shop
Length Should Be ${PRODUCTS} 3
Should Be Equal ${PRICES}[backpack] 79
Var Creates Inline
VAR ${note} checked
Should Be Equal ${note} checked
Run

Run the suite:

Terminal window
uv run robot checks
Terminal window
robot checks
Terminal window
poetry run robot checks

Done when

the test Variables Are Defined passes.

Check your understanding

Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.

  1. Which prefix declares a list variable?

    Show the answer

    Answer: @{name} — ${x} is a scalar, @{x} a list, &{x} a dictionary.

  2. What does VAR do?

    Show the answer

    Answer: Creates a variable inline, in the current scope — VAR (Robot Framework 7+) creates a variable inline inside a test or keyword, with an optional scope.

Key terms

${} @{} &{}concept
Scalar, list, and dictionary variable prefixes.
VARkeyword
RF 7+ inline variable creation with optional scope (local/TEST/SUITE/GLOBAL).

All terms in the glossary →