JMeter定制肥皂采样器以使用变量

时间:2019-02-25 14:26:02

标签: soap groovy jmeter

我在JMeter中使用自定义SOAP采样器来发送SOAP请求。

<soapenv:Envelope>
   <soapenv:Header/>
   <soapenv:Body>
      <ser:${myvar}Upload>
...
      </ser:${myvar}Upload>
   </soapenv:Body>
</soapenv:Envelope>

我在执行过程中收到以下错误消息:

Caught exception while updating attachments
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source: 
    at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(Unknown Source) ~[?:1.8.0_201]
    at com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl.createEnvelopeFromSource(Unknown Source) ~[?:1.8.0_201]
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(Unknown Source) ~[?:1.8.0_201]
    at com.jmeter.protocol.soap.sampler.CustomSOAPSampler.updateAttachmentReferences(CustomSOAPSampler.java:431) [CustomSoapSampler-1.3.jar:?]
    at com.jmeter.protocol.soap.sampler.CustomSOAPSampler.sample(CustomSOAPSampler.java:259) [CustomSoapSampler-1.3.jar:?]
    at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:622) [ApacheJMeter_core.jar:5.0 r1840935]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:546) [ApacheJMeter_core.jar:5.0 r1840935]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.0 r1840935]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.0 r1840935]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]
Caused by: net.sf.saxon.trans.XPathException: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 12; Element type "ser:" must be followed by either attribute specifications, ">" or "/>".
    at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:447) ~[Saxon-HE-9.8.0-12.jar:?]
    at net.sf.saxon.event.Sender.send(Sender.java:138) ~[Saxon-HE-9.8.0-12.jar:?]
    at net.sf.saxon.jaxp.IdentityTransformer.transform(IdentityTransformer.java:383) ~[Saxon-HE-9.8.0-12.jar:?]
    at com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer.transform(Unknown Source) ~[?:1.8.0_201]
    ... 10 more
Caused by: org.xml.sax.SAXParseException: Element type "ser:" must be followed by either attribute specifications, ">" or "/>".

1 个答案:

答案 0 :(得分:1)

添加JSR223预处理器并使用MarkupBuilder in Groovy

对于您的xml,脚本将具有以下形式:

import groovy.xml.*

def builder = new StreamingMarkupBuilder()
builder.encoding = 'UTF-8'
def yourCustomElement = vars.get("myvar") + "Upload" // myvar is a saved variable in previous step

def Envelope = builder.bind {
    mkp.xmlDeclaration()
    namespaces << [soapenv:'http://schemas.xmlsoap.org/soap/envelope/']  // Or mkp.declareNamespace('soapenv':'http://schemas.xmlsoap.org/soap/envelope/')
    soapenv.Envelope() {
        soapenv.Header(){}
        soapenv."${yourCustomElement}"() {
        }
    }
}

vars.put("myXML", XmlUtil.serialize(Envelope));
log.info("\n" + vars.get("myXML"))

输出:

2019/02/25 17:13:51 INFO  - jmeter.modifiers.JSR223PreProcessor: 
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:TestUpload/>
</soapenv:Envelope>