asyrjasalo / RESTinstance

Robot Framework library for RESTful JSON APIs
https://pypi.org/project/RESTinstance
GNU Lesser General Public License v3.0
205 stars 84 forks source link

Check if 1 or more items in JSON array has matching param value #92

Closed schkolne closed 3 years ago

schkolne commented 4 years ago

Hi - new to RF and loving RESTinstance thanks so much for writing it!

I want to write a test to see if any of my purchases have a certain name. I can successfully test the to see if 0th instance matches...

Purchase exists with name
    [Arguments]                    ${name}
    Set Headers                    { "authorization": "Bearer ${authtoken}"}
    GET                            /purchases
    ${purchases} =                 Output    response body    /dev/null
    Should Be Equal As Strings     ${purchases}[0][name]   ${name}

... but scratching my head as to how to generalize this to check all N purchases to see if any match. I know this must be simple but after quite a bit of fiddling I can't make it happen. Anyone know the answer?

[while we're at it -- you'll notice some grossness in my sample... I ended up making my Output go to /dev/null to store a variable without printing it to the output... is that really the best way, feels like I'm missing something]

schkolne commented 4 years ago

for those who care, i ended up solving this using RF's json library like so

Purchase exists with name
    [Arguments]                  ${name}
    Set Headers                  { "authorization": "Bearer ${authtoken}"}
    GET                          /purchases
    ${purchases} =               Output    response body   /dev/null
    ${values}=                   Get Value From Json     ${purchases}     $..name
    List Should Contain Value    ${values}    ${name}

make sure to add Library JSONLibrary at top of file

asimell commented 3 years ago

Hi @schkolne

For Output the only option is redirect it to /dev/null.

For your issue, I think it could be solved by using String keyword. It accepts JSONpath as the first parameter and you can add a validation to it as well.

E.g

Purchase exists with name
    [Arguments]                  ${name}
    Set Headers                  { "authorization": "Bearer ${authtoken}"}
    GET                          /purchases
    String   $..name    ${name}