richie
8 months agoCommunity Hero
GroovyScript Help - Repeating JSON Attributes Have Specific Value.....some of the time?
Hey!
Ok - so I've got GET request with multiple different query parms -one of these query parms is 'handling_codes'.
The GET returns a JSON response payload that contains multiple 'CONSIGNMENT' r...
- 8 months ago
Here is the complete script which uses the attached file and asserts the codes
Please follow the inline comments:
//Script Assertion def json = new groovy.json.JsonSlurper().parseText(context.response) def expectedCode = 'SPX' //find all non empty handling_codes def hc_list = json.data.'handling_codes'.findAll() //List the non empty handling_codes item count log.info hc_list.size //Iterate thru each handling_codes, then check the code list contains at least expectedCode hc_list.each { hc -> log.info hc.code assert hc.code.any {it == expectedCode} , "$hc does not contain $expectedCode" }
EDIT:
Use the above script as Script Assertion where you get the response (not separate groovy script test step)
There are some empty handling_codes are there in the data and assuming that you want to ignore them, hence .findAll() used as shown below (line #5 of the script)
def hc_list = json.data.'handling_codes'.findAll()
if you also need to consider them, then change it to
def hc_list = json.data.'handling_codes'