在jMeter中从SOAP请求中删除空组

时间:2019-02-06 06:19:46

标签: soap jmeter

我需要预处理在jMeter中使用CSV数据集填充的SOAP请求(使用HTTP请求)。 某些绑定变量为null,这是预期的,但是我正在测试的服务也对这些元素执行验证。

过去也有类似的问题(JMeter - Remove empty elements in SOAP request),但就我而言,我并不是只想清除要清除整个块的空白元素。

示例: 我想要这个

<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>

成为

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

2 个答案:

答案 0 :(得分:1)

查找空对象并删除其父节点的参考代码如下:

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))

上面的代码假定您的示例XML来自Sampler的响应,需要放入JSR223 PostProcessor

演示:

enter image description here

参考文献:

答案 1 :(得分:-1)

也许您可以将此普通脚本用于JSR223 PreProcessor。它基于以下文章。看待。结果与输入xml格式不同。

Modifying / Removing nodes with Groovy

Groovy Goodness: Pretty Print XML

我在发布答案后发现了这一点。这也可能有帮助。

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());