Asserting on contents of a .json response in output log & passing attribute values to propertiesstep
Hi,
nmrao put a script together for me to build a GET REST request's URI and query string using groovy (I cant use the OTB functionality due to other project requirements) and the GET request's response is logged in the 'log output' window.
I need to assert on contents of this .json response and also perhaps grab certain attribute values for property transfer.
I've been investigating how to do this (i was looking at the 'Assertion' test step - but I found the instructions quite confusing!) and was wondering if someone could comment on my groovy script attempt to add assertions into my script?
The final bit of Rao's script is as follows:
def response = client.get(path: template.make(binding) as String, accept: ContentType.JSON, query : queryParams ) assert response.statusCode == 200
I tried adding the following assertion
assert response.header.success == "true"
(where json for the success attribute within the header object == $['header']['success'] = "true")
But that didnt work.
I tried adding into the existing script - but adding the jsonslurper import dec at the beginning of the script and tried the following (adding the ParsedResponse variable and json path assertions)
def client = new RESTClient(serviceHost) def response = client.get(path: template.make(binding) as String, accept: ContentType.JSON, query : queryParams ) assert response.statusCode == 200 log.info groovy.json.JsonOutput.prettyPrint(response.text) // Set up JSON parser of the response def parsedResponse = new JsonSlurper().parseText(response); //thought this syntax might work for json response - but it didn't assert (parsedResponse["header"].["Success"] == "true"); assert (parsedResponse["data"].["Links"].["method"] == "GET"); assert (parsedResponse["data"].[VersionNumber] == 1); //also tried this syntax for the json response in the logged output - but it didn't assert parsedResponse.header.Success == "true" assert parsedResponse.data.Links.method == "GET" assert parsedResponse.data.VersionNumber == 1
but the script failed before it even got to the assertions - script said "groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText()" <<- so it didnt like my scripting alteration!
So - as you can see my groovy is rubbish - can anyone point me in the right direction? Essentially I need to be able to assert on the contents of the json response (in the log output window) and I also need to be able to grab attribute values and store in a properties step for later use (I need to pass the value onto a following JDBC step).
Oh - I've attached an example json response for clarity
thanks to all!
richie
Hi richie,
I've modified your code in the following way:
import groovy.json.* def response = context.expand( '${REST Request#Response}' ) def object = new JsonSlurper().parseText(response) assert object.header.success == true assert object.data[0].Links.method == "GET" assert object.data[0].VersionNumber == 1
It works fine for me: