The dynamic API underneath
Chapter lab: Libraries that scale
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:
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.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch19-libraries-that-scale.zip -OutFile ch19-libraries-that-scale.zip; Expand-Archive ch19-libraries-that-scale.zip -DestinationPath .; Remove-Item ch19-libraries-that-scale.zipOpen 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:
uv syncpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython -m venv .venv.venv\Scripts\Activate.ps1pip install -r requirements.txtKeep the environment active in every terminal where you run lesson commands.
poetry install --no-rootPython 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.
Task 1 — A hand-written dynamic library
Section titled “Task 1 — A hand-written dynamic library”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)Task 2 — Import it and run it green
Section titled “Task 2 — Import it and run it green”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} HELLORun it:
uv run robot checks/echo.robotrobot checks/echo.robotpoetry run robot checks/echo.robotDone 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.
The dynamic library API is the pair of methods…
get_keyword_nameslists the keywords;run_keywordruns the named one — PythonLibCore implements this for you.Show the answer
Answer:
get_keyword_namesandrun_keyword—get_keyword_nameslists the keywords;run_keywordruns the named one — PythonLibCore implements this for you.You hand-write the dynamic API when keywords are…
Reach for it only for runtime-generated keywords; otherwise a static or PythonLibCore library is simpler.
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.