Ask a Question

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

SOLVED
MRutten
Occasional Contributor

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)

Count 2022-04-13_14-36-06.png
 

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']}

 

LoopCount 2022-04-13_14-53-09.png

 

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

6 REPLIES 6
giovanni_favara
Contributor

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

 

KarelHusa
Super Contributor

@MRutten ,

using JSON DataSource might help here.

 

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

 

KarelHusa_1-1649859099792.png

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:

 

KarelHusa_2-1649859409899.png

 

Best regards,

Karel

 

Karel@apimate.eu
https://apimate.eu
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

 

 

MRutten
Occasional Contributor

@KarelHusa works like a charm! Thanks!

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

 

MRutten
Occasional Contributor

@giovanni_favara 

That did the trick.

 
This DOES work:

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

 

('Daag meneer' -> 'Geachte heer' or 'Hallo meneer'  but it is too formal on a community) 😉 

cancel
Showing results for 
Search instead for 
Did you mean: