Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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}
like image 982
Suyash Nande Avatar asked Sep 05 '25 03:09

Suyash Nande


1 Answers

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.

like image 69
A. Kootstra Avatar answered Sep 07 '25 21:09

A. Kootstra