Anton
Anton

Reputation: 11

Robot framework: variable function with dictionary arguments

I have a variable file which defines a variable like dictionary in dictionary and variable functions.

global.py

DEFAULT_VAL=111
TEST_VAR={'key1':{'elem1':'val1', 'elem2':'val2', 'elem3':'val3'}, 'key2':{'elem2':'val2', 'elem3':'val3'}}

def get_elem1_or_default_1(key):
   return   TEST_VAR[key]['elem1'] if 'elem1' in TEST_VAR[key] else DEFAULT_VAL

def get_elem1_or_default_2(key_dict):
   return   key_dict['elem1'] if 'elem1' in key_dict else DEFAULT_VAL

From robot I can call variable function 'get_elem1_or_default_1' which accept string as key, like this:

*** Settings ***
Variables           Global.py

${var}    Set variable    ${get_elem1_or_default_1('key2')}
INFO : ${var} = 111

But when I try to call another function 'get_elem1_or_default_2' which accept dict as argument I get an error

${key_dict}   Evaluate   ${TEST_VAR}['key1']
${var}    Set variable    ${get_elem1_or_default_2(${key_dict})}

INFO : ${key_dict} = {'elem2': 'val2', 'elem3': 'val3', 'elem1': 'val1'} FAIL : Invalid variable name '${get_elem1_or_default_2({'elem2': 'val2', 'elem3': 'val3', 'elem1': 'val1'})}'.

Is it possible to do so or something is wrong? May be there is another way?

Thanks!

Upvotes: 1

Views: 2563

Answers (1)

Laurent Bristiel
Laurent Bristiel

Reputation: 6935

Your "variable functions" are just functions that should be called as keywords and not as variables. So you you can keep your global.py as it is, but call your functions this way:

*** test cases ***
mytest
  ${key_dict} =   Evaluate  ${TEST_VAR}['key1']
  ${var} =  get_elem1_or_default_2  ${key_dict} 

Upvotes: 1

Related Questions