Skip to content

Libraries and resources

Chapter lab: Keywords and importsch03-keywords-imports.zip · 3 KB

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

Download ch03-keywords-imports.zip

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

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

Open the folder in VS Code with code rfe-ch03-keywords-imports (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-ch03-keywords-imports/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • imports.robot
    • Directoryresources/
      • common.resource
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

Keywords arrive in a suite two ways, both declared in *** Settings ***:

  • Library imports Python-backed keywords — the standard libraries (Collections, String, OperatingSystem, …) and third-party ones.
  • Resource imports keywords and variables from a shared .resource file you write in Robot Framework syntax.

This chapter’s workspace already has a resource at resources/common.resource imported by checks/imports.robot. Add a library import alongside it.

Task 1 — Import the Collections library and use one of its keywords

Section titled “Task 1 — Import the Collections library and use one of its keywords”
Edit

Goal: in checks/imports.robot, import the Collections library and add a test that builds a list and checks its length.

Here is the same pattern with a different library — importing String and calling one of its keywords. Adapt it to Collections.

Worked example — adapt it, don’t copy it

*** Settings ***
Library String
*** Test Cases ***
Path Splits Into Parts
@{parts}= Split String a/b/c /
Length Should Be ${parts} 3

Done when

checks/imports.robot imports Collections and calls Create List, and the new test passes.

Hint 1

Add Library Collections to the *** Settings *** section, alongside the existing Resource import.

Hint 2

Add a test that builds a list with Create List and checks it with Length Should Be.

Hint 3

${items}= Create List backpack lamp mug, then Length Should Be ${items} 3.

Solution
*** Settings ***
Resource ../resources/common.resource
Library Collections
*** Test Cases ***
Catalog List Has Items
${items}= Create List backpack lamp mug
Length Should Be ${items} 3
Run

Run it:

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

Done when

the test Catalog List Has Items passes.

YouTube video · 5:10Pekka's Demos — Resource files in Robot Framework

Watch a test case become simpler and its keywords and variables reusable by extracting them into a resource file.

Check your understanding

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

  1. What is the difference between a Library and a Resource import?

    Show the answer

    Answer: A Library adds keywords from Python; a Resource adds keywords/variables from a .resource file — Library imports Python-backed keywords (e.g. Collections); Resource imports keywords and variables from a .resource file.

  2. Where do you declare imports?

    Show the answer

    Answer: *** Settings *** — Library and Resource imports go in the *** Settings *** section.

Key terms

Libraryconcept
Imports Python-backed keywords, e.g. Library Collections.
Resourceconcept
Imports keywords and variables from a shared .resource file, e.g. Resource ../resources/common.resource.

All terms in the glossary →