Forum Discussion

DouglasY's avatar
DouglasY
Occasional Contributor
7 years ago

How do I test for non-existence of an XML element while avoiding error: Unexpected element: CDATA

// The following sample is based on advice given on SmartBear community about how to check for the non-existence of an xml element
// https://community.smartbear.com/t5/SoapUI-Open-Source/How-to-check-non-existence-of-a-xml-element/m-p/37511/highlight/true#M15180


def testStepName = context.testCase.getTestStepAt(context.getCurrentStepIndex()).getLabel()
def expectNull = context.expand( '${'+testStepName+'#Request//*:nonExistentField}' )
assert expectNull.isEmpty()

 

// The assertion works as expected; however, the code generates an error when expanding the Xpath
// ERROR:An error occurred [error: Unexpected element: CDATA], see error log for details
// How do I test for non-existence of an XML node without generating an ERROR in SoapUI?

 

12 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Unfortunately the above is not really helpful and this is directly depends on the data that is being used. So, would you mind providing sample data (need not be the original data, but with the same structure) in order to reproduce the issue.
  • groovyguy's avatar
    groovyguy
    Champion Level 1

    We would definitely need to see more to accurately troubleshoot the groovy script you have. You could also use an XPATH assertion with

     

    count(//*:nonExistentElement) and set it to be 0

     

    Otherwise I'd think we'd need to see your response if it has to be done via groovy.

    • DouglasY's avatar
      DouglasY
      Occasional Contributor

      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.

      • groovyguy's avatar
        groovyguy
        Champion Level 1

        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;