Most of the time an existing library already has the keywords you need. Now and then it does not, and that is when Robot Framework's Python roots pay off: you can write your own keywords in Python and use them like any other.
How it works
A custom library is a Python file whose functions become keywords. The function name maps to a keyword name, so total_price becomes the keyword Total Price:
# CartLibrary.py
def total_price(items):
return sum(float(i) for i in items)
Import the file and call it like any built-in keyword:
*** Settings ***
Library CartLibrary.py
*** Test Cases ***
The Basket Totals Correctly
${total}= Total Price ${PRICES}
Should Be Equal As Numbers ${total} 59.97
Arguments pass straight through, and whatever the function returns becomes the keyword's return value.
When to reach for one
A custom library is the right tool when you need something no existing library offers: a calculation, a call to an internal system, or a piece of test data generated in code. It is not the right tool for combining existing keywords into a readable step, because that is what a user keyword is for, and it keeps you in the readable syntax. The skill is knowing which of the two a given problem calls for.
Custom libraries also have finer points, such as how state is shared across tests, that matter once you go beyond a single function. Our Robot Framework Certified Professional course covers custom libraries alongside the rest of the framework, and prepares you for the RFCP exam.
Frequently asked questions
When should I write a custom library? When you need something no existing library provides, such as a calculation or a call to an internal system. For combining existing keywords, use a user keyword instead.
How does a Python function become a keyword?
Robot Framework maps the function name to a keyword name, so total_price becomes Total Price. Arguments and return values pass through.
Do I need to be a strong programmer? No. A simple library is a few plain functions. The deeper features are there when you need them, but you can start small.
Next: understanding Robot Framework logs and reports, or revisit user keywords and resource files.