cancel
Showing results for 
Search instead for 
Did you mean: 

How to extract a value from a SOAP response

SOLVED
New Contributor

How to extract a value from a SOAP response

I am new with SoapUI tool. I need to extract the values of "resultOperation" and "errorCode" from the following SOAP response:

 

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=utf-8
Transfer-Encoding: chunked
Date: Mon, 08 Apr 2019 09:49:47 GMT

<?xml version="1.0" encoding="UTF-8"?>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1Smiley TonguerocessRequestResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://services.ni.devoteam.vpg.com">
<processRequestReturn href="#id0"/>
</ns1Smiley TonguerocessRequestResponse>
<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:NIResponse" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:server.ws.ni.vpg.devoteam.com">
<resultOperation xsi:type="xsd:string">0</resultOperation>
<errorCode xsi:type="xsd:string">0</errorCode>
<errorDescription xsi:type="xsd:string">Operazione eseguita con successo</errorDescription>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>

 

Thanks 

Massimo

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: How to extract a value from a SOAP response

@massimo65 

You may use below Script Assertion:

 

assert context.response, 'Response is empty or null'
def xml = new XmlSlurper().parseText(context.response)

def getElementValue = { element -> xml.'**'.find{element == it.name()}?.text() }

log.info 'resultOperation value is ' + getElementValue('resultOperation')
log.info 'errorCode value is ' + getElementValue('errorCode')

Here is the script which you can play and test it online with fixed response.



Regards,
Rao.
4 REPLIES 4
Occasional Contributor

Re: How to extract a value from a SOAP response

Hi,

You can try the following solution:

 

def resOper,errorCode

Response="Your XML"

ResParsed= new XmlSlurper().parseText(Response)

resOper=ResParsed.resultOperation

err=ResParsed.errorCode

log.info resOper,err

 

New Contributor

Re: How to extract a value from a SOAP response

I try but still does not work; after the SOAP step I add the following groovy script:

 

import com.eviware.soapui.support.*
groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
ResponseMessage = testRunner.testCase.testSteps["ADD_VP_NVS"].testRequest.response.contentAsString;
ResParsed = new XmlSlurper().parseText(ResponseMessage)
resOper = ResParsed.resultOperation
err = ResParsed.errorCode
log.info ResponseMessage
log.info ResParsed
log.info resOper
log.info err

 

The result of the four log.info are:

log.info ResponseMessage:

Mon Apr 08 16:41:46 CEST 2019:INFO:<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1Smiley TonguerocessRequestResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://services.ni.devoteam.vpg.com">
<processRequestReturn href="#id0"/>
</ns1Smiley TonguerocessRequestResponse>
<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:NIResponse" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:server.ws.ni.vpg.devoteam.com">
<resultOperation xsi:type="xsd:string">0</resultOperation>
<errorCode xsi:type="xsd:string">0</errorCode>
<errorDescription xsi:type="xsd:string">Operazione eseguita con successo</errorDescription>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>

 

log.info ResParsed:

Mon Apr 08 16:41:46 CEST 2019:INFO:00Operazione eseguita con successo

 

log.info resOper and log.info err:

Mon Apr 08 16:41:46 CEST 2019:INFO:

Mon Apr 08 16:41:46 CEST 2019:INFO:

 

Regards

Massimo

 

 

 

Highlighted
Super Contributor

Re: How to extract a value from a SOAP response

Seems like your xml is not valid. Please validate your xml first form this website :-

https://codebeautify.org/xmlvalidator

 

Provide the correct xml. So, that we will provide you the exact solution.

 

Community Hero

Re: How to extract a value from a SOAP response

@massimo65 

You may use below Script Assertion:

 

assert context.response, 'Response is empty or null'
def xml = new XmlSlurper().parseText(context.response)

def getElementValue = { element -> xml.'**'.find{element == it.name()}?.text() }

log.info 'resultOperation value is ' + getElementValue('resultOperation')
log.info 'errorCode value is ' + getElementValue('errorCode')

Here is the script which you can play and test it online with fixed response.



Regards,
Rao.
New Here?
Join us and watch the welcome video:
Join the exciting event
SeptemberHubBub
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win