从jmeter请求主体获取动态值,然后将其关联回去

时间:2019-02-18 12:23:10

标签: groovy jmeter

正在Jmeter上为入站呼叫创建加载脚本。当应用程序收到调用Jmeter时,将其捕获为POST请求正文:

5|0|21|https://d50.eng.domain.com:1050/newgenmashup/v1/mashup/|1D4E8B05B21570719C10066C4E15360C|com.contactual.client.SFService|triggerEvent|java.lang.String|com.contactual.client.model.GenericXmlData|java.util.TreeMap|tenant~~pagent2|com.contactual.client.model.GenericXmlData/2918165779||353-726-6515|no|offered|PHONE|in|563|PavanInboundQueue|1|465|<UserData>
<param name = "@pri"><![CDATA[100]]></param>
<param name="callingName"><![CDATA[ 1234567366]]></param>
<param name="cha"><![CDATA[3537266515]]></param>
<param name="channelName"><![CDATA[353-726-6515]]></param>
<param name="cnt"><![CDATA[0]]></param>
<param name="con"><![CDATA[0]]></param>
<param name="med"><![CDATA[T]]></param>
<param name="otim"><![CDATA[1550070210]]></param>
<param name="pho"><![CDATA[ 1234567366]]></param>
<param name="phoneNum"><![CDATA[ 1234567366]]></param>
<param name="priority"><![CDATA[50]]></param>
<param name="que"><![CDATA[vcclondon~~queue~~phone~~563]]></param>
<param name="queueDirection"><![CDATA[in]]></param>
<param name="tenantName"><![CDATA[vcclondon]]></param>
<param name="tenantRecServer"><![CDATA[d50]]></param>
<param name="tenantSkillName"><![CDATA[PavanInboundQueue]]></param>
<param name="tim"><![CDATA[1550070210]]></param>
<param name="tok"><![CDATA[465]]></param>
<param name="queuedTime"><![CDATA[0]]></param>
<param name="ani"><![CDATA[ 1234567366]]></param>
<param name="interaction_guid"><![CDATA[int-168e76257e3-r5tEbRqwNR7hBd7jmuvwnsxG9-phone-00-vcclondon]]></param>
<param name="transaction_id"><![CDATA[465]]></param>
<param name="notes"><![CDATA[]]></param>
<param name="callbackTimeMs"><![CDATA[]]></param>
<param name="ctl_userdata"><![CDATA[[doNotCallSelected\!false][donotcallSelected\!false]]]></param>
</UserData>|java.util.TreeMap/1575826026|1|2|3|4|3|5|6|7|8|9|0|0|0|0|10|0|0|0|10|0|11|0|12|0|13|10|0|10|1|0|0|0|14|15|16|17|18|10|10|0|19|20|21|0|0|

我想检索每个参数值并进行关联。将上方的身体数据存储到字符串中。

String requestBody = sampler.getArguments();

使用xmlslurper导航到树图。示例:我正在尝试使用以下代码段提取互动ID。

def UserData = new XmlSlurper().parseText(requestBody)
def interaction_guid = UserData.param[20].@name

预期:我想关联所有值。因为,没有值是用户输入数据。

实际是:

2019-02-18 12:14:33,111 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor
javax.script.ScriptException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.15.jar:2.4.15]
at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.15.jar:2.4.15]
at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_151]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:221) ~[ApacheJMeter_core.jar:5.0 r1840935]
at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:5.0 r1840935]
at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:935) [ApacheJMeter_core.jar:5.0 r1840935]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:537) [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_151]
Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:?]
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) ~[xercesImpl-2.11.0.jar:?]
at groovy.util.XmlSlurper.parse(XmlSlurper.java:205) ~[groovy-all-2.4.15.jar:2.4.15]
at groovy.util.XmlSlurper.parse(XmlSlurper.java:258) ~[groovy-all-2.4.15.jar:2.4.15]
at groovy.util.XmlSlurper.parseText(XmlSlurper.java:284) ~[groovy-all-2.4.15.jar:2.4.15]
at groovy.util.XmlSlurper$parseText.call(Unknown Source) ~[?:?]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) ~[groovy-all-2.4.15.jar:2.4.15]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) ~[groovy-all-2.4.15.jar:2.4.15]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128) ~[groovy-all-2.4.15.jar:2.4.15]
at Script26.run(Script26.groovy:17) ~[?:?]
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321) ~[groovy-all-2.4.15.jar:2.4.15]
... 9 more

能帮我提取每个参数,关联并在同一POST请求中放回我吗?

1 个答案:

答案 0 :(得分:0)

  1. 您获取请求有效负载的方式是错误的,您应该修改此行:

    String requestBody = sampler.getArguments();
    

    对此

    String requestBody  = sampler.getArguments().getArgument(0).getValue();
    

    由于Sampler.getArguments() function返回Arguments类实例,因此您需要第一个实例的值。

  2. 您的请求主体似乎不是有效的XML,因此您将无法使用XmlSlurper,请考虑使用Find Operator instead

    def interaction_guid= (requestBody =~ "<param name=\"interaction_guid\"><!\\[CDATA\\[(.+?)\\]\\]></param>")[0][1] as String
    

    演示:

    enter image description here

    查看Apache Groovy - Why and How You Should Use It文章以了解有关JMeter中Groovy脚本的更多信息