Forum Discussion

zelot's avatar
zelot
New Contributor
9 years ago

How to extract value from xml response in script assertion

Hi,

After sendig a soap request I get this as my response:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:genXXYYZZ xmlns:ns2="http://zzzzzzz.yyy.com/">
<return>
<test>
<status>OK</status>
</test>
</return>
</ns2:genXXYYZZ>
</S:Body>
</S:Envelope>

What I want to do is extract value of status in script assertion. I do this:

- click add an assertion...
- script -> script assertion and then

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( messageExchange.requestContent)
holder.namespaces["ns2"] = 'http://zzzzzzz.yyy.com/';
def responseId = holder.getNodeValue("//ns2:status");
log.info(responseId);

and every time I get this:

 INFO:null
  • Oh, you are trying to get data from request, but not on the response.

    Here you go:

     

    def Envelope=new XmlSlurper().parseText(messageExchange.request.requestContent)
    def status = Envelope.Body.genXXYYZZ.return.test.status
    log.info status
    assert OK == status, "Status is not ok"
  • nmrao's avatar
    nmrao
    Champion Level 3

    Oh, you are trying to get data from request, but not on the response.

    Here you go:

     

    def Envelope=new XmlSlurper().parseText(messageExchange.request.requestContent)
    def status = Envelope.Body.genXXYYZZ.return.test.status
    log.info status
    assert OK == status, "Status is not ok"
    • zelot's avatar
      zelot
      New Contributor

      thx for your answer it almost worked. like you said I was getting data from request so it should be

       

      def Envelope=new XmlSlurper().parseText(messageExchange.request.responseContent)
      def status = Envelope.Body.genXXYYZZ.return.test.status
      log.info status
      assert OK == status, "Status is not ok"

      :)

      • nmrao's avatar
        nmrao
        Champion Level 3

        But note that if you are doing it on response, then use
        messageExchange.response.responseContent