Tareque
18 years agoOccasional Contributor
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 /> <ovm-error-info><br /> <error-type>2</error-type><br /> <error-code>144</error-code><br /> <error-details>This is a corporate/government order, but no corporate/government identifier was sent.</error-details><br /> </ovm-error-info><br /></ovm>
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.