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

6
  • You cannot call a key variable in a dictionary (how it's done here). Keys with the same names are not available in the default Python dictionaries. Commented Jan 10, 2018 at 12:43
  • That was a typo..corrected now Commented Jan 10, 2018 at 13:49
  • Is there a reason you don't do this in the variable file itself? Commented Jan 10, 2018 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. Commented Jan 10, 2018 at 14:58
  • @BryanOakley it's a requirement, parameters needs to be passed as scalar data type from python file Commented Jan 10, 2018 at 15:52

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.