3

I have a Variables.yaml file as below:

userId: 12
URL: xyz.com/user=${userId}

The problem above is, the variable is not being replaced, at run-time my URL looks like:

xyz.com/user=${userId}
A. Kootstra
  • 6,827
  • 3
  • 20
  • 43
Suyash Nande
  • 59
  • 1
  • 1
  • 9
  • There is a bit of context missing. Can you add more details to your question. Where is this `Variables.yaml` file used or how you except to use it? How you currently try to pass the `userId` into it? – Torsten Engelbrecht Aug 30 '18 at 00:55
  • What is your goal, what are you trying to achieve? Is it for the yaml parser to replace in the value of `URL` the value of `userId` that's defined before that? Because this is not the correct yaml syntax. Or, do you want Robotframework to do it (as your using RF's variable access syntax)? – Todor Minakov Aug 30 '18 at 06:27
  • @Todor - Yes I'm using RF. Here is the scenario: I am calling Variables.yaml file into generics.robot, where there is my keyword which is accessing URL variable. And as i mentioned, my URL variable is calling again a variable "userId", but its value is not replacing. – Suyash Nande Aug 30 '18 at 19:53
  • What you're trying to achieve is simply not possible with the standard yaml - it doesn't allow for inline variable substitution. Your best bet is A. Kootstra's answer with `Replace Variables`, **or** moving to python's variables file, it gives the greatest flexibility. – Todor Minakov Aug 31 '18 at 07:31

1 Answers1

2

In YAML it is possible to refer to another value however only as the complete value and not combine it with another variable or string. In the Robot Framework Guide chapter on Resource and variable files several options are explain of which YAML variable files is one of them.

It is important to note that these variables are Global Level variables. Which means they can be imported in one file and then accessed from another. This can be a test case file or a resource file containing just keywords.

In the below example everything is in the same file, but it can easily be split into several files. It is common to add the variable file via a command line argument, keywords seperated from their test cases in Resource Files.

variables.yaml

userId: 12
URL: xyz.com/user=${userId}

robot_script.robot

*** Settings ***
Library      Collections

Variables    variables.yaml

*** Test Cases ***
TC
    ${userId}    Set Variable        MyUserName
    Log To Console    \n ${URL}
    ${URL}       Replace Variables   ${URL}
    Log To Console    ${URL}

This will then result in the following console output

==============================================================================
TC                                                                    
xyz.com/user=${userId}
xyz.com/user=MyUserName
| PASS |
------------------------------------------------------------------------------

Another approach you can take it to move from YAML to Python Variable files. In the Robot Framework Guide there is a chapter on Implementing variable file as Python or Java class which contains several good and simple code examples on how to do this. This may give you that added flexibility you seek to return the right set of variable values.

A. Kootstra
  • 6,827
  • 3
  • 20
  • 43
  • Thanks, but I am trying to access "Variables.yaml" file into another file generics.robot and there I am not writing any test cases, "generics.robot" consist of an only custom keyword file, where I am using this URL.So, will the replace variables keyword be used in that file? – Suyash Nande Aug 30 '18 at 18:53
  • Also, here you are replacing the variable values as "MyUserName", however, my requirement is that, I should not use any variables into my test case file, I have the Variables.yaml file for that. – Suyash Nande Aug 30 '18 at 19:13
  • Ya, I created a python variable file, and I am able to substitute the variables dynamically now. Thanks everyone! – Suyash Nande Aug 31 '18 at 16:43
  • @A. Kootstra Is there any way to define variables inside yaml file. I want to define some system variables like key: ${EXECDIR}${/}mydir in yaml. I've multiple such system variables inside yaml. Replace variable requires me to first read particular key and then replace it. Is there a way to replace the content of whole file. I'm reading the file like "Variables testdata.yaml" – SeekerN Sep 16 '22 at 09:29