richie
7 years agoCommunity Hero
Grabbing .json response attribute value in output log & passing attribute values to propertiesstep
Hey!
Ok - so rao put a groovy script together for me that builds a GET request (template and query parameters as required sourcing the names and values from Properties because ReadyAPI! doesn't allow the user to parameterize a REST request's QueryParamater name values).
The script generates the response in the output log and I'm trying to update the script to actually grab an attribute value from the .json response and save as a property
The script is as follows:
//imports
import groovy.json.*
import wslite.rest.*
//specify the endpoint
def serviceHost = context.expand ('${#Project#TestEndpoint}')
//Below to handle template parameter, additional $ required before
def getNamespaceDatasetPath = '''/api/1/${namespace}/${dataset}'''
//define all template param values as shown below
def binding = [namespace : context.expand( '${#TestCase#namespace}') ,  dataset : context.expand('${#TestCase#dataset}')]
//def binding = [namespace : 'certification-nomenclature', dataset : 'certification']
def template = new groovy.text.SimpleTemplateEngine().createTemplate(getNamespaceDatasetPath)
def queryParams = [:]
//Get the properties of Property Test step to build the query parameters as required
context.testCase.testSteps["Properties"].properties.each {
	queryParams[it.key] = it.value.value
}
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)
//grab the response
def object = new JsonSlurper().parseText(response.text)
//some relevant assertions
assert object.header.success == true
assert object.header.success == true
assert object.meta.type == "certification-nomenclature"
//this is my attempt to alter the script so that it grabs the 'custodian' attribute value and writes it to the Properties2 step
//the JSONPATH to the attribute is as follows (JSONPATH --> $['meta'][custodian])
{
	def custodianvalue = (object.meta.custodian)   //I'm guessing this bit is the problem!
      def propertiesStep = context.testCase.testSteps["Properties2"];
	propertiesStep.setPropertyValue("Custodian", custodianvalue);
	
}
Now I'm pretty sure I've got everything correct except the 'def custodianvalue = ' line.
I have attached an example of a response
Can anyone point out what I'm doing wrong?
As always - thanks to all!
rich