Return variable value from Robot Framework to Python

Hi All,

I have a .py file that calls a.robot file during run.
robot.run("test.robot")
At the end of the test robot I have a variable ${output_variable} with a calculated value and would like to give back this variable to the .py file.
Could you please help how is this possible?
output_variable = BuiltIn.get_variable_value('${output_variable}') is not working as I get the following error: RobotNotRunningError('Cannot access execution context')

Thanks

Well, the error message says it all. Robot is no longer running!

This is what I would do:

  • In your test Teardown, have some keyword to write the variable to a Python file. Then you can read it (import if python file, or read if some other format) (importing a file may have security questions)
  • Maybe can be sent the value as an environment variable, that you can read after robot execution.
1 Like

Thanks for the reply.
When I call Python from Robot Framework it is very easy to pass and return variable values from Python functions, or using suite variable that Python can access.
I thought there is an easy way to return variable values from Robot Framework when Python calls it, as pass variable to Robot Framework is very easy.

In addition to solutions already listed above, you could create a listener that stores this information. The listener could probably get the value of this variable in its close method and store it as an instance attribute that you can access after execution.

2 Likes

Thank you. Could you please help me out how to achieve it? Should I write it in Python and somehow call it?

Listener API is explained in the User Guide:
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#listener-interface

You start execution programmatically and with listeners the usage could be something like this:

listener = MyListener()
robot.run(..., listener=listener)
variable = listener.variable

I have created the MyListener class with a single close def.

def close(self):
	var = BuiltIn().get_variable_value("${output_variable}")
	return var

The output_variable value is set in the .robot file.

When I call the close variable = listener.close() in my python file I get the “Cannot access execution context” error.

That’s annoying. Apparently the execution context isn’t anymore available that late in the execution. What ought to work is capturing the value in the last end_suite method. To know you are in the last end_suite, you can either implement also start_suite to keep track how many suites have started or just that check is suite id s1.