0

I have python file, which is mandatory to be used as variable file i.e. -V

content of file is

Volte_user_01='samsung'
Volte_user_02='HTC'
cs_test='Vivo'

I am trying to find a way by which I can store all the variables having 'user' text in variable name in a dictionary in robot framework.

so the dictionary in robotframe work would be like

${Phones} = {Volte_user_01:'samsung', Volte_user_02: 'HTC'}

I went through the documentation , but its bit complex to understand

Any help would be appreciated

pankaj mishra
  • 2,555
  • 2
  • 17
  • 31
  • You cannot call a key variable in a dictionary ([how it's done here](https://stackoverflow.com/questions/3972872/python-variables-as-keys-to-dict)). Keys with the same names are not available in the default Python dictionaries. – dsgdfg Jan 10 '18 at 12:43
  • That was a typo..corrected now – pankaj mishra Jan 10 '18 at 13:49
  • Is there a reason you don't do this in the variable file itself? – Bryan Oakley Jan 10 '18 at 14:30
  • You would probably want to store those variables in config files and pass them as parameters to your python objects when you run the test. The ${..} syntax is normally only used for runtime variables inside a robot testcase. – postoronnim Jan 10 '18 at 14:58
  • @BryanOakley it's a requirement, parameters needs to be passed as scalar data type from python file – pankaj mishra Jan 10 '18 at 15:52
  • I don't understand why you can't pass both the scalars, and a dictionary that has the data you need. – Bryan Oakley Jan 10 '18 at 16:02

1 Answers1

2

I suspect there are better ways to solve the problem you're trying to solve, but here's a literal answer to your question.

Robot has a keyword named get variables which returns a dictionary of all known robot variables. Robot also has a keyword named evaluate which lets you run arbitrary python code. In that code, you can reference robot variables by using the special syntax $varname.

Using those keywords, you can create a dictionary comprehension that creates a new dictionary for you.

In the following example, and using the data from your original question, the keyword Get user variables will return a dictionary that looks like this:

{
    'Volte_user_1': samsung, 
    'Volte_user_02': HTC
}

Note that if you have other variables that have the string "user" in their name, they will show up in the list too.

*** Keywords ***
Get user variables
    [Documentation] 
    ...  Return a dictionary of key/value pairs where the keys
    ...  are all robot variables that have _user_ as part of 
    ...  their name. 
    ${vars}=  get variables
    ${user variables}=  evaluate
    ...  {name[2:-1]:value for (name,value) in $vars.items() if "_user_" in name}
    [Return]  ${user variables}    

*** Test Cases ***
Example
    ${phones}=  get user variables
    should be equal  ${phones['Volte_user_01']}  samsung
    should be equal  ${phones['Volte_user_02']}  HTC
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685