[SOLVED] How to access a variable inside another variable in yaml file?

Issue

This Content is from Stack Overflow. Question asked by SeekerN

Is there a way, robot framework system variables can be defined in Variable Yaml file.
For example; I’ve a yaml file like

testdata.yaml

key: ${EXECDIR}${/}mydir

Importing file in robot Test Case:

*** Settings ***
Variables  testdata.yaml

*** Test Cases ***
TestCase1
  log  key1

This is literally printing “${EXECDIR}${/}mydir” rather than the value of EXECDIR.

EXECDIR is just an example, I’ve a requirement to use many robotframework system variables inside yaml in customized way.
I’ve checked an existing question, How to access a variable inside another variable in yaml file?. But this refers to user defined variable. Is the same limitation applicable for system variables as well?

If it’s not possible then I would have to use
Yaml Load and create another variable holding yaml content in Robot framework dictionary.
But I would prefer:

*** Settings ***
Variables   testdata.yaml 



Solution

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.


This Question was asked in StackOverflow by Suyash Nande and Answered by A. Kootstra It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?