Forum Discussion

shiva1020243's avatar
shiva1020243
Occasional Contributor
4 years ago

Groovy script to aggregate data in each iteration from POST request

Hi Team,

 

Can somebody please help me? 

I've one data source - Groovy ( I wrote a script where it gives accounts numbers for each client) let's say got 3 account numbers, now I've to pass one by one as a POST request so that the response will give me some data (let's say points) next step I've is Data source loop.

 

Now the question is how can I store the points data for each client and aggregate them in each iteration? 

 

Structure 

DS - Fetch accounts

POST REQUEST and GET Points in response

Groovy Script - here I have to write the script (i tried to get the response from above request and tried to stored in list but it is storing the points of last record only)
DS LOOP

 

Thanks in advance!

  • ChrisAdams's avatar
    ChrisAdams
    4 years ago

    If the datasink is inside the loop, then it should record each response.  Without a screenshot, it's really difficult to properly help.  You may have simply configured it wrong.

     

    As for properties, the value will be overwritten on each iteration.  Now, the properties are string name/value pairs.  I'm assuming that you don't know how many iterations, so you could concatenate each value to the string and aggregate after the loop.

     

    E.g.

    Initial ""

    Loop 1 : "20,"

    Loop 2 : "20,23"

    Loop 3 : "20,23,26"

    etc.

     

    Or have have two property values and total up on each iteration.  E.g.

    Initial : running-total = 0.  count = 0.

    Loop 1 : running-total = 20.  count = 1.

    Loop 2 : running-total = 43.  count = 2.

    Loop 2 : running-total = 69.  count = 3.

     

    Be aware, property values are strings.  Before calculating them, you'll need to convert to integer and convert back to sting when saving them.

  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    Hi,

     

    Calculations, running totals won't stay in memory.  You need to access, update and persist the values on each iteration.

     

    Have a look at properties.  In this case, look up test case properties.  You can create, read and write to to these properties and access from any step within your test.  Think of them as global variables for your test.

    There is also a technical article on here that explains these properties really, really well.

    Here is the URL to the technical articles... https://community.smartbear.com/t5/Technical-Articles/tkb-p/readyapi-community-techarticles

    Here is the URL to Lucian's excellent article...https://community.smartbear.com/t5/Technical-Articles/Properties-in-SoapUI-case-study/ta-p/223255

     

     

     

     

    • shiva1020243's avatar
      shiva1020243
      Occasional Contributor

      Hi,

       

      I tried using properties and data sink as well, it is not working..

      Here's is the thing,

      From Data source get the credit card numbers (this is a post request where we post credit cards in the loop then it'll give a response like totalpoints:1235)

      now I'm stuck at the second step - on how to get the points for each credit card and store them in the list and aggregate them - in my case, it is just storing the last credit cards data

      third step data source loop

       

      I tired properties as well, it is replacing the first value with second in the second iteration

      • ChrisAdams's avatar
        ChrisAdams
        Champion Level 3

        If the datasink is inside the loop, then it should record each response.  Without a screenshot, it's really difficult to properly help.  You may have simply configured it wrong.

         

        As for properties, the value will be overwritten on each iteration.  Now, the properties are string name/value pairs.  I'm assuming that you don't know how many iterations, so you could concatenate each value to the string and aggregate after the loop.

         

        E.g.

        Initial ""

        Loop 1 : "20,"

        Loop 2 : "20,23"

        Loop 3 : "20,23,26"

        etc.

         

        Or have have two property values and total up on each iteration.  E.g.

        Initial : running-total = 0.  count = 0.

        Loop 1 : running-total = 20.  count = 1.

        Loop 2 : running-total = 43.  count = 2.

        Loop 2 : running-total = 69.  count = 3.

         

        Be aware, property values are strings.  Before calculating them, you'll need to convert to integer and convert back to sting when saving them.