Skip to content Skip to sidebar Skip to footer

How To Make Variables From 'import Variables' Globally Available?

I'm currently using Robot Framework to automate some tests. Problem: I am trying to figure out a way to make variables imported from 'Import Variables' keyword globally available (

Solution 1:

Bear in mind that Global variables should be constant in nature. Event though it is possible to update Global variables from a keyword or Test Case, this should be an exceptional action. For regular updating of variables the Suite, Test Case and Keyword scopes should be used.

In the below example I use a YAML variable file that holds a nested set of values. YAML imports allows for the definition of Python Lists, Scalars and Dictionaries in a human readable format, which is what makes them so useful for these types of nested imports.

data.yaml

DATA:Set 1:Lisa:-listitem1-listitem2Dica:dict item 1:value1dict item 2:value2vara:variablevalue1Set 2:Lisa:-listitem3-listitem4Dica:dict item 1:value3dict item 2:value4vara:variablevalue2

The above script is loaded through Variables data.yaml statement below. The rest of the script then takes a specific sub-set of that structure and creates Global Variables from the items by looping through them.

create_globals.robot

*** Setting ***
Library    Collections    

Variables    data.yaml

*** Test Cases ***
Test Case
    Create Globals    Set 2
    No Operation


*** Keywords ***
Get Data Set
    [Arguments]    ${name}
    [Return]    ${DATA['${name}']}

Create Globals
    [Arguments]    ${name}
    ${dataset}    Get Data Set    ${name}
    @{global_var_names}    Get Dictionary Keys    ${dataset}
    :FOR${global_var_name}    IN    @{global_var_names}
    \    Set Global Variable    ${${global_var_name}}    ${dataset['${global_var_name}']}

Because fetching the data set itself is abstracted through a keyword, changing the source of the data to for example a command line import or even a database is relative small change.

Post a Comment for "How To Make Variables From 'import Variables' Globally Available?"