Import and pass variables/arguments in Robot (Tricky)

Hey guys, thank you in advance and I’m sorry if my english is not perfect.

I’ve read some parts of Robot doc but I can’t find exactly what I’m looking for. In my project I have a “test descriptor” written in YAML, and we want to pass some variables/arguments (with specific values) to our Robot test. Then this Robot test, in the test cases, should use those variables in some of our python functions.

So, we import our code.py as library, and we want to “read/get/input/whatever it is” from that YAML file, then use those variables in our python methods.

PD: I know you maybe have the idea of passing the variables directly to the Python library, but no, we can’t do that even if it is possible.

Thank you so much again, maybe it is something easy but I’m a bit new in Robot.

I am not sure I understand what you mean to be honest.
Here is how I pass the variables I need when I run the tests

robot --outputdir results --variable ENV:test --variable email:test@tests.com tests/00__GUI_Tests/02__Something.robot

Or when I run the tests using docker, I pass the variables inside the ROBOT_OPTIONS, like this:
docker run --rm
–user $(id -u):$(id -g)
–network=host
-v $PWD/results:/opt/robotframework/results:Z
-v $PWD/tests/:/opt/robotframework/tests:Z
-v $PWD/resources/:/opt/robotframework/resources:Z
-v $PWD/libs/:/opt/robotframework/libs:Z
-e ROBOT_OPTIONS="–variable ENV:local"
ppodgorsek/robot-framework:3.8.0

Both options can be used inside a yml file.
Is this what you mean? Does this help you?

Hey! Thank you so much for your answer. I learned some things from it.

But what I mean is that we already have a YAML file which contains a lot of things, etc etc, AND the values I want as variables in robot.

So I want to read/pass the variables to the robot test, and then use these variables inside test cases for my library functions.

Yes, by passing the variables in the execution command you can use them inside the tests (they will be Global variables then).

Just make sure you do make those variables actually available for other scripts to use.

Hi Retourned,

I’m also not exactly sure what you are asking, but I think this section of the documentation most closely matches what you seem to be trying to do:

resource-and-variable-files

  • Implementing variable file as Python or Java class
  • Variable file as YAML

If so, give some example of what code.py and your yml files look like and explain what you are trying to retrieve and then I can try and give you a working example.

Dave.

@damies13 @Eftychia

Hi guys, thank you so much for your answers. I draw a little diagram to show you my idea, so maybe it is clear now. I’m sure it’s something pretty simple and factible (I hope).

Hi Retourned,

everything you are wanting to do is documented in the documentation I referenced in my previous post, here is a working example, create these three files, run robot Retourned.robot and look at the log, hopefully everything will be clear:

Retourned.yaml:

string:   Hello, world!
integer:  42
list:
  - one
  - two
dict:
  one: yksi
  two: kaksi
  with spaces: kolme
  three: 3

Retourned.py:

def multiply_string(var1, var2):
	res = int(var1) * int(var2)
	str = "{} x {} = {}".format(var1, var2, res)
	return str

Retourned.robot:


*** Settings ***
Library    Retourned.py

Variables 	Retourned.yaml

*** Test Cases ***
Retourned
	Log    ${string}
	Log    ${integer}
	Log    ${list}
	Log    ${dict}

	${result}= 	Multiply String 	${integer} 	${dict['three']}
	Log    ${result}

don’t forget to install PyYAML if you haven’t already:

Note

Using YAML files with Robot Framework requires PyYAML module to be installed. If you have pip installed, you can install it simply by running pip install pyyaml .

YAML variable files must have either .yaml or .yml extension. Support for the .yml extension is new in Robot Framework 3.2.

Hopefully that is enough to help you achieve what you want to do,

Dave.

Dave, you literally solved my life right now. I have a lot of other things to do, and that Robot part has been very stressful for me since I had not use it a lot.

Thank you!!!

Hi Retourned,

When you get some time, read the documentation for the core robot framework, even if you just skim over it so you have some idea what is there, it’s really good documentation and will save you a lot of pain.

Also read over the keyword documentation for the libraries you use the most, usually these are well documented too.

FYI before creating this example for you I’d not even used YAML in RF, I had to install pyyaml for this example, I just followed the documentation.

Glad I could help,

Dave.

Hi damies13,

Yeah, of course I will take a deep read of Robot framework document, since it’s a tool that looks very interesting and useful for some things I will do in the future.

Ty again :slight_smile:

Hey again Damies! I made my project work with the info you gave me last month. I’ve been reading some things from Robot doc and learning a bit more. But I’m stuck in something again.

It’s the same case than before, BUT now I have nested hierarchy inside the YAML file which acts as variable file. I mean, inside my YAML file I have the following, for example:

If I want to access that “value”, how can I map the variable? I mean, in your example you used ${dict[three]} but the example only had 1 depth level, you know what I mean? I’m reading the Doc right now, the YAML mapping section but I can not find what I’m looking for.

Hi Retourned,

Remember from my example Log ${dict} Log is your friend when you are unsure it helps you see what is going on.

But from memory I think it goes like this:

Log    ${test_phases}                                                        # returns a Dictionary
Log    ${test_phases['setup']}                                           # returns a Dictionary
Log    ${test_phases['setup']['deployments']}                # returns a List
Log    ${test_phases['setup']['deployments'][0]}            # returns first Dictionary from List
Log    ${test_phases['setup']['deployments'][0]['paramaters']}   # returns a List

obviously you can use list item 1,2 etc and iterate over the lists and dictionaries with for loops

Hope that helps,

Dave.

Thank you so much again damies!

It is such a great solution! :slight_smile:

With latest RF versions (starting from RF 3.2 or latest from 4.0) dictionary and list access can be written also like ${dict}[key] and ${list}[1] so that nesting like ${phases}[setup][deplayments][0][parameters] works as well. The main benefit compared to ${dict['key']} is that you don’t need quotes.

1 Like

It is Great soluation thanks for sharing this.

NomNom