Forum Discussion

MRutten's avatar
MRutten
Occasional Contributor
3 years ago

REST Response: executing several teststeps for every object in REST response. How?

GOAL

Execute several Test steps (GET requests) for every Object in a REST Query Response.

 

SCENARIO

I want to build a Functional Test

1. Execute GET Query request that will have several results in the response (there is a "count" element in the response with the number of results)


 

2. For every result in the response i want to execute several GET requests to obtain the details of each Result 

 

WHAT I AM TRYING

1. Store the value of "count" in a Property so i know how many loops i have to perform

2. Define another Property 'loopCount' that will count the number of loops

3. Use the property 'loopCount' to refer to elements of a certain Result 

Example:

In one of the Test Steps a Request proprerty is used that refers to the first Result: 
${Query Zaken - GET by bsn#Response#$['results'][0]['uuid']}

I was thinking to make this dynamically using the 'loopCount' property.

${Query Zaken - GET by bsn#Response#$['results'][<some reference to property loopCount>]['uuid']}

 

 

QUESTIONS

1. Is this the way to go or are there nicer/better solutions possible?

2. How can i make a dynamically reference to a certain Result in the list with Results in subsequent GET requests?  

 

Kind regards,

Maarten

  • MRutten ,

    using JSON DataSource might help here.

     

    An example for illustration (using famous Pet store use case from https://petstore3.swagger.io/ )

     

    How it works:

    1. GetPets: We get all pets with a GET query
    2. PetsDataSource: Parsing the GetPets response into a Data Source
    3. GetPetDetail: Using the value within a request
    4. Data Source Loop: Taking another set of values from Data Source and jump to GetPetDetail.

    There may be more complex processing between GetPetDetail and the end of the loop if needed.

    Here's the configuration of the Data Source:

     

     

    Best regards,

    Karel

     

  • Daag meneer MRutten , ğŸ™‚

    ${loopCount} is wrong.
    Use ${#Properties#loopCount}.
    After ${# you must wirte the location, in your case the properties test step. On the end you write the name of the property. In your example is the name of the property #loopCount}.

     

    Best wishes, Giovanni

     

  • Hello MRutten ,
    here are my answers:

     

    To 1)
    It depends on your skills and what are for you nice. A another way is to use more Groovy Script. Without Groove you can not resolve your issue.

     

    To 2)
    At same way you get the value for uuid.
    ${Query Zaken - GET by bsn#Response#$['count']}

     

    To 3)

    You can use nested properties. See below the link. There is the nested properties described and an example.

    https://www.soapui.org/docs/scripting-and-properties/property-expansion/

    But this not enough. You need a loop and you can realize this with a Groovy Script - see below.

     

    You need a Groovy script after the test step with the query. This script check the count. Is the actual count greater then max count value (size of the array in the JSON) the script do nothing and the test case ends else increase the property count in the test step properties (first test step in your test case) and then in the script give the command to jump in to the query test step (testRunner.gotoStepByName("Query Zaken - GET by bsn")). You get the value from the count from the test step "Properties" with ${Properties#count}. This test step is first test step in you test case.
    Finally you must write a script for the test case setup script. In this script you must initialize the count property in the test step "Properties" (context.getTestCase().getTestStepByName("Properties").setPropertyValue("count", 0)).

    I hope that my hints are helpful.

     

    Greetings

     

    • MRutten's avatar
      MRutten
      Occasional Contributor

      Hi giovanni_favara,

       

      Thanks for your clear explanation! 

       

      I have defined a Custom Test Case Property 'loopCount'.

      In a Groovy script i assign a value to this property: 

      testRunner.testCase.setPropertyValue( "loopCount", "0" ) 

       

      But now i am struggling with using this property 'loopCount' in the script that determines the value of the Request parameter UUID_REF.

      (see second screenprint in my initial Question)

       

      This doesn't work:

      ${Query Zaken - GET by bsn#Response#$['results'][${loopCount}]['uuid']}

       

      What am doing wrong?

       

      Kind regards,

      Maarten

       

       

      • giovanni_favara's avatar
        giovanni_favara
        Contributor

        Daag meneer MRutten , ğŸ™‚

        ${loopCount} is wrong.
        Use ${#Properties#loopCount}.
        After ${# you must wirte the location, in your case the properties test step. On the end you write the name of the property. In your example is the name of the property #loopCount}.

         

        Best wishes, Giovanni

         

  • KarelHusa's avatar
    KarelHusa
    Champion Level 1

    MRutten ,

    using JSON DataSource might help here.

     

    An example for illustration (using famous Pet store use case from https://petstore3.swagger.io/ )

     

    How it works:

    1. GetPets: We get all pets with a GET query
    2. PetsDataSource: Parsing the GetPets response into a Data Source
    3. GetPetDetail: Using the value within a request
    4. Data Source Loop: Taking another set of values from Data Source and jump to GetPetDetail.

    There may be more complex processing between GetPetDetail and the end of the loop if needed.

    Here's the configuration of the Data Source:

     

     

    Best regards,

    Karel