2017-06-21 53 views
0

豆壳后处理器代码:如何在beanshell后处理器中设置Jmeter Loop Controller中的动态值?

int totalElements= Integer.parseInt(vars.get("totalElements")); 
vars.put("totalElements", String.valueOf(totalElements)); 

在回路控制器,我用以下这些,但不能得到这个值。

${__javaScript(parseInt(${totalElements})}; 
${__javaScript(parseInt("${totalElements}"))}; 
${__V(totalElements)}; 
${totalElements}; 

回答

0

尝试变量名没有分号:

enter image description here

的另一个问题是,如果由于某种原因变量totalElements没有定义,声明vars.get("totalElements")将返回null和解析Integer.parseInt(null)会导致一个例外,这将导致采样器的失败。如果这是一个预期的行为,罚款,但如果没有,你可以这样做:

String value = vars.get("totalElements"); 
int totalElements = (value != null) ? Integer.parseInt(value) : 0; 
vars.put("totalElements", String.valueOf(totalElements)); 

所以,如果变量不能被检索,totalElements设置为0,因此循环将无法运行。但采样器也不会失败。

0

我得到了这个问题的解决方案。曾在按照以下脚本:

int totalElements= Integer.parseInt(vars.get("totalElements")); 
vars.put("totalElements", String.valueOf(totalElements)); 

然后我在回路控制器使用${__javaScript("${totalElements}")}和工作正常。

相关问题