evelyn
(Evelyn Braz)
2 October 2024 14:44
1
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
evelyn
(Evelyn Braz)
2 October 2024 17:35
5
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
evelyn
(Evelyn Braz)
2 October 2024 17:51
7
It’s the same as other imports
If you have the example or code it could be easier
evelyn
(Evelyn Braz)
2 October 2024 17:57
9
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
evelyn
(Evelyn Braz)
2 October 2024 18:27
11
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.
evelyn
(Evelyn Braz)
2 October 2024 20:03
13
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!