4

I am invoking Robot Framework on a folder with a command like following:

robot --name MyTestSuite --variablefile lib/global_variables.py --variable TARGET_TYPE:FOO --variable IMAGE_TYPE:BAR --prerunmodifier MyCustomModifier.py ./tests

MyCustomModifier.py contains a simple SuiteVisitor class, which includes/excludes tags and does a few other things based on some of the variable values set.

How do I access TARGET_TYPE and IMAGE_TYPE in that class? The method shown here does not work, because I want access to the variables before tests start executing, and therefore I get a RobotNotRunningError with message Cannot access execution context.

After finding this issue report, I tried to downgrade to version 2.9.1 but nothing changed.

Community
  • 1
  • 1
Subhamoy S.
  • 6,566
  • 10
  • 37
  • 53

1 Answers1

2

None of public API's seem to provide this information but debugging the main code does provide an alternative way of obtaining it. It has to be said that this example code will work with version 3.0.2, but may not work in the future as these are internal functions subject to change. That said, I do think that the approach will remain.

As Robot Framework is an application, it obtains the command line arguments through it's main function: run_cli (when running from command line). This function is filled with the arguments from the system itself and can be obtained throughout every python script via:

import sys

cli_args = sys.argv[1:]

Robot Framework has a function that interprets the commandline argument list and make it into a more readable object:

from robot.run import RobotFramework
import sys

options, arguments = RobotFramework().parse_arguments(sys.argv[1:])

The argument variable is a list where all the variables from the command line are added. An example:

arguments[0] = IMAGE_TYPE:BAR

This should allow you to access the information you need.

A. Kootstra
  • 6,827
  • 3
  • 20
  • 43