|
| 1 | +--- |
| 2 | +title: Python Library Core |
| 3 | +--- |
| 4 | + |
| 5 | +Tools to ease creating larger test libraries for [Robot |
| 6 | +Framework](http://robotframework.org) using Python. The Robot Framework |
| 7 | +[hybrid](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#hybrid-library-api) |
| 8 | +and [dynamic library |
| 9 | +API](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#dynamic-library-api) |
| 10 | +gives more flexibility for library than the static library API, but they |
| 11 | +also sets requirements for libraries which needs to be implemented in |
| 12 | +the library side. PythonLibCore eases the problem by providing simpler |
| 13 | +interface and handling all the requirements towards the Robot Framework |
| 14 | +library APIs. |
| 15 | + |
| 16 | +Code is stable and version 1.0 is already used by |
| 17 | +[SeleniumLibrary](https://github.com/robotframework/SeleniumLibrary/) |
| 18 | +and |
| 19 | +[WhiteLibrary](https://pypi.org/project/robotframework-whitelibrary/). |
| 20 | +The version 2.0 support changes in the Robot Framework 3.2. |
| 21 | + |
| 22 | +[](https://github.com/robotframework/PythonLibCore) |
| 23 | + |
| 24 | +# Usage |
| 25 | + |
| 26 | +There are two ways to use PythonLibCore, either by |
| 27 | +[HybridCore]{.title-ref} or by using [DynamicCore]{.title-ref}. |
| 28 | +[HybridCore]{.title-ref} provides support for the hybrid library API and |
| 29 | +[DynamicCore]{.title-ref} provides support for dynamic library API. |
| 30 | +Consult the Robot Framework [User |
| 31 | +Guide](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#creating-test-libraries), |
| 32 | +for choosing the correct API for library. |
| 33 | + |
| 34 | +Regardless which library API is chosen, both have similar requirements. |
| 35 | + |
| 36 | +1) Library must inherit either the [HybridCore]{.title-ref} or |
| 37 | + [DynamicCore]{.title-ref}. |
| 38 | +2) Library keywords must be decorated with Robot Framework |
| 39 | + [\@keyword](https://github.com/robotframework/robotframework/blob/master/src/robot/api/deco.py) |
| 40 | + decorator. |
| 41 | +3) Provide a list of class instances implementing keywords to |
| 42 | + [library_components]{.title-ref} argument in the |
| 43 | + [HybridCore]{.title-ref} or [DynamicCore]{.title-ref} |
| 44 | + [\_\_init\_\_]{.title-ref}. |
| 45 | + |
| 46 | +It is also possible implement keywords in the library main class, by |
| 47 | +marking method with [\@keyword]{.title-ref} as keywords. It is not |
| 48 | +requires pass main library instance in the |
| 49 | +[library_components]{.title-ref} argument. |
| 50 | + |
| 51 | +All keyword, also keywords implemented in the classes outside of the |
| 52 | +main library are available in the library instance as methods. This |
| 53 | +automatically publish library keywords in as methods in the Python |
| 54 | +public API. |
| 55 | + |
| 56 | +The example in below demonstrates how the PythonLibCore can be used with |
| 57 | +a library. |
| 58 | + |
| 59 | +# Example |
| 60 | + |
| 61 | +``` python |
| 62 | +"""Main library.""" |
| 63 | + |
| 64 | +from robotlibcore import DynamicCore |
| 65 | + |
| 66 | +from mystuff import Library1, Library2 |
| 67 | + |
| 68 | + |
| 69 | +class MyLibrary(DynamicCore): |
| 70 | + """General library documentation.""" |
| 71 | + |
| 72 | + def __init__(self): |
| 73 | + libraries = [Library1(), Library2()] |
| 74 | + DynamicCore.__init__(self, libraries) |
| 75 | + |
| 76 | + @keyword |
| 77 | + def keyword_in_main(self): |
| 78 | + pass |
| 79 | +``` |
| 80 | + |
| 81 | +``` python |
| 82 | +"""Library components.""" |
| 83 | + |
| 84 | +from robotlibcore import keyword |
| 85 | + |
| 86 | + |
| 87 | +class Library1(object): |
| 88 | + |
| 89 | + @keyword |
| 90 | + def example(self): |
| 91 | + """Keyword documentation.""" |
| 92 | + pass |
| 93 | + |
| 94 | + @keyword |
| 95 | + def another_example(self, arg1, arg2='default'): |
| 96 | + pass |
| 97 | + |
| 98 | + def not_keyword(self): |
| 99 | + pass |
| 100 | + |
| 101 | + |
| 102 | +class Library2(object): |
| 103 | + |
| 104 | + @keyword('Custom name') |
| 105 | + def this_name_is_not_used(self): |
| 106 | + pass |
| 107 | + |
| 108 | + @keyword(tags=['tag', 'another']) |
| 109 | + def tags(self): |
| 110 | + pass |
| 111 | +``` |
| 112 | + |
| 113 | +# Plugin API |
| 114 | + |
| 115 | +It is possible to create plugin API to a library by using PythonLibCore. |
| 116 | +This allows extending library with external Python classes. Plugins can |
| 117 | +be imported during library import time, example by defining argumet in |
| 118 | +library [\_\_init\_\_]{.title-ref} which allows defining the plugins. It |
| 119 | +is possible to define multiple plugins, by seperating plugins with with |
| 120 | +comma. Also it is possible to provide arguments to plugin by seperating |
| 121 | +arguments with semicolon. |
| 122 | + |
| 123 | +``` python |
| 124 | +from robot.api.deco import keyword # noqa F401 |
| 125 | + |
| 126 | +from robotlibcore import DynamicCore, PluginParser |
| 127 | + |
| 128 | +from mystuff import Library1, Library2 |
| 129 | + |
| 130 | + |
| 131 | +class PluginLib(DynamicCore): |
| 132 | + |
| 133 | + def __init__(self, plugins): |
| 134 | + plugin_parser = PluginParser() |
| 135 | + libraries = [Library1(), Library2()] |
| 136 | + parsed_plugins = plugin_parser.parse_plugins(plugins) |
| 137 | + libraries.extend(parsed_plugins) |
| 138 | + DynamicCore.__init__(self, libraries) |
| 139 | +``` |
| 140 | + |
| 141 | +When plugin class can look like this: |
| 142 | + |
| 143 | +``` python |
| 144 | +class MyPlugi: |
| 145 | + |
| 146 | + @keyword |
| 147 | + def plugin_keyword(self): |
| 148 | + return 123 |
| 149 | +``` |
| 150 | + |
| 151 | +Then Library can be imported in Robot Framework side like this: |
| 152 | + |
| 153 | +``` bash |
| 154 | +Library ${CURDIR}/PluginLib.py plugins=${CURDIR}/MyPlugin.py |
| 155 | +``` |
0 commit comments