cancel
Showing results for 
Search instead for 
Did you mean: 

Grabbing .json response attribute value in output log & passing attribute values to propertiesstep

SOLVED
Highlighted
Community 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

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Grabbing .json response attribute value in output log & passing attribute values to properti

@richie ,

 

Is this what you would like to do?

Selection_027.png

 



Regards,
Rao.
3 REPLIES 3
Contributor

Re: Grabbing .json response attribute value in output log & passing attribute values to properti

@richie if custodian is subnode under meta then your code is correct.

 

If meta and custodians are separate nodes then all you have to do is - 

 def custodianval = object.custodian

 

One more thing to consider if those nodes are an array i.e., nodes are in [ ].

If so then you need to reffer that using a counter.

for ex:

object = {meta: [], custodian: [{name:test1, id:1}, {name:test2, id:2}] }

 

def cutodianval = object.cutodian[0] .name  would give you test1

 

hope this helps.

Community Hero

Re: Grabbing .json response attribute value in output log & passing attribute values to properti

@richie ,

 

Is this what you would like to do?

Selection_027.png

 



Regards,
Rao.
Community Hero

Re: Grabbing .json response attribute value in output log & passing attribute values to properti

Hey @New2API & @Rao

 

thanks and Kudos to both - my script was generating multiple compilation errors on the code snippet to extract the property value:

 

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script15.groovy: 41: Ambiguous expression could be a parameterless closure expression, an isolated open code block, or it may continue a previous statement; solution: Add an explicit parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L

@Rao - yep that works - thank you!  Exactly what I need!

 

Cheers!

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Ask a SmartBear
Top Kudoed Authors