How to use extra arguments passed with a variable file - Robot framework

The Robot Framework user guide has a section that describes how to transfer variable files, as well as some possible variables, if necessary.
Example:
pybot --variablefile taking_arguments.py:arg1:arg2

My question is: can I use these possible variables arg1 and arg2 in the take_arguments.py file after that, and if I can, then how?

Now I have this:

pybot --variablefile taking_arguments.py:arg1:arg2

content_arguments.py:

IP_PREFIX = arg1

But this leads to

NameError: name 'arg1' is not defined

+6
source share
2 answers

The only way to use variables in the argument file using the syntax --variablefile filename.py:arg1:arg2 is to have your variable file implement the get_variables function. This function will be passed by the arguments specified on the command line and should return a dictionary of variable names and values.

For example, consider the following variable file called "variables.py":

 def get_variables(arg1, arg2): variables = {"argument 1": arg1, "argument 2": arg2, } return variables 

This file creates two robot variables named ${argument 1} and ${argument 2} . The values โ€‹โ€‹for these variables will be the values โ€‹โ€‹of the arguments that were passed. You can use this variable file as follows:

 pybot --variablefile variables.py:one:two ... 

In this case, the strings โ€œoneโ€ and โ€œtwoโ€ will be passed to get_variables as two arguments. Then they will be associated with two variables, as a result of which ${argument 1} set to one and ${argument 2} to two .

+5
source

I did not try to pass the initial values โ€‹โ€‹to the variables in the variable file ... Therefore, I'm not sure if this is possible ...

I can offer an alternative ...

You can manually define some variables with your values โ€‹โ€‹in the pybot command ...

 pybot -variablefile taking_arguments.py -v IP_PREFIX:arg1 -v Varibale:Value 

If I am not mistaken, these manually initiated variables have a higher priority than in the variable file. Therefore, even if they are triggered in a variable file, the values โ€‹โ€‹passed with the -v option will be used in the test file.

Hope this helps you!

+2
source

Source: https://habr.com/ru/post/944971/


All Articles