Search code examples
xmlgroovyjmeterjsr223

How to use XMLSlurper with JMeter JSR223 assertion?


I am trying to extract text from an element, using JSR 223 assertion and XmlSlurper

offer=vars.get('CurrentOfferUnderTest')
def xml = new XmlSlurper(false,true).parseText(SampleResult.getResponseDataAsString())
if(offer=='410012'){
    //assert xml.ResultHeader.ResultCode.text()=='102020006'
    log.info(xml.Envelope.Body.LoanManageResultMsg.ResultHeader.ResultCode.text())
    log.info(xml.'*'.ResultCode.text())
    a=xml.'*'.find{node -> node.name()=='ns2:ResultCode'} //regardless of namespace, same result
    assert a.text()=='102020006'
}

whenever I do log just the xml var, I see text in the logs, when I try ad navigate as per above I get blanks\nulls. even when doing xml.Envelope.text() hence I added the namespace aware boolean. It didn't really help. What am I doing wrong?

XML excerpt

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns4:LoanManageResultMsg xmlns:ns4="http://www.p.com/wsdl/manageloanmgr"
                               xmlns:ns2="http://www.p.com/wsdl/shared"
                               xmlns:ns3="http://www.p.com/wsdl/loan"
                               xmlns:ns5="http://www.p.com/wsdl/newsubscriber"
                               xmlns:ns6="http://www.p.com/wsdl/query"
                               xmlns:ns7="http://www.p.com/wsdl/subscribe">
         <ResultHeader>
            <ns2:CommandId>LoanManage</ns2:CommandId>
            <ns2:Version>1</ns2:Version>
            <ns2:TransactionId>6</ns2:TransactionId>
            <ns2:SequenceId>4u6w6qry3</ns2:SequenceId>
            <ns2:ResultCode>102020006</ns2:ResultCode>
            <ns2:ResultDesc>Operation invalid.</ns2:ResultDesc>
            <ns2:OrderId>0</ns2:OrderId>

this did not help Are XmlParser and XmlSlurper namespace aware by default?


Solution

  • I don't think you should be using Envelope in your GPath because your xml object resolves to the Soap Envelope already.

    enter image description here

    More information: