Writing complex robot keywords in robot language is sometimes very time consuming because robot language is not a real programming language. I would like to write my keywords in python and only expose simple html tables in robotframework language. The problem is that we already have a lot of low level robot keywords written in robot language (in .robot and .txt files). Is it possible to execute those keywords from the python code using the robotframework python api ?
Yes, it is possible. In your python code you can get a reference to the BuiltIn library, and then use the Run Keyword keyword to run any keyword you want.
For example, you could write a python keyword that takes another keyword as an argument and runs it. The following might be how you do it in python:
# MyLibrary.py
from robot.libraries.BuiltIn import BuiltIn
def call_keyword(keyword):
return BuiltIn().run_keyword(keyword)
You can then tell this keyword to call any other keyword. Here's an example suite that has a keyword written in robot,, and then has the python code execute it:
*** Settings ***
| Library | MyLibrary.py
*** Keywords ***
| Example keyword
| | log | hello, world
*** Test Cases ***
| Example of calling a python keyword that calls a robot keyword
| | Call keyword | Example keyword
Notice how the test case tells the call_keyword
method to run the keyword Example Keyword
. Of course, you don't have to pass in a keyword. The key point is to get a reference to the BuiltIn library, which then allows you to call any method in that library.
This is documented in the robot framework user guide, in the section titled Using Robot Framework's Internal Modules. More specifically, see the section Using BuiltIn Library.
Note that the documentation states you need to call register_run_keyword if your keyword calls the run_keyword
method. I won't reproduce the documentation here. You can get the documentation by looking in the BuiltIn module itself, or run the following code in an interactive python session:
>>> import robot.libraries.BuiltIn
>>> help(robot.libraries.BuiltIn.register_run_keyword)