Forum Discussion

rajsrqa's avatar
rajsrqa
Occasional Visitor
9 years ago

How to add assertion when the REST API response returns more than one record as response

ReadyAPI 1.7.0

I have a response which generates 10 records and i want to loop and verify all the 10 records with assertion.

 

I had a Datasource > Request > Datasource loop mapped correctly. This is an example response and i get more than 1 response like this. <e>-</e> is 1 record and i get more than one record like this.

 

<Response>
<e>
<active>true</active>
<isCategory>false</isCategory>
<isSpecial>false</isSpecial>
<parentId>1036</parentId>
<productid>1022</productid>
</e>

 

In Request Assertion i have "$[0].productid" and expected result ${DataSrc_PrdFilter#products_productid}

 

ISSUE : the first record get executed successfully because the index starts at 0 and the next record which is 1 is not changed there.

Error: Comparison failed $[0].productid": expected value 1008(incorrect, this is the value of index 0) actual value 1022(correct)

 

My Question: what should i specify in place of zero (0) so that it automatically increments and matches the expected vs actual.

  • I suggest you use some Groovy Script and do a loop

     

    example:

     

    for (int i = 0; i < 10; i++) {
    assert expectedData == context.expand( '${Test Step Name#Response#$.e[i].productid}' )
    // i is equal to 0 and will increment each iteration until it reaches 9
    // this will do a loop from index 0 - 9
    // also the expectedData should be incremented also }

     

    if you are using JSON as data you can use JsonSlurper, or XMLSlurper for XML.

    hope this will help you.