我必须断言API的JSON响应。因此,使用JSON路径提取器提取字段(状态)的值并将其保存在变量(Optinurl)中
"state":"opted_in“
在调试采样器中,我看到Optinurl的值为
Optinurl=:"opted_in“
Optinurl_1=opted_in
Optinurl_matchNr=1
当我尝试在Beanshell断言中检索变量Optinurl的值时,如下所示:
String optinValue = ${Optinurl}
我得到了
ERROR - jmeter.util.BeanShellInterpreter:调用bsh方法时出错: eval源文件:内联求值:String optinValue = '["opted_in"]';'' Token Parsing Error: Lexical error at line 1, column 23. Encountered: "\"" (34), after : "\'[" 2016/03/07 14:40:15 WARN - jmeter.assertions.BeanShellAssertion: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of:字符串optinValue = '"opted_in"';'‘标记解析错误:第1行,第23列出现词法错误。遇到:"\"“(34),在:"\'[”
提前感谢您的帮助!
发布于 2016-03-08 13:44:18
- `String optinValue = "${Optinurl}";`或
- String optinValue = vars.get("Optinurl");
Optinurl变量初始化无关。正在调查第1行第23列出现词法错误。
看起来你在第一个脚本行有一些语法错误。因此,有以下选项:
- Double check your code, make sure that parentheses, quotation marks, etc. are matching, statements are ending with semicolon, quotation marks in strings are escaped, etc.
- Adding `debug();` line as the first line of your script produces comprehensive debug output to STDOUT
- Surrounding your code into [try/catch block](https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html) allows to having more informative error stracktraces
有关在JMeter测试中使用How to Use BeanShell: JMeter's Favorite Built-in Component的更多详细信息,请参阅Beanshell指南。
发布于 2016-03-08 13:08:07
我认为您希望将[ : "opted_in" ]存储到一个字符串变量中,因此请使用以下代码:
String optionValue= vars.get("Optinurl");
如果您只希望将opted_in存储到一个变量中,那么可以使用
String optionValue= vars.get("Optinurl_1");
发布于 2016-03-09 00:18:33
感谢Dmitri,Kaushlendra的回复。
我更新了我的脚本,如下所示,它在GUI/命令行中工作正常。由于vars.get("Optinurl")返回"opted_in",所以在比较字符串之前必须删除引号和方括号。
String optinValue = vars.get("Optinurl"). replace("[","").replace("]","").replace("\"","");
String expectedState = "${EXPECTED_STATE}";
log.info(optinValue);
log.info(expectedState);
if(!optinValue.equals(expectedState)){
Failure = true;
FailureMessage = "Values of state field for Campaign id " + "${CAMPAIGN_ID}" + " dont match "; }
我不能使用字符串optinValue = vars.get("Optinurl_1"),因为当我从命令行运行测试时它会失败(虽然在图形用户界面模式下工作得很好)
https://stackoverflow.com/questions/35852957
复制相似问题