Eviware Forum

soapUI => soapUI => Topic started by: Tareque on November 16, 2007, 05:06:18 pm



Title: Assert using XPath in XML contained in CDATA
Post by: Tareque on November 16, 2007, 05:06:18 pm
Hi Ole,
I am trying to add XPath assertions to validate the data that I am getting in a request that has an XML as one of the tag values enclosed in CDATA section. I am able to do it using this Groovy script,
def request = testRunner.testCase.getTestStepByName("AsyncMockResponse").getProperty("request");
println "\n Async Request "
def node = new groovy.util.XmlParser(false,false).parseText(request.value);
def input = node["soapenv:Body"]["ns1:processRequest"]["incomingXml"]
def requestXml = new groovy.util.XmlParser(false,false).parseText(input.value);
def tagValue = requestXml.children()["error-code"]
println tagValue.value.toString()
if(tagValue.value.toString() == '[144]'){
   println "You passed"
}else{
   println "You failed"
   throw new Exception("You failed")
}

Is there a better way to do it using just XPATH.
Regards


PS. I am also attaching a snippet of the XML that I am trying to validate.
<ns1:processRequest soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:WSDLGateway" 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">
  <incomingXml xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"><![CDATA[<ovm xmlns="http://next.com/ovm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://next.com/ovm http://localhost:8000/ovm/xsd/ovm-response.xsd">
    <ovm-error-info>
        <error-type>2</error-type>
        <error-code>144</error-code>
        <error-details>This is a corporate/government order, but no corporate/government identifier was sent.</error-details>
    </ovm-error-info>
</ovm>]]></incomingXml>
  <dealer xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"/>
  <msgType xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"/>
  <systemIndicator xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"/>
</ns1:processRequest>


Title: Re: Assert using XPath in XML contained in CDATA
Post by: Tareque on November 19, 2007, 10:00:49 pm
Hi,
I was able to make the groovy script simpler by introducing a PropertyTransfer and a Properties object. The groovy script now is as follows

def utils = new com.eviware.soapui.support.GroovyUtils(context)
def value = testRunner.testCase.getTestStepByName("Properties").getPropertyValue("XML")
def holder = utils.getXmlHolder(value)
assert holder.getNodeValue("//ns1:ovm-error-info/ns1:error-code") == '254'


Is this a good approach or is there a better way to do this.
Regards


Title: Re: Assert using XPath in XML contained in CDATA
Post by: omatzura on November 20, 2007, 12:19:37 am
Hi!

This looks like a fine approach to me :-)

regards!

/Ole
eviware.com