@@ -156,15 +156,63 @@ Then Library can be imported in Robot Framework side like this:
156156Library ${CURDIR}/PluginLib.py plugins=${CURDIR}/MyPlugin.py
157157```
158158
159+ # Listener
160+
161+ PLC supports
162+ [ library listeners] ( https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#libraries-as-listeners ) ,
163+ also listener can be defined in the class that defines keywords. PLC will automatically detect
164+ is class is also listener and set the ` ROBOT_LIBRARY_LISTENER ` as a list. List will contains all
165+ the class instances that are marked as listeners.
166+
167+ Example:
168+ ``` robotframework
169+ from robot.running.model import TestCase
170+ from robot.result.model import TestCase as TestCaseResult
171+
172+ from robotlibcore import DynamicCore, keyword
173+
174+
175+ class ListenerExample(DynamicCore):
176+
177+ ROBOT_LIBRARY_SCOPE = 'GLOBAL'
178+
179+ def __init__(self):
180+ self.ROBOT_LIBRARY_LISTENER = self
181+ components = [KeywordsWithListener()]
182+ super().__init__(components)
183+
184+
185+
186+ class KeywordsWithListener:
187+ ROBOT_LISTENER_API_VERSION = 3
188+
189+ def __init__(self):
190+ self.test = None
191+
192+
193+ def start_test(self, data: TestCase, result: TestCaseResult):
194+ self.test = data.name
195+ self.passed = result.passed
196+
197+ @keyword
198+ def keyword_with_listener(self, name: str, status: bool):
199+ assert name == self.test, f"Test case name {name} does not match expected {self.test}"
200+ assert status == self.passed, f"Test case status {status} does not match expected {self.passed} {type(self.passed)}"
201+
202+ ```
203+
204+ In the example ` KeywordsWithListener ` will act as a listener and ` start_test ` method is
205+ called each time test starts.
206+
159207# Translation
160208
161- PLC supports translation of keywords names and documentation. Translations must be provided in
162- the ` translation ` argument in the ` HybridCore ` or ` DynamicCore ` ` __init__ ` , either as a
163- dictionary or through a [ Path] ( https://docs.python.org/3/library/pathlib.html ) to a
164- [ JSON] ( https://www.json.org/json-en.html ) file. Providing translation data is optional, also it
209+ PLC supports translation of keywords names and documentation. Translations must be provided in
210+ the ` translation ` argument in the ` HybridCore ` or ` DynamicCore ` ` __init__ ` , either as a
211+ dictionary or through a [ Path] ( https://docs.python.org/3/library/pathlib.html ) to a
212+ [ JSON] ( https://www.json.org/json-en.html ) file. Providing translation data is optional, also it
165213is not mandatory to provide translation to all keyword.
166214
167- The keys of the dictionary are the methods names, not the keyword names, which implements keyword.
215+ The keys of the dictionary are the methods names, not the keyword names, which implements keyword.
168216Values are objects which contains two keys: ` name ` and ` doc ` . ` name ` key contains the keyword
169217translated name and ` doc ` contains keyword translated documentation. Providing
170218` doc ` and ` name ` is optional, i.e. translations data can also provide translations only
0 commit comments