Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute robot keyword from python using robotframework api

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 ?

like image 653
Emil Salageanu Avatar asked Nov 20 '14 15:11

Emil Salageanu


People also ask

Can we call a robot keyword in Python?

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. 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.

How do you call a keyword in Robot Framework?

Enter the argument to be used with the keyword. Go back to your test case. Now, you need to pass the value which is the URL to be used for the test case. In the test case, when you type the user-defined keyword and press Ctrl + Spacebar, it gives the details of the keyword along with the arguments.

How do you create a custom keyword in robot framework using Python?

The first thing to do, to achieve this is to create a folder in the root of the project we have i.e. lib. Next thing is to create a file for our custom library which we will be used to write our own custom keyword which can be imported into our Robot Framework file.


1 Answers

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)
like image 139
Bryan Oakley Avatar answered Nov 09 '22 14:11

Bryan Oakley