Forum Discussion

Tareque's avatar
Tareque
Occasional Contributor
17 years ago

Assert using XPath in XML contained in CDATA

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.

  <ovm xmlns="http://next.com/ovm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://next.com/ovm <!-- m --><a class="postlink" href="http://localhost:8000/ovm/xsd/ovm-response.xsd">http://localhost:8000/ovm/xsd/ovm-response.xsd</a><!-- m -->"><br />&nbsp; &nbsp; <ovm-error-info><br />&nbsp; &nbsp; &nbsp; &nbsp; <error-type>2</error-type><br />&nbsp; &nbsp; &nbsp; &nbsp; <error-code>144</error-code><br />&nbsp; &nbsp; &nbsp; &nbsp; <error-details>This is a corporate/government order, but no corporate/government identifier was sent.</error-details><br />&nbsp; &nbsp; </ovm-error-info><br /></ovm>
 
 
 

2 Replies

  • Tareque's avatar
    Tareque
    Occasional Contributor
    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
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    This looks like a fine approach to me :-)

    regards!

    /Ole
    eviware.com