Robot Framework PythonLibCore
Robot Framework PythonLibCore (version 4.5.0) is a generic component designed to simplify the creation of larger, more flexible test libraries for Robot Framework using Python. It abstracts away the complexities of the Robot Framework's hybrid and dynamic library APIs, providing a streamlined interface. The library is actively maintained with frequent releases addressing bug fixes, performance enhancements, and compatibility with new Python and Robot Framework versions. It is widely used by other popular Robot Framework libraries like SeleniumLibrary and Browser library.
Warnings
- breaking Python 3.7 support was dropped in PythonLibCore v4.2.0. Users on Python 3.7 must either upgrade their Python version or use an older version of `robotframework-pythonlibcore`.
- breaking Major Robot Framework versions require specific `pythonlibcore` compatibility. For instance, v3.0.0 dropped support for Robot Framework 3.1 and addressed issues with Robot Framework 4 and `typing.Union`. Version 4.3.0 added support for Robot Framework 7.0, and v4.5.0 supports Robot Framework 6.1 and newer.
- gotcha Prior to v4.4.1, a bug could cause custom keyword names defined via the `@keyword` decorator to 'leak', potentially leading to unexpected keyword resolution or conflicts.
- gotcha Incorrect library import paths in Robot Framework are a common source of 'No keyword with name X found' errors. When importing a Python class-based library, ensure the import path correctly points to the module or class.
- gotcha When developing a dynamic library with PythonLibCore, prefer inheriting `DynamicCore` (or `HybridCore`) and using the `@keyword` decorator on methods within your component classes. Avoid manually implementing `get_keyword_names()` and `run_keyword()` if you are using PythonLibCore's keyword discovery mechanism, as this can lead to keywords not being registered or found.
- gotcha Version 4.5.0 includes a bug fix to avoid 'maximum recursion depth exceeded' errors on `getattr`. If you encounter this error in older versions, it might be related to this known issue.
Install
-
pip install robotframework-pythonlibcore
Imports
- DynamicCore
from robotlibcore import DynamicCore
- HybridCore
from robotlibcore import HybridCore
- keyword
from robotlibcore import keyword
Quickstart
from robotlibcore import DynamicCore, keyword
class LibraryComponent1:
@keyword
def my_first_keyword(self, arg):
print(f"Executing My First Keyword with: {arg}")
return f"Processed: {arg}"
class LibraryComponent2:
@keyword('Custom Name Keyword')
def another_keyword(self, value):
print(f"Executing Another Keyword with value: {value}")
return f"Custom handled: {value}"
class MyLibrary(DynamicCore):
"""Example Robot Framework library using PythonLibCore."""
ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
def __init__(self):
# Pass instances of classes containing keywords to DynamicCore
libraries = [LibraryComponent1(), LibraryComponent2()]
DynamicCore.__init__(self, libraries)
@keyword
def keyword_in_main_library(self, data):
"""A keyword directly in the main library class."""
print(f"Keyword in main library received: {data}")
return data.upper()
# To use this library in Robot Framework:
# *** Settings ***
# Library MyLibrary.py
#
# *** Test Cases ***
# Example Test
# ${result1}= My First Keyword hello
# Log To Console ${result1}
# ${result2}= Custom Name Keyword world
# Log To Console ${result2}
# ${result3}= Keyword In Main Library pythonlibcore
# Log To Console ${result3}