Robot calling python function with arguments

I created a python function just to print a value.

def getData(record: list):
print(record)

And I call it in the robot like this:

${body_value}= Run Keyword Data.Get Data(${message})

However, it displays an error:
Keyword failed: No keyword with name

I also called it like this:
${body_value}= Run Keyword Data.Get Data ${message}

In this case, it doesn’t even enter the function

My question is when is the correct syntax to call a python function with arguments in the robot?

Hi,

You need several things:

  • Import “keyword” library in python file
  • Add a decorator to the function with @keyword in python file (name of the called keyword from RF)
  • Import the .py file as a library in your robot file or a higher resource file
  • Simply call the keyword as named in the decorator in the test with arguments if applicable

Example here with calculation.py:

From robot.api.deco import keyword

@keyword("MultiplyBy2")
def multiply_value(value):
    result = int(value)*2
    return result

And robot file:

*** Settings ***
Library    ./scripts/calculation.py


*** Tests Cases ***
Test1
    ${myresult}    MultiplyBy2    24
    Should Be Equal As Integers  ${myresult}   48

Regards
Charlie

I didn’t understand why when I imported the Keyword library into the python file. In the test file, the import gives an error: TypeError: ‘module’ object is not callable

Hi,

You mean you still have the error?
I understand you set properly the library in python file but is the link to the python correct in the robot file/settings section?

Regards
Charlie

It’s the same as other imports

If you have the example or code it could be easier :slightly_smiling_face:

Archive python

import json
Import Keyword

@Keyword(“getDataTest”)
def getData(registro):
print(“Mensagem getData”+registro)

Archive Robot
Library …/scripts/getData.py AS Data

GETDATA TESTE
${valor_body}= Evaluate Data.Get Data(Texto teste) modules=getData
Log To Console “Valor getData:”${valor_body}

It doesn’t really follow my example.
You need to:

  • Correct the python keyword import as in my example (from…)

  • Ensure .py path is correct in Library/robot file import (try first without AS/alias)

  • You don’t need evaluate as you imported library and call the keyword. You just have to use:

    ${valor_body}     getDataTest    Texto teste  
    
  • Module as well is not needed here

  • After it works, you can add alias this way:

    ${valor_body} Data.getDataTest Texto teste

Charlie

Even with direct import, without importing and noting the working key. The call does not work. The body_value receives None

OK, so no error but empty value.
Here:

@Keyword(“getDataTest”)
def getData(registro):
    print(“Mensagem getData”+registro)

Take care of indentation, and you have no return value to RF, like this:

 @Keyword(“getDataTest”)
 def getData(registro):
     value1 = f“Mensagem getData + {registro}"
     print(value1) 
     return value1

You just print, so nothing will be send back to robot.

As it doesn’t print anything, I imagined that it doesn’t even enter the Python function. And that the call was incorrect. I’m increasing the getData function and I’m going to try the robot call again and I’ll come back here to let you know how it was resolved. Thanks!

The issue in your example is probably that you are using python “print” instead of returning the value, when you print in a python class the message wilI not be shown on RF console, returning the value in the def should work

Python.py

import json

def get_data(registro):
  return registro

Archive Robot

Library …/scripts/getData.py

GETDATA TESTE
 ${valor_body}   get data  teste 
Log To Console  Valor getData:Mensagem getData${valor_body}

Also if you want to print within a python class and see the logs in RF you could do so importing the RF logger RF logger

from robot.api import logger
def write_to_console():
    logger.console("hello world")
1 Like

Also double check your import to the library, make sure …/scripts/getData.py is the actual path to your python file from the context of where you are executing it

1 Like