Skip to content

The dynamic API underneath

Chapter lab: Libraries that scalech19-libraries-that-scale.zip · 4 KB

This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.

Download ch19-libraries-that-scale.zip

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

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch19-libraries-that-scale.zip && unzip -q ch19-libraries-that-scale.zip && rm ch19-libraries-that-scale.zip

Open the folder in VS Code with code rfe-ch19-libraries-that-scale (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-ch19-libraries-that-scale/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • smoke.robot
    • Directorylibraries/
      • README.md
    • Directoryvariables/
      • README.md
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

DynamicCore is convenient because it implements Robot Framework’s dynamic library API for you. It is worth seeing that API directly, once — both to demystify PythonLibCore and to know what to do in the rare case you need it.

A dynamic library answers two questions at run time:

  • get_keyword_names(self) — the list of keyword names the library provides.
  • run_keyword(self, name, args, named) — run the named keyword with its arguments and return the result.

You reach for this only when the keywords aren’t known until run time — generated from a data file, a schema, or a remote service. When you know your keywords at authoring time (almost always), a static or PythonLibCore library is simpler.

The dynamic library API at run timesequence diagram. A dynamic library answers these two methods at run timeget_keyword_names()RF asks which keywords exist1run_keyword(name, args)RF calls one keyword2run_keyword(name, args)...once per keyword call3A dynamic library answers these two methods at run time
Edit

Goal: create libraries/EchoLibrary.py — a class that reports two keyword names, Echo Value and Shout Value, and runs them in run_keyword.

Here is the same shape with different keywords. Adapt it to echo and shout.

Worked example — adapt it, don’t copy it

class MathLibrary:
def get_keyword_names(self):
return ["Double Value", "Negate Value"]
def run_keyword(self, name, args, named=None):
if name == "Double Value":
return int(args[0]) * 2
if name == "Negate Value":
return -int(args[0])
raise AttributeError(name)

Done when

libraries/EchoLibrary.py defines an EchoLibrary class with the dynamic API.

Hint 1

get_keyword_names returns the two names as a list: return ["Echo Value", "Shout Value"].

Hint 2

run_keyword dispatches on name: return args[0] for Echo Value, and str(args[0]).upper() for Shout Value; raise AttributeError(name) otherwise.

Solution
class EchoLibrary:
def get_keyword_names(self):
return ["Echo Value", "Shout Value"]
def run_keyword(self, name, args, named=None):
if name == "Echo Value":
return args[0]
if name == "Shout Value":
return str(args[0]).upper()
raise AttributeError(name)
Run

Create checks/echo.robot:

*** Settings ***
Library ../libraries/EchoLibrary.py
*** Test Cases ***
Dynamic Keywords Respond
${v}= Echo Value hello
Should Be Equal ${v} hello
${s}= Shout Value hello
Should Be Equal ${s} HELLO

Run it:

Terminal window
uv run robot checks/echo.robot
Terminal window
robot checks/echo.robot
Terminal window
poetry run robot checks/echo.robot

Done when

the test Dynamic Keywords Respond passes.


You can now write custom keywords three ways — module, class, and dynamic — and document them with Libdoc. Part 7 — Hooking into Execution goes further: it uses the Robot Framework API to observe and reshape runs with listeners, modifiers, result visitors, and a parser, and the Extension Lab there packages a library, a report, and a modifier together.

Check your understanding

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

  1. The dynamic library API is the pair of methods…

    Show the answer

    Answer: get_keyword_names and run_keyword — get_keyword_names lists the keywords; run_keyword runs the named one — PythonLibCore implements this for you.

  2. You hand-write the dynamic API when keywords are…

    Show the answer

    Answer: not known until run time (generated from data) — Reach for it only for runtime-generated keywords; otherwise a static or PythonLibCore library is simpler.

Key terms

get_keyword_namesconcept
Dynamic API: the list of keyword names a library provides.
run_keywordconcept
Dynamic API: run a named keyword with its arguments.

All terms in the glossary →