GroovyScript - Extract specific number of attribute values from variable content response
- 7 months ago
You can quickly try this groovy to understand how this can be achieved in general.
Follow inline comments:
def range = 1..10 println "Items in the range: $range" def startingIndex=0 //Set this value, how many values to be stored def endIndex=4 //This is to ensure not to grab more than present in the list assert endIndex <= range.size(), 'Choose the endIndex less than list size' //Now get the sub list from original list def mySubList = range.subList(startingIndex,endIndex) println mySubList //In order to save data as custom property, coierce it to String def mySubListToString = mySubList.join(',') println mySubListToString //Save this at suite level //Convert string back to list when needed later def stringToList = mySubListToString.split() println stringToList
If running the above from the ReadyAPI / SoapUI, use log.info instead of println
Hope you knew how to store mySubListToString into Test Suite custom property.
Note that all the properties in ReadyAPI are stored as string.
- 7 months ago
All it needs here is to replace
def range = 1..10
with
def json = new groovy.json.JsonSlurper().parseText(context.response)
def range = json.data.id.findAll()
Change endIndex value depending on how many values that you need.
In order to store values at test suite level, add below statement at line number 20.
context.testCase.testSuite.setPropertyValue('MY_SUBLIST', mySubListToString)
And lines #22, 23, 24 are not needed here i.e., script assertion, as those to fetch and convert string to list. Instead use the when you need those values later.
By the way, first it requires to fetch the MY_SUBLIST value stored at test suite level, so add below statement before using those lines.
def mySubListToString = context.testCase.testSuite.getPropertyValue('MY_SUBLIST)
Now it has all code it required with your data to achieve what is mentioned in the original question.
NOTE: I am referring the line numbers here in this response from my original response's code.