0

I need to preprocess a SOAP request (using HTTP request) populated using a CSV dataset in jMeter. Some of the binded variables are null which is expected, but the service I am testing performs validation on these elements as well.

There was a similar question in the past (JMeter - Remove empty elements in SOAP request), but in my case I am not looking to only clear the blank element I want to clear the whole block.

Example: I want this

<table>
    <name>businessLicenceGroup</name>
    <tableRow>
        <sequence>1</sequence>
        <field>
            <sequence>0</sequence>
            <name>authority</name>
            <value>AD01</value>
        </field>
        <field>
            <sequence>1</sequence>
            <name>licenceNumber</name>
            <value></value>
        </field>
    </tableRow>
</table>

to be

<table>
    <name>businessLicenceGroup</name>
    <tableRow>
        <sequence>1</sequence>
        <field>
            <sequence>0</sequence>
            <name>authority</name>
            <value>AD01</value>
        </field>
    </tableRow>
</table>
Neil
  • 227
  • 2
  • 8

2 Answers2

1

The reference code which looks for an empty objects and removes their parent nodes would be something like:

def before = prev.getResponseDataAsString()
log.info('Before: ' + before)
def xml = new groovy.util.XmlParser().parseText(before)
def nodesWithoutText = xml.'**'.findAll { it.name() && !it.text() }
def removeNode = { node ->
    def field = node.parent()
    def tableRow = field.parent()
    tableRow.remove(field)
}
nodesWithoutText.each{removeNode(it)}
log.info('After: ' + groovy.xml.XmlUtil.serialize(xml))

The above code assumes that your example XML comes as the response of a Sampler and needs to be put into JSR223 PostProcessor

Demo:

enter image description here

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
-1

Maybe you can use this groovy script for JSR223 PreProcessor. It's based on following articles. Regard. that the result has not the same format like the input xml.

Modifying / Removing nodes with Groovy

Groovy Goodness: Pretty Print XML

I found this after posting the answer. It might also help.

Groovy XmlSlurper replace node with given child node value

import groovy.xml.*

def xml = """
<table>    
    <name>businessLicenceGroup</name>   
    <tableRow>        
        <sequence>1</sequence>        
        <field>            
            <sequence>0</sequence>            
            <name>authority</name>            
            <value>AD01</value>        
        </field>        
        <field>            
            <sequence>1</sequence>            
            <name>licenceNumber</name>            
            <value></value>        
        </field>    
    </tableRow>
</table>
"""

log.info("XML before removing Node: " + xml);

def table = new XmlParser().parseText(xml);

log.info("Parsed XML before removing Node: " + table);

log.info("Node to remove: " + table.tableRow.field[1]);
// remove the node by replacement with empty element
table.tableRow.field[1].replaceNode {};

// Create Output
log.info("Parsed XML after removing Node: " + table);
def xmlOutput = new StringWriter()
def xmlNodePrinter = new XmlNodePrinter(new PrintWriter(xmlOutput))
xmlNodePrinter.print(table)

log.info("Parsed XML after removing Node: " + xmlOutput.toString());
Johann
  • 349
  • 2
  • 9