利用JMeter,我怎样才能从API的响应的主体提取字符串,并将其保存到CSV文件?

问题描述 投票:1回答:2

我正在从API一些不同的身体反应,看起来是这样的:

“EyJhbRciOiRRRzI1RiIsInR5cCI6IkpXVCJ9.eyJ1c2RybmRtZSI6IlRlcmhhdCIsInR1aWQiOiJRZXJoYXQiLRJyb2xlRjoibW9iaWxlRRRwcCIsImVtYWlsIjoidGVzdEB0ZXN0LmNvbSIsImlkIjoiU2RyaGF0IiwiaWR0IjoxRRR5MDU5OTUxLCJleRRiOjE1NR​​kwNzQzRRR9.RRzm3VvioZ_iR-v5EGSSfYJLf0d9aZ-9R-RP4UbER04”

我使用JMeter的正则表达式提取器,它是这样的:enter image description here

我如何可以打印在不同的测试,从正则表达式提取,以供日后使用.csv文件中的值?

我想在这里提出的解决方案,但它并没有帮助。也许这脚本需要更多的调整,但我不知道常规:JMeter extract all values from regular expression and store in a csv

我非常感谢你能提供任何帮助。

更新问题:从前面提到的问题的答案用确切的代码,我得到了波纹管的错误:

码:

def csv = new File("my.csv")
1.upto(vars.get("foo_matchNr") as int, {
    csv << vars.get("foo_$it") << System.getProperty("line.separator")  
})

错误:

    019-02-02 11:30:26,972 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2019-02-02 11:30:26,974 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2019-02-02 11:30:26,978 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2019-02-02 11:30:27,223 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
2019-02-02 11:30:27,223 INFO o.a.j.e.StandardJMeterEngine: Starting 3 threads for group Thread Group.
2019-02-02 11:30:27,223 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2019-02-02 11:30:27,224 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=3 ramp-up=1 perThread=333.33334 delayedStart=false
2019-02-02 11:30:27,226 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2019-02-02 11:30:27,225 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2019-02-02 11:30:27,226 INFO o.a.j.s.FileServer: Stored: D:/Software/apache-jmeter-5.0/bin/testUser.csv
2019-02-02 11:30:27,226 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2019-02-02 11:30:27,559 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-2
2019-02-02 11:30:27,892 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-3
2019-02-02 11:30:28,428 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'null' to class 'int'. Try 'java.lang.Integer' instead
    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_191]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:221) ~[ApacheJMeter_core.jar:5.0 r1840935]
    at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor.java:44) [ApacheJMeter_components.jar:5.0 r1840935]
    at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:925) [ApacheJMeter_core.jar:5.0 r1840935]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:564) [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_191]
Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'null' to class 'int'. Try 'java.lang.Integer' instead
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:176) ~[groovy-all-2.4.15.jar:2.4.15]
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.intUnbox(DefaultTypeTransformation.java:82) ~[groovy-all-2.4.15.jar:2.4.15]
    at Script5.run(Script5.groovy:2) ~[?:?]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321) ~[groovy-all-2.4.15.jar:2.4.15]
    ... 9 more
2019-02-02 11:30:28,436 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2019-02-02 11:30:28,439 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2019-02-02 11:30:28,583 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'null' to class 'int'. Try 'java.lang.Integer' instead
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.15.jar:2.4.15]

等等

加上2个错误这样。

我认为,“foo_matchNr”被看作是一个变量,只是“富”是我的变量,它改变了它的价值,每次或它的值的列表?我不知道这个正则表达式提取器的工作原理或Groovy对这一问题。

使用适合(使用Groovy代码我0级知识)从前面提到的问题的答案的代码中,我得到了波纹管的错误:

改变的代码:

def csv = new File("my.csv")
1.upto(vars.get("foo") as int, {
    csv << vars.get("foo_$it") << System.getProperty("line.separator")  
})

错误:

2019-02-02 11:08:27,613 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2019-02-02 11:08:27,614 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2019-02-02 11:08:27,635 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2019-02-02 11:08:27,850 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
2019-02-02 11:08:27,850 INFO o.a.j.e.StandardJMeterEngine: Starting 3 threads for group Thread Group.
2019-02-02 11:08:27,850 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2019-02-02 11:08:27,850 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=3 ramp-up=1 perThread=333.33334 delayedStart=false
2019-02-02 11:08:27,851 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2019-02-02 11:08:27,852 INFO o.a.j.s.FileServer: Stored: D:/Software/apache-jmeter-5.0/bin/testUser.csv
2019-02-02 11:08:27,853 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2019-02-02 11:08:27,853 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2019-02-02 11:08:28,187 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-2
2019-02-02 11:08:28,520 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-3
2019-02-02 11:08:28,794 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: java.lang.NumberFormatException: For input string: "eyJhbRciOiRRRzI1RiIsInR5cCI6IkpXVCJ9.eyJ1c2RybmRtZSI6IlRlcmhhdCIsInR1aWQiOiJRZXJoYXQiLRJyb2xlRjoibW9iaWxlRRRwcCIsImVtYWlsIjoidGVzdEB0ZXN0LmNvbSIsImlkIjoiU2RyaGF0IiwiaWR0IjoxRRR5MDU5OTUxLCJleRRiOjE1NRkwNzQzRRR9.RRzm3VvioZ_iR-v5EGSSfYJLf0d9aZ-9R-RP4UbER04"
    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_191]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:221) ~[ApacheJMeter_core.jar:5.0 r1840935]
    at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor.java:44) [ApacheJMeter_components.jar:5.0 r1840935]
    at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:925) [ApacheJMeter_core.jar:5.0 r1840935]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:564) [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_191]
Caused by: java.lang.NumberFormatException: For input string: "eyJhbRciOiRRRzI1RiIsInR5cCI6IkpXVCJ9.eyJ1c2RybmRtZSI6IlRlcmhhdCIsInR1aWQiOiJRZXJoYXQiLRJyb2xlRjoibW9iaWxlRRRwcCIsImVtYWlsIjoidGVzdEB0ZXN0LmNvbSIsImlkIjoiU2RyaGF0IiwiaWR0IjoxRRR5MDU5OTUxLCJleRRiOjE1NRkwNzQzRRR9.RRzm3VvioZ_iR-v5EGSSfYJLf0d9aZ-9R-RP4UbER04"
    at java.lang.NumberFormatException.forInputString(Unknown Source) ~[?:1.8.0_191]
    at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_191]
    at java.lang.Integer.valueOf(Unknown Source) ~[?:1.8.0_191]
    at org.codehaus.groovy.runtime.StringGroovyMethods.toInteger(StringGroovyMethods.java:3319) ~[groovy-all-2.4.15.jar:2.4.15]
    at org.codehaus.groovy.runtime.StringGroovyMethods.asType(StringGroovyMethods.java:178) ~[groovy-all-2.4.15.jar:2.4.15]
    at org.codehaus.groovy.runtime.dgm$1048.doMethodInvoke(Unknown Source) ~[groovy-all-2.4.15.jar:2.4.15]
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1225) ~[groovy-all-2.4.15.jar:2.4.15]
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034) ~[groovy-all-2.4.15.jar:2.4.15]
    at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:935) ~[groovy-all-2.4.15.jar:2.4.15]
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:926) ~[groovy-all-2.4.15.jar:2.4.15]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:181) ~[groovy-all-2.4.15.jar:2.4.15]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.asType(ScriptBytecodeAdapter.java:604) ~[groovy-all-2.4.15.jar:2.4.15]
    at Script4.run(Script4.groovy:2) ~[?:?]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321) ~[groovy-all-2.4.15.jar:2.4.15]
    ... 9 more
2019-02-02 11:08:28,797 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2019-02-02 11:08:28,797 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2019-02-02 11:08:29,160 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: java.lang.NumberFormatException: For input string: "

这一次“看见”琴弦我感兴趣的处理,但似乎遇到的一些问题处理它们。我得到这样,对于其他两个变量,我从其中包含不同的字符串,另2个反应(我正在3个请求并获得3个回答,每个包含不同的身体串)获得2个错误。就在值之后,“对于输入字符串”是不同的。

groovy jmeter performance-testing load-testing beanshell
2个回答
1
投票

如果你在你的反应只有一个匹配,使用此代码粘贴到JSR223 PostProcessor中:

def csv = new File("my.csv")
csv << vars.get("foo") << System.getProperty("line.separator")

0
投票
  1. 为了您的第一块“代码” def csv = new File("my.csv") 1.upto(vars.get("foo_matchNr") as int, { csv << vars.get("foo_$it") << System.getProperty("line.separator") }) 该错误是告诉${foo_matchNr}变量不存在,再次检查是否存在并且具有使用Debug SamplerView Results Tree听者组合的数值。
  2. 关于你的第二个代码段: def csv = new File("my.csv") 1.upto(vars.get("foo") as int, { csv << vars.get("foo_$it") << System.getProperty("line.separator") }) 错误是告诉它不能施放此行: eyJhbRciOiRRRzI1RiIsInR5cCI6IkpXVCJ9.eyJ1c2RybmRtZSI6IlRlcmhhdCIsInR1aWQiOiJRZXJoYXQiLRJyb2xlRjoibW9iaWxlRRRwcCIsImVtYWlsIjoidGVzdEB0ZXN0LmNvbSIsImlkIjoiU2RyaGF0IiwiaWR0IjoxRRR5MDU5OTUxLCJleRRiOjE1NRkwNzQzRRR9.RRzm3VvioZ_iR-v5EGSSfYJLf0d9aZ-9R-RP4UbER04 Integer。我的期望是,你需要将代码更改为类似: new File("my.csv") << vars.get("foo") << System.getProperty("line.separator")

如果上面的代码不会产生你在找什么 - 与上述调试样的输出更新您的问题,并表示你希望存储和如何哪个变量(一个或多个)。

退房The Groovy Templates Cheat Sheet for JMeter文章开始使用Groovy脚本。


一般来说,我会建议以产生JMeter的变量存储到.jtl结果文件中使用Sample Variables财产。如果你想输出去到一个单独的文件 - 考虑使用Flexible File Writer

© www.soinside.com 2019 - 2024. All rights reserved.