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.
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?
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.
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).
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
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, 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.
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.
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.
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.
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
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.