2

I have a scenario where I need to do different set of actions depending on dictionary key and value

Dictionary can be any of the below

&{DICT_VAL_APPLINPUT}=   Create Dictionary      status=Single    employmentType=PAYG  income=
&{DICT_VAL_APPLINPUT}=   Create Dictionary      name=  status=Single  employmentType=PAYG  income=
&{DICT_VAL_APPLINPUT}=   Create Dictionary      name=ABC  status=Single  employmentType=PAYG  income=

*** Test Cases ***
Test
    &{DICT_VAL_APPLINPUT}=   Create Dictionary      status=Divorced  employmentType=PAYG  income=
    Check Dictionary Key or Value Exist             ${DICT_VAL_APPLINPUT}   name

*** Keywords ***    
Check Dictionary Key or Value Exist
        [Documentation]     checks whether dictionary key and value exists
        [Arguments]         ${sp_dict_keyvalue}     ${key}
        ${sp_retkeystatus}=     run keyword and return status    dictionary should contain key    ${sp_dict_keyvalue}     ${key}
        ${sp_retstatus}=        run keyword if  '${sp_retkeystatus}'=='False' return from keyword    False
        ...                     ELSE IF     '${sp_dict_keyvalue}[${key}]'=='${EMPTY}'    return from keyword    False
        ...                     ELSE        return from keyword    True
        [Return]    ${sp_retstatus}

I am getting below error when I send dictionary with no key even after validating key.

Dictionary '${sp_dict_keyvalue}' has no key 'name'.

It is failing at step

...                     ELSE IF     '${sp_dict_keyvalue}[${key}]'=='${EMPTY}'    return from keyword    False

*** Settings ***
Library           Collections

*** Test Cases ***
Test
    &{DICT_VAL_APPLINPUT}=   Create Dictionary      status=Divorced  employmentType=PAYG  income=
#    Set To Dictionary    ${DICT_VAL_APPLINPUT}    name=
    Log    ${DICT_VAL_APPLINPUT}
    Check Dictionary Key or Value Exist             ${DICT_VAL_APPLINPUT}   name

*** Keywords ***
Check Dictionary Key or Value Exist
        [Documentation]     checks whether dictionary key and value exists
        [Arguments]         ${sp_dict_keyvalue}     ${key}
        ${sp_retkeystatus}=     run keyword and return status    dictionary should contain key    ${sp_dict_keyvalue}     ${key}
        ${sp_retstatus}=        run keyword if  '${sp_retkeystatus}'=='False'    return from keyword    False
        ...                     ELSE IF     '${sp_dict_keyvalue}[${key}]'=='${EMPTY}'    return from keyword    Empty
        ...                     ELSE        return from keyword    True
        [Return]    ${sp_retstatus}

See below screenshot of the log enter image description here

2

Issues:

  1. You are using create dictionary keyword so it will have only the below content:

    { status=Divorced | employmentType=PAYG | income= }

so each time you creat a new dictionary, it will have only that specific content

  1. You don't have Collections imported , so the status false you are getting from "Dictionary should contain key" is actually keyword not found and not that key is not there.

  2. There is only one space after 'False' in : run keyword if '${sp_retkeystatus}'=='False' return from keyword. hence the keyword is searching for the sting ${sp_retkeystatus}=='False' return from keyword and hence it goes on to check whether the key is empty . So, it goes to check empty condition for all the cases even if 'name' is not present.

Hence in case name is not present , you are validating {sp_key}[key]==Empty and it throws error at {sp_key}[key] as there is no key 'name' in sp_key dictinary

  • Robotframework is trying to resolve the variables in '${sp_dict_keyvalue}[${key}]'=='${EMPTY}' before executing the logic , so even when status is false its trying to resolve it be fore running the if elseif else loop

So the working code is:

    *** Settings ***
Library           Collections

*** Test Cases ***
Test
    &{DICT_VAL_APPLINPUT}=   Create Dictionary      status=Divorced  employmentType=PAYG  income=
    Set To Dictionary    ${DICT_VAL_APPLINPUT}    name=
    Log    ${DICT_VAL_APPLINPUT}
    Check Dictionary Key or Value Exist             ${DICT_VAL_APPLINPUT}   name

*** Keywords ***    
Check Dictionary Key or Value Exist
        [Documentation]     checks whether dictionary key and value exists
        [Arguments]         ${sp_dict_keyvalue}     ${key}
        ${sp_retkeystatus}=     run keyword and return status    dictionary should contain key    ${sp_dict_keyvalue}     ${key}
        ${sp_retkeyval_status}=    run keyword if  ${sp_retkeystatus}    Get From Dictionary    ${sp_dict_keyvalue}     ${key}
        ${sp_retstatus}=        run keyword if  '${sp_retkeystatus}'=='False'    return from keyword    False
        ...                     ELSE IF     '${sp_retkeyval_status}'=='${EMPTY}'    return from keyword    Empty
        ...                     ELSE        return from keyword    True
        [Return]    ${sp_retstatus}

Code Explanation

  1. Check if dictionary has key ${sp_retkeystatus}= run keyword and return status dictionary should contain key ${sp_dict_keyvalue} ${key}
  2. If exists then get the value ${sp_retkeyval_status}= run keyword if ${sp_retkeystatus} Get From Dictionary ${sp_dict_keyvalue} ${key}
  3. Then return false if no key , return empty if key exists but value is empty, else True

Tips:

use log.html properly , it will show exactly whats the reutn value for each keyword. Use TRACE Level robot --loglevel TRACE test.robot

Output:

enter image description here

| improve this answer | |
  • Thanks for the reply. I have a scenario where dictionary doesn't have key 'name=', so I commented 'Set To Dictionary' line, it still fails with the same error. I expected this line 'run keyword if '${sp_retkeystatus}'=='False' return from keyword False' to be executed – AVB Apr 16 at 20:23
  • @user2036678 copy paste the entire code I have given n run it – PDHide Apr 16 at 20:56
  • @user2036678 just to get more clarity I made it print empty instead of false . First see if empty is getting printed n then change it to false if it works – PDHide Apr 16 at 21:30
  • I tried your code, still getting the same error. Please see below code – AVB Apr 16 at 22:35
  • @AVB Sorry i missed that , i updated my answer please try now – PDHide Apr 17 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.