6

In Robot Framework user guide there is a section that describes how to pass variable files and also some possible variables if needed.
Example:
pybot --variablefile taking_arguments.py:arg1:arg2

My question is can i use these possible variables arg1 and arg2 in the taking_arguments.py file afterwards and if i can then how?

Right now i have this:

pybot --variablefile taking_arguments.py:arg1:arg2

taking_arguments.py contents:

IP_PREFIX = arg1

But that results in

NameError: name 'arg1' is not defined

2 Answers 2

6

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

For example, consider the following variable file, named "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 in. You might use this variable file like this:

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

In this case, the strings "one" and "two" will be passed to get_variables as the two arguments. These will then be associated with the two variables, resulting in ${argument 1} being set to one and ${argument 2} being set to two.

Sign up to request clarification or add additional context in comments.

3 Comments

Received TypeError: get_variables() takes exactly 2 arguments (0 given) when I tried the example above. This is my setup: one robot file with open browser and these two arguments ${argument 1} and ${argument 2} I am also calling the .py file in the Settings my command line execution looks like this pybot -v variables.py:username:password file.robot I am trying to pass a username and password to a form via the command line using this method, but no luck.
@Freddy You've probably used wrong argument. You should use lowercase -v to define single variable. For variable file, use uppercase -V or --variablefile
@sjudǝʊ Thank you for this clarification. In fact, I was passing the wrong flag. Using -V worked as expected. I still received the following error Error in file '.../_robot/file.robot': Processing variable file '.../_robot/variables.py' failed: TypeError: get_variables() takes exactly 2 arguments (0 given) in the logs, however, the process worked. Not sure if the error from the logs is just something that RF has trouble processing.
2

I haven't tried to pass initial values to variables in a variable file... So I am not sure if this is something possible...

I can offer an alternative...

You can define manually some variables with their values at 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 these in the variable file. So even if they are initiated in the variable file, the values passed with the -v option will be used in the testcase.

Hope this can help you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.