Forum Discussion
I'm sorry that my plea for assistance is so unhelpful. This kind of reaction is why I seldom post to community forums. I already feel like an idiot because I can't figure it out on my own. If a community hero cannot understand me, maybe I should keep away.
I want a groovy solution. Rather than the response, I think you want to see my request XML. The scripts I have are checking the request for existence of an XML element, which in my example I named nonExistentElement, then doing different validations on the response based on what answer comes back about how the request was formulated. The construct as I present in my sample code appears many times in many different scripts, and they are all generating errors which I would like to clean up.
Whether I use the context expansion with an exact XPath, or one with wildcards as I have given, when the element is not there, I get a null back, which is all fine and good for the working of the script conditional branching; however, I also get an annoying error in the error logs and the Ready API logs about "Unexpected element: CDATA". I want to eliminate the error by some means.
I would like some way to evaluate in the groovy script whether an Xpath exists or not, but without generating an error in the case where the Xpath does not exist.
Don't take our asking questions as a personal attack. We're here to help. We just may need more information; that does not reflect at all poorly on you.
As for the groovy script, try this:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ); def request = groovyUtils.getXmlHolder( messageExchange.requestContentAsXml ) def response = messageExchange.getResponseHeaders().toString(); int currValue = request["count(//*:nonExistentElement)"].toInteger(); assert currValue == 0;
- nmrao7 years agoChampion Level 3groovyguy, the above solution only works if there is no CDATA in the xml. Some response xml has an xml wrapped under CDATA and it may not work in such situations.
For example:
https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/CdataInsideXml.groovy
https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/CdataXpathExists.groovy
https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/XmlInsideCdata.groovy- groovyguy7 years agoChampion Level 1
For what it's worth, I've tried this against requests that had CDATA filled elements where the CDATA had xml in it that matched what my script was testing for.
AFAIK, the method I employed does not parse CDATA and count that?
- DouglasY7 years agoOccasional Contributor
Looks like the CDATA example scripts are no longer available.