Skip to content

One library, many parts

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

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:

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

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”
Edit

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()])
Run

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.90

Run it:

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

Done 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.

  1. What does PythonLibCore let you do?

    Show the answer

    Answer: split keywords across small classes composed into one library — DynamicCore composes component classes into one flat set of keywords — the pattern Browser uses.

  2. A DynamicCore library receives its components…

    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.

All terms in the glossary →