在beanshell脚本中访问yaml中的Jmeter变量(variable_MarchNr)时出错

bttbmeg0  于 2022-11-09  发布在  Shell
关注(0)|答案(1)|浏览(155)

我正在尝试批量编辑一个名为issues的实体,我调用GetIssues API并通过json提取器获取变量**“issueIds”**中的所有issueId
json extractor to extract issueIds
现在我想在其他API批量编辑问题中传递这些ID,如果我在下一个API中直接使用这些数组,我会得到以下错误:{“details”:“JSON中位置19处出现意外标记$”,“metadata”:{}}
所以我习惯在Beanshell后处理器中使用下面的代码:

var mylist;
props.put("mylist", new ArrayList());
log.info("Detected " + vars.get("issueIds_matchNr") + " issueIds");
for (int i=1; i<= Integer.parseInt(vars.get("issueIds_matchNr")); i++) {
    log.info("IDs # " + i + ": " + vars.get("issueIds_" + i));
    props.get("mylist").add('"'  +  vars.get("issueIds_" + i)  + '"' );
}
log.info(props.get("mylist").toString());
var issueIdList;
vars.put("issueIdList", props.get("mylist").toString());
log.info(vars.get("issueIdList"));

在我的下一个api调用中,如果我传递issueIdList变量,那么这在jmeter中工作得很好。
调试采样器中采样变量值如下所示:

issueIdList=["555bcfc2", "33974d2c", "e58db1d6"]
issueIds_1=555bcfc2
issueIds_2=33974d2c
issueIds_3=e58db1d6
issueIds_matchNr=3

如果我转换我的jmx 2 yaml并尝试使用bzt issues.yml运行此文件,我面临的问题
然后在执行上述shell脚本时,未检测到这些issueIds_matchNr、issueIds_3,我得到以下错误;

2022-05-29 08:26:10,785 INFO o.a.j.e.J.JSR223PostProcessor: Detected null issueIds
2022-05-29 08:26:10,795 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223PostProcessor
javax.script.ScriptException: Sourced file: eval stream : Method Invocation Integer.parseInt : at Line: 4 : in file: eval stream : Integer .parseInt ( vars .get ( "issueIds_matchNr" ) ) 
Target exception: java.lang.NumberFormatException: null
 in eval stream at line number 4
    at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:87) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]

我的Yaml脚本是:

- extract-jsonpath:
        issueIds:
          default: NotFound
          jsonpath: $..id
      follow-redirects: true
      jsr223:
      - compile-cache: true
        execute: after
        language: beanshell
        parameters: issueIds
        script-file: script.bsh
      label: Get Issue Id's
      method: GET
      url: https://${BASE_URL1}/${PROJECT_ID}/issues?limit=5&sortBy=-displayId&filter%5Bvalid%5D=true
ykejflvf

ykejflvf1#

你忽略了一个重要的细节:在Taurus YAML中设置匹配编号
JSON提取器的正确定义如下:

extract-jsonpath: 
 issueIds:
   jsonpath: $..id
   match-no: -1 #this is what you need to add

还要注意starting from JMeter 3.1 it's recommended to use Groovy as the scripting language,所以考虑迁移,它将像从脚本中删除第一行一样简单。bsh

相关问题