One library, many parts
Chapter lab: Libraries that scale
This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.
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
As a library grows, one giant class becomes hard to navigate. PythonLibCore (the toolkit the Browser library itself is built on) lets you split keywords across several small classes and compose them into one library.
You inherit from DynamicCore, pass it your component instances, and decorate
each keyword method with @keyword. The components stay focused; the library
presents them as one flat set of keywords.
Task 1 — Compose two keyword classes into one library
Section titled “Task 1 — Compose two keyword classes into one library”Goal: create libraries/ShopLibrary.py — a ShopLibrary(DynamicCore) that
composes an OrderKeywords component (an order_reference keyword) and a
CartKeywords component (add_item_price and cart_total).
Here is the same composition over a different pair of components. Adapt it to orders and a cart.
Worked example — adapt it, don’t copy it
from robotlibcore import DynamicCore, keyword
class GreetKeywords: @keyword def greeting_for(self, name): return f"Hello, {name}!"
class CountKeywords: def __init__(self): self.seen = 0
@keyword def count_one(self): self.seen += 1
@keyword def times_counted(self) -> int: return self.seen
class HelloLibrary(DynamicCore): def __init__(self): DynamicCore.__init__(self, [GreetKeywords(), CountKeywords()])Done when
libraries/ShopLibrary.py defines a ShopLibrary class built on PythonLibCore.
Hint 1
Import from the toolkit: from robotlibcore import DynamicCore, keyword. Write two
small component classes, each with @keyword methods.
Hint 2
The main class passes instances of both components up to DynamicCore:
DynamicCore.__init__(self, [OrderKeywords(), CartKeywords()]).
Solution
from robotlibcore import DynamicCore, keyword
class OrderKeywords: @keyword def order_reference(self, prefix, number: int) -> str: return f"{prefix}-{number:04d}"
class CartKeywords: def __init__(self): self.prices = []
@keyword def add_item_price(self, price: float): self.prices.append(price)
@keyword def cart_total(self) -> float: return sum(self.prices)
class ShopLibrary(DynamicCore): def __init__(self): DynamicCore.__init__(self, [OrderKeywords(), CartKeywords()])Task 2 — Import it and run it green
Section titled “Task 2 — Import it and run it green”Create checks/shop.robot — keywords from both components are available as one set:
*** Settings ***Library ../libraries/ShopLibrary.py
*** Test Cases ***One Library Two Components ${ref}= Order Reference TRN ${3} Should Be Equal ${ref} TRN-0003 Add Item Price 19.90 Add Item Price 12.00 ${total}= Cart Total Should Be Equal As Numbers ${total} 31.90Run it:
uv run robot checks/shop.robotrobot checks/shop.robotpoetry run robot checks/shop.robotDone when
the test One Library Two Components passes.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
What does PythonLibCore let you do?
DynamicCorecomposes component classes into one flat set of keywords — the pattern Browser uses.Show the answer
Answer: split keywords across small classes composed into one library —
DynamicCorecomposes component classes into one flat set of keywords — the pattern Browser uses.A
DynamicCorelibrary receives its components…DynamicCore.__init__(self, [OrderKeywords(), CartKeywords()])— you pass instances.Show the answer
Answer: as instances passed to
DynamicCore.__init__—DynamicCore.__init__(self, [OrderKeywords(), CartKeywords()])— you pass instances.
Key terms
- PythonLibCoreconcept
- Compose multi-class libraries; the toolkit Browser is built on.
- DynamicCoreconcept
- Base class that presents component keywords as one library.