Running Robot Framework keywords from Python Function

Hi - I was trying to run a Robot Framework keyword from a Python function and capture the JSON/data returned from the keyword and do some additional logic and validation that was already implemented in Python. Calling Python function from a Robot test suite is no issue, but can we do other way around? I found below thread and tried following the same. but When I call this Python function externally as a Flask application I see this error. Can Python function access any robot resource files /keywords from the project ?

sample code tried:

libraries.BuiltIn.RobotNotRunningError: Cannot access execution context

from robot.libraries.BuiltIn import BuiltIn
from flask import Flask, request

app = Flask(name)

@app.get(‘/myEndPoint’)
def call_keyword():
myKeyword = request.args.get(‘keyword’) or ‘load_variables’
resp= BuiltIn().run_keyword(myKeyword)
… Do some thing here with the resp
return something

Hi Sarala,

The last chapter of the Robot Framework User Guide is Internal API, start buy having a read of the API documentation to get an idea of what you can do with it.

In you sample, I suspect the problem is you haven’t imported something else first that robot.libraries.BuiltIn needs? Entry points doesn’t list robot.libraries as an entry point, but i’m only guessing here.

Dave.

I added below line before the run_keyword. Still same error

      BuiltIn().import_resource('/myKeywords.robot')

Error: venv/lib/python3.9/site-packages/robot/libraries/BuiltIn.py", line 62, in _get_context
raise RobotNotRunningError(‘Cannot access execution context’)
robot.libraries.BuiltIn.RobotNotRunningError: Cannot access execution context

Hi Sarala,

From the error I suspect it’s failing at the line from robot.libraries.BuiltIn import BuiltIn, Unfortunately I’m not so familiar with the api, I’ve had a bit of a look at it, but moved on as there were better ways to achieve what i was trying to do

Dave.