pythonautomationyamlrobotframeworkautomation-testing

Robot Framework System Variable inside Variable Yaml file


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  key

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. So I can not use Replace Variables. 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

  • This is possible with the Robot Framework nested variables. The way you can use this is to have the YAML file look like this

    path/to/data.yml

    DATA:
      exec_dir: EXEC_DIR
    

    Notice that the variable name is here without any fancy decorations. To reference it inside the Robot script you can use it from the imported dictionary like this.

    test.robot

    *** Settings ***
    Variables           path/to/data.yml
    
    *** Test Cases ***
    Fetch Configuration Data
        [Documentation]     This shows how to use Robot Framework runtime 
        ...                 variables from a variable file.
        Log    The whole dict contains ${DATA}
        Log    The variable name is ${DATA["exec_dir"]} and its value is ${${DATA["exec_dir"]}}
    

    Notice the additional ${ } wrapping around the variable name to make it evaluated correctly.

    This actually will work with any variables as long as they are defined before calling them in the Robot - even user defined variables can be called like this but not before they are defined in the script. YAML does not provide variable evaluation.