How to access a variable inside another variable in yaml file?

11,338

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.

Share:
11,338
Suyash Nande
Author by

Suyash Nande

Updated on June 04, 2022

Comments

  • Suyash Nande
    Suyash Nande almost 2 years

    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}
    
    • Torsten Engelbrecht
      Torsten Engelbrecht over 5 years
      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?
    • Todor Minakov
      Todor Minakov over 5 years
      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)?
    • Suyash Nande
      Suyash Nande over 5 years
      @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.
    • Todor Minakov
      Todor Minakov over 5 years
      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.
  • Suyash Nande
    Suyash Nande over 5 years
    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
    Suyash Nande over 5 years
    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
    Suyash Nande over 5 years
    Ya, I created a python variable file, and I am able to substitute the variables dynamically now. Thanks everyone!