AutoTig
AutoTig

Reputation: 105

Splitting a REST response to separate fields in groovy and soapUI

I am working on a test suite in SoapUI that contain both REST and SOAP requests. The scenario begins with a REST request. From the response I need to take the user name value. The response is the following:

{
   "user_name": "Z.ZCLGN",
   "first_name": "Tester1",
   "user_code": "19225",
   "last_name": "QA"
}

I need the "Z.ZCLGN". How do I split the username value so I could transfer it to the next Soap Request? should I use split like I'm trying to do and if so how?

Thats what I have so far in a groovy script:

def responseAsXml = context.expand( '${GetToken#ResponseAsXml#declare namespace ns1=\'https://10.1.9.13/cvbs/oauth2/token\'; //ns1:Response[1]/ns1:user[1]}' )
log.info responseAsXml
def (user_name, first_name, Tester1, user_code, last_name) = responseAsXml.split("")

Upvotes: 2

Views: 2697

Answers (1)

Renato
Renato

Reputation: 13690

You don't need Groovy scripting for that. Use Property Transfer.

http://www.soapui.org/Functional-Testing/transferring-property-values.html

But if you love scripting so much you still want to do it, here you go:

def json = new JsonSlurper().parseText(jsonText)
def userName = json.user_name
// now use userName in some other request by setting a property, for exampl
testRunner.testCase.setPropertyValue( "MyProp", userName )

More help about Json parsing here:

http://mrhaki.blogspot.se/2011/04/groovy-goodness-parse-json-with.html

But I would also recommend reading the tips for SoapUI beginners:

http://www.soapui.org/Getting-Started/10-tips-for-the-soapui-beginner.html

Upvotes: 1

Related Questions