Forum Discussion

massimo65's avatar
massimo65
New Contributor
6 years ago

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>
<ns1:processRequestResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://services.ni.devoteam.vpg.com">
<processRequestReturn href="#id0"/>
</ns1:processRequestResponse>
<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

  • 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.

  • Shivani1's avatar
    Shivani1
    Occasional Contributor

    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

     

    • massimo65's avatar
      massimo65
      New Contributor

      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>
      <ns1:processRequestResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://services.ni.devoteam.vpg.com">
      <processRequestReturn href="#id0"/>
      </ns1:processRequestResponse>
      <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

       

       

       

  • nmrao's avatar
    nmrao
    Champion Level 3

    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.