Tools to ease creating larger test libraries for Robot Framework using Python.
Project description
Python Library Core
Tools to ease creating larger test libraries for Robot Framework using Python. The Robot Framework hybrid and dynamic library API gives more flexibility for library than the static library API, but they also sets requirements for libraries which needs to be implemented in the library side. PythonLibCore eases the problem by providing simpler interface and handling all the requirements towards the Robot Framework library APIs.
Code is stable and is already used by SeleniumLibrary and Browser library. Project supports two latest version of Robot Framework.
Usage
There are two ways to use PythonLibCore, either by
HybridCore
or by using DynamicCore
. HybridCore
provides support for
the hybrid library API and DynamicCore
provides support for dynamic library API.
Consult the Robot Framework User
Guide,
for choosing the correct API for library.
Regardless which library API is chosen, both have similar requirements.
- Library must inherit either the
HybridCore
orDynamicCore
. - Library keywords must be decorated with Robot Framework @keyword decorator.
- Provide a list of class instances implementing keywords to
library_components
argument in theHybridCore
orDynamicCore
__init__
.
It is also possible implement keywords in the library main class, by marking method with
@keyword
as keywords. It is not required pass main library instance in the
library_components
argument.
All keyword, also keywords implemented in the classes outside of the main library are available in the library instance as methods. This automatically publish library keywords in as methods in the Python public API.
The example in below demonstrates how the PythonLibCore can be used with a library.
Example
"""Main library."""
from robotlibcore import DynamicCore
from mystuff import Library1, Library2
class MyLibrary(DynamicCore):
"""General library documentation."""
def __init__(self):
libraries = [Library1(), Library2()]
DynamicCore.__init__(self, libraries)
@keyword
def keyword_in_main(self):
pass
"""Library components."""
from robotlibcore import keyword
class Library1(object):
@keyword
def example(self):
"""Keyword documentation."""
pass
@keyword
def another_example(self, arg1, arg2='default'):
pass
def not_keyword(self):
pass
class Library2(object):
@keyword('Custom name')
def this_name_is_not_used(self):
pass
@keyword(tags=['tag', 'another'])
def tags(self):
pass
Plugin API
It is possible to create plugin API to a library by using PythonLibCore. This allows extending library with external Python classes. Plugins can be imported during library import time, example by defining argumet in library [__init__]{.title-ref} which allows defining the plugins. It is possible to define multiple plugins, by seperating plugins with with comma. Also it is possible to provide arguments to plugin by seperating arguments with semicolon.
from robot.api.deco import keyword # noqa F401
from robotlibcore import DynamicCore, PluginParser
from mystuff import Library1, Library2
class PluginLib(DynamicCore):
def __init__(self, plugins):
plugin_parser = PluginParser()
libraries = [Library1(), Library2()]
parsed_plugins = plugin_parser.parse_plugins(plugins)
libraries.extend(parsed_plugins)
DynamicCore.__init__(self, libraries)
When plugin class can look like this:
class MyPlugi:
@keyword
def plugin_keyword(self):
return 123
Then Library can be imported in Robot Framework side like this:
Library ${CURDIR}/PluginLib.py plugins=${CURDIR}/MyPlugin.py
Translation
PLC supports translation of keywords names and documentation, but arguments names, tags and types
can not be currently translated. Translation is provided as a file containing
Json and as a
Path object. Translation is provided in
translation
argument in the HybridCore
or DynamicCore
__init__
. Providing translation
file is optional, also it is not mandatory to provide translation to all keyword.
The keys of json are the methods names, not the keyword names, which implements keyword. Value
of key is json object which contains two keys: name
and doc
. name
key contains the keyword
translated name and doc
contains keyword translated documentation. Providing
doc
and name
is optional, example translation json file can only provide translations only
to keyword names or only to documentatin. But it is always recomended to provide translation to
both name
and doc
.
Library class documentation and instance documetation has special keys, __init__
key will
replace instance documentation and __intro__
will replace libary class documentation.
Example
If there is library like this:
from pathlib import Path
from robotlibcore import DynamicCore, keyword
class SmallLibrary(DynamicCore):
"""Library documentation."""
def __init__(self, translation: Path):
"""__init__ documentation."""
DynamicCore.__init__(self, [], translation.absolute())
@keyword(tags=["tag1", "tag2"])
def normal_keyword(self, arg: int, other: str) -> str:
"""I have doc
Multiple lines.
Other line.
"""
data = f"{arg} {other}"
print(data)
return data
def not_keyword(self, data: str) -> str:
print(data)
return data
@keyword(name="This Is New Name", tags=["tag1", "tag2"])
def name_changed(self, some: int, other: int) -> int:
"""This one too"""
print(f"{some} {type(some)}, {other} {type(other)}")
return some + other
And when there is translation file like:
{
"normal_keyword": {
"name": "other_name",
"doc": "This is new doc"
},
"name_changed": {
"name": "name_changed_again",
"doc": "This is also replaced.\n\nnew line."
},
"__init__": {
"name": "__init__",
"doc": "Replaces init docs with this one."
},
"__intro__": {
"name": "__intro__",
"doc": "New __intro__ documentation is here."
},
}
Then normal_keyword
is translated to other_name
. Also this keyword documentions is
translted to This is new doc
. The keyword is name_changed
is translted to
name_changed_again
keyword and keyword documentation is translted to
This is also replaced.\n\nnew line.
. The library class documentation is translated
to Replaces init docs with this one.
and class documentation is translted to
New __intro__ documentation is here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file robotframework-pythonlibcore-4.4.0.tar.gz
.
File metadata
- Download URL: robotframework-pythonlibcore-4.4.0.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de0eeabdae23f22d452a0a9f70531fb3e92e18eb7ac8a520fcfbd91cb85a84f7 |
|
MD5 | 73f4d72426099df3aaf9d313f33bf93e |
|
BLAKE2b-256 | f899e5a2df454933d542897d353dd822e30b2126f8fc46f01c9b34d21434c66c |
File details
Details for the file robotframework_pythonlibcore-4.4.0-py2.py3-none-any.whl
.
File metadata
- Download URL: robotframework_pythonlibcore-4.4.0-py2.py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d21ff5f2ffe70c24b314dd9a237b0653f6358c32189d782aa37a33fd53764dc6 |
|
MD5 | 9caeedc3fd8e3db89bd49975e6b74494 |
|
BLAKE2b-256 | dbebc192698de6ea8e32f4201cf05e75a57c0e9f630e9b7324791dc0caea7549 |