Forum Discussion

VincentVonk's avatar
VincentVonk
Occasional Contributor
11 years ago

Stripping dynamicly XML parrent tags with SoapUI PRO

Hi,

I have a question, currently we are using in SoapUI pro for our test data an excelsheet DataSource, to fill the XML requests. In some cases an XML element is not needed in the request, because in an testcase it is not needed (leaving it empty is not allowed and gives back errors in our services).

What we currently do to solve this is:

DataSource with excelsheet
A disabled Teststep with all fields filled dynamically from the Excelsheet
Groovy Script step with the following code:

import com.eviware.soapui.support.xml.XmlUtils

def request = context.expand( '${Place holder#Request}' )
def parsedXml = new XmlParser().parseText(request)
def buffer = new StringWriter()
new XmlNodePrinter(new PrintWriter(buffer)).print(parsedXml)
def filteredRequest = buffer.toString()
filteredRequest = XmlUtils.prettyPrintXml(filteredRequest)
filteredRequest = filteredRequest.replaceAll( "<(.+)>--REMOVE--<(.+)>", "")
filteredRequest = XmlUtils.prettyPrintXml(filteredRequest)
context.requestContent = filteredRequest


Then another test step with the same Envelope/Header as the first request but for the body the result of the groovy test script.

If we use in the excel file --REMOVE-- it works fine for most cases. However in some situations it is required to remove the parent node , otherwise it still gives back errors. Do you know have any idea how we could achieve this?

Thank you for your help

4 Replies

  • GiscardN's avatar
    GiscardN
    Frequent Contributor
    Hi, you can use a script in a RequestFilter.filterRequest event to remove empty nodes from your
    request. You can learn more how to work with event handlers here:
    http://www.soapui.org/Scripting‐Properties/custom‐event‐handlers.html

    You could further customize your script to run in a loop and check whether there are (still) empty parent nodes once an empty (child) node has been removed. Custom Groovy solutions are outside of the scope of our support, but I hope this can get you started.

    Please see this post in our forum also:http://forum.soapui.org/viewtopic.php?f=2&t=21175&sid=b8f0f8db79324a37670e432dc5b5ced9&sid=b8f0f8db79324a37670e432dc5b5ced9#p48766

    Thanks,

    Giscard
    SmartBear Software
  • VincentVonk's avatar
    VincentVonk
    Occasional Contributor
    Thank you for your response, the option remove empty content in SoapUI solved the need for the extra groovy script almost. I still have 2 services that also need the parent node removed.

    With the option remove empty content it removes the child tags, however it still has the parent tags in the request. You state i could use a RequestFilter.filterRequest in the events tab on project level. I see how i can add one, but it is not completely clear what kind of (groovy?) code i need to fill it with, to make it work?
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    You can use this groovy script to remove the empty tags using it with the RequestFilter.filterRequest event.

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def stepName = context.getCurrentStep().getLabel()

    def holder = groovyUtils.getXmlHolder( stepName + "#Request" )

    // set default value here
    def defaultVal = ""

    for( item in holder.getDomNodes( "//*[. = '']" )){
    holder.removeDomNodes("//"+item.nodeName)
    }
    // update request
    holder.updateProperty()
    // write updated request back to teststep
    context.requestContent = holder.xml


    Regards,
    Marcus
    SmartBear Support
  • pbmca27's avatar
    pbmca27
    New Contributor

    Hello

     

    I have 

    1. A Data source with 3 columns (firstName, lastName) with a row of data

    2. In one row the firstName is empty

    3. I have a SOAP test with the Properties mapped

     

    Now everytime the row with missing firstName is hit, it is introducing <firstName/> in the request XML (which can only be seen from Raw view) and the response fails (since there is a rule that any of these firstName, lastName should either do not have values or value of length 1 - 10).

     

    So the Request XML I am expecting is

     

    <customerSearchRequest xmlns="http://www.XYZ.net/services/customersearch">
    <customerName>
    <lastName>Langley</lastName>
    </customerName>
    </customerSearchRequest>

     

    but it is taking 

     

    <customerSearchRequest xmlns="http://www.XYZ.net/services/customersearch">
    <customerName> 

    <firstName></firstName>
    <lastName>Langley</lastName>
    </customerName> 
    </customerSearchRequest>

     

    I followed the post and created a RequestFilter.filterRequest event and wrote the below code in the Edit section (I am using ReadyAPI)

     

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def stepName = context.getCurrentStep().getLabel()

    def holder = groovyUtils.getXmlHolder(stepName + "#Request")

     

    for( item in holder.getDomNodes( "//*[. = '']" )){
    holder.removeDomNodes("//"+item.nodeName)
    }

    holder.updateProperty()

    context.requestContent = holder.xml

    But I am unable to remove <firstName></firstName> from the request. Where am I doing wrong?