Octane
Octane

Reputation: 198

Is there a way to assign variable in Robot Framework to python without using it as an argument?

My robot keyword looks like this:

${HW_list}  Get_hw_list  ${file}
Run process   python    python_test.py 

Inside my python_test.py

from robot.libraries.BuiltIn import BuiltIn
List_of_modules = BuiltIn().get_variable_value("${HW_list}")

Im having an error saying,

robot.libraries.BuiltIn.RobotNotRunningError: Cannot access execution context

I've tried searching for similar issues but I can't find where I am wrong. I also have RF==3.1.2 since in 1 post, I think there was an issue that was fixed on this version.

Upvotes: 0

Views: 1820

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385970

Since you are running python_test.py as a separate process, you can't directly use robot variables or keywords in the separate process.

If you don't want to pass the value as arguments, you're going to have to use some other method. For example, you could set an environment variable and have your script pick the data up from the environment. This can only be used to pass strings.

Another option would be for your robot script to write the data to a file or database, and have your script read that file or database to get the value.

Upvotes: 2

Błażej Michalik
Błażej Michalik

Reputation: 5055

Run process will run your module in a separate interpreter. That's why it cannot find the execution context.

Instead, do the following:

  1. Make a custom keyword from your module code:
from robot.libraries.BuiltIn import BuiltIn

def my_custom_keyword():
    List_of_modules = BuiltIn().get_variable_value("${HW_list}")
  1. Import the module as a Library in your robot code:
*** Settings ***
Library    python_test.py
  1. Use the keyword in your test, instead of Run process:
${HW_list}  Get_hw_list  ${file}
My Custom Keyword

Upvotes: 3

Related Questions