Forum Discussion
You can get an entire JSON response in an assertion script step.
import groovy.json.JsonSlurper def resp = messageExchange.response.responseContent def jsonSlurper = new JsonSlurper().parseText(resp)
from there you can access fields like jsonSlurper.fieldName1
or output the json
log.info jsonSlurper.toString()
I know you really want to do this in an ordinary Groovy script though.
I have a real working test case structured with the following steps
Test case xyz
groovy script 1
properties (2)
request step 1
groovy script 2
request step 2
So in my groovy script 2 I have code like
import static groovy.json.JsonOutput.toJson; import groovy.json.JsonSlurper import groovy.json.JsonBuilder def previousTestStep = testRunner.getTestCase().getTestStepByName("request step 1") String propertySpec = '${' + previousTestStep.name + '#Response}' def response = context.expand(propertySpec) def jsonSlurper = new JsonSlurper().parseText(response)
I probably don't use the first import in the code above.
I use JsonBuilder to construct new json and then set the json for the request step 2.
You can print out the json from the response (I assume response to your POST)
log.info jsonSlurper.toString()
The catch is that you nave to know the name of the request test step.
Bill
Related Content
- 11 months agoakilan