问题
Setting below sample result in jar file
mySampleResult.setResponseData("ReturnCode" + returnCode + "EndReturnCode" ,null);
copy the jar file in lib/ext
of JMeter
.
Create a project where Regular expression extractor
is attached to java request which calls runtest in jar file
Response string is below
ReturnCodeThu Feb 16 08:01:56 GMT 2017,Thu Feb 16 09:09:27 GMT 2017,0:1:7:31,98.74105EndReturnCode
Regular Expression Extractor
Reference Name: returnValue
Regular Expression: ReturnCode(.*?)EndReturnCode
Beanshell Code
${returnValue}
Error in Beanshell
Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: `` try { Thu Feb 16 08:01:56 GMT 2017,Thu Feb 16 09:09:27 GMT 2017,0:1:7:31,9 . . . '' Encountered "16" at line 4, column 13.
Please let me know that why I am getting this error.
回答1:
Your ${returnValue}
statement is syntaxically incorrect, you need at least to surround it with quotation marks like "${returnValue}"
so Beanshell interpreter would treat it as a Java String
Going forward, just putting this statement inside the Beanshell Sampler doesn't make any sense, you should add some further processing, i.e:
return "${returnValue}";
- will set Beanshell Sampler result to this valueSampleResult.setResponseData("${returnValue}".getBytes());
- the same as above but using SampleResult shorthandprint("${returnValue}");
- output the value to STDOUTlog.info("${returnValue}");
- output the value to jmeter.log file
See How to Use BeanShell: JMeter's Favorite Built-in Component for more information on using Beanshell in JMeter scripts
回答2:
If you want to just print the value to JMeter log file, use log.info
method and using vars.get
.
BeanShell Code:
log.info("returnValue " + vars.get("returnValue"));
回答3:
Way access to JMeter variable from Beanshell is different from what you've been supposed.
You're going to use "vars" built-in Beanshell object (or variable, if you like).
whatsActuallyReturned = vars.get("returnValue");
vars.put("returnValue", "whatever I want to put instead");
来源:https://stackoverflow.com/questions/42275610/error-in-beanshell-sampler-jmeter-for-parsing-regularexpressionextractor