Run robot keyword from python using robotframework api

Writing complex robot keywords in a robot language sometimes takes a lot of time, because a 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 many low-level keywords written in the language of robots (in .robot and .txt files). Is it possible to execute these keywords from python code using apache robotframework python?

+4
source share
1 answer

Yes it is possible. In python code, you can get a link to the BuiltIn library, and then use the Run Keyword keyword to run any keyword you want.

For example, you can 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 pass this keyword to call up any other keyword. Here is an example set in which there is a keyword written in a robot, and then it executes python code:

*** 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 call_keywordthe keyword launch method Example Keyword. Of course, you do not need to pass a keyword. The key point is to get a link to the BuiltIn library, which then allows you to call any method in this library.

Robot Framework. , . .

, , register_run_keyword, run_keyword. . , BuiltIn, python:

>>> import robot.libraries.BuiltIn
>>> help(robot.libraries.BuiltIn.register_run_keyword)
+5

All Articles