here 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 or DynamicCore. Library keywords must be decorated with Robot Framework @keyword decorator. Provide a list of class instances implementing keywords to library_components argument in the HybridCore or DynamicCore __init__. It is also possible implement keywords in the library main class, by marking method with @keyword as keywords. It is not requires 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.
时间: 2024-02-14 12:06:31 浏览: 29
The example code below demonstrates how to use PythonLibCore with a library:
```python
from robot.libraries.PythonLibCore import HybridCore, keyword
class MyLibrary(HybridCore):
def __init__(self):
super(MyLibrary, self).__init__(library_components=[self])
@keyword
def my_keyword(self, arg1, arg2):
return arg1 + arg2
```
In the above example, `MyLibrary` is a library class that inherits from `HybridCore`. The library class has a single keyword `my_keyword` decorated with the `@keyword` decorator.
The `__init__` method of `MyLibrary` calls the parent constructor and passes `self` as the only argument to the `library_components` parameter. This makes the `my_keyword` method available as a keyword in the library.
When the library is imported and instantiated, the `my_keyword` method can be called as a keyword from within a Robot Framework test case. For example:
```robotframework
*** Settings ***
Library MyLibrary
*** Test Cases ***
Example Test Case
${result}= My Keyword 1 2
Should Be Equal As Integers ${result} 3
```
In the above test case, the `My Keyword` keyword is used to call the `my_keyword` method of the `MyLibrary` instance. The result is then checked using the `Should Be Equal As Integers` keyword.