英文:
For Loop not working as expected in Beanshell scripting Jmeter
问题
以下是我的代码,我正在尝试循环遍历一组状态,并在状态等于N时中断,并在同一索引处保存EpisodeId。 (使用Javascript在beanshell postprocessor中)
var i;
var count = vars.get("AuthStatus_matchNr");
var EpisodeID;
log.info("Status的数量:" + count)
for (i = 1; i <= count; i++) {
var AuthStatus_i = vars.get("AuthStatus_" + i);
log.info("Auth状态:" + AuthStatus_i);
if (AuthStatus_i.equals("N")) {
EpisodeID = vars.get("corr_EpisodeID_" + i);
break;
}
}
log.info("EpisodeID:" + EpisodeID);
vars.put("EpisodeID", EpisodeID);
但我的循环没有按预期工作。循环运行了2次迭代,并在找到N状态之前中断。以下是日志响应:
捕获 'N' 状态的 EpisodeID: Status的数量:3
捕获 'N' 状态的 EpisodeID: Auth状态:null
捕获 'N' 状态的 EpisodeID: Auth状态:A
捕获 'N' 状态的 EpisodeID: EpisodeID:undefined
英文:
Below is my code, where I am trying to loop through set of Status's and break if Status is equal N. And Save EpisodeId at same index. (using Javascript in beanshell postprocessor)
var i;
var count = vars.get("AuthStatus_matchNr");
var EpisodeID;
log.info("Count of Status:"+count)
for(i=0;i<=count;i++){
var AuthStatus_i;
AuthStatus_i = vars.get("AuthStatus_"+i);
log.info("Auth:"+AuthStatus_i);
if (AuthStatus_i == "N"){
EpisodeID = vars.get("corr_EpisodeID_"+i);
break;
}
else{
i++;
}
}
log.info("EpisodeID:"+EpisodeID)
vars.put("EpisodeID",EpisodeID);
But my loop is not working as expected. Loop runs for 2 iteration and breaks before finding N status. Below is the Log response
Capture 'N' Status EpisodeID: Count of Status:3
Capture 'N' Status EpisodeID: Auth:null
Capture 'N' Status EpisodeID: Auth:A
Capture 'N' Status EpisodeID: EpisodeID:undefined
答案1
得分: 1
你需要移除这部分代码:
else {
i++;
}
因为你已经在for
循环中进行了增量操作。
另外要注意,根据JMeter最佳实践,你应该使用JSR223测试元素和Groovy语言进行脚本编写,所以考虑迁移。
更多信息:Apache Groovy: Groovy 用于什么?
英文:
You need to remove this bit:
else {
i++;
}
because you're already doing the increment in for loop.
Also be aware that according to JMeter Best Practices you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating.
More information: Apache Groovy: What Is Groovy Used For?
答案2
得分: 0
我明白问题出在哪里。在JavaScript中,数组和列表是从零开始索引的,这意味着索引从0开始,最后一个索引是count - 1。因此,您需要修改条件i <= count为i < count,以便遍历AuthStatus数组中的所有元素。
因此,正确的方法是这样的:
var i;
var count = vars.get("AuthStatus_matchNr");
var EpisodeID;
log.info("Status的数量: " + count);
for (i = 0; i < count; i++) {
var AuthStatus_i = vars.get("AuthStatus_" + i);
log.info("Auth: " + AuthStatus_i);
if (AuthStatus_i === "N") {
EpisodeID = vars.get("corr_EpisodeID_" + i);
break;
}
}
log.info("EpisodeID: " + EpisodeID);
vars.put("EpisodeID", EpisodeID);
现在基本上循环将遍历所有元素,直到找到"N"状态并分配相应的EpisodeID。
英文:
I see where the issue is. In JavaScript, arrays and lists are zero-based, which means the index starts from 0, and the last index is count - 1. Therefore, you need to modify the condition i <= count to i < count in order to iterate over all the elements in the AuthStatus array.
So correct way to do it would be like this:
var i;
var count = vars.get("AuthStatus_matchNr");
var EpisodeID;
log.info("Count of Status: " + count);
for (i = 0; i < count; i++) {
var AuthStatus_i = vars.get("AuthStatus_" + i);
log.info("Auth: " + AuthStatus_i);
if (AuthStatus_i === "N") {
EpisodeID = vars.get("corr_EpisodeID_" + i);
break;
}
}
log.info("EpisodeID: " + EpisodeID);
vars.put("EpisodeID", EpisodeID);
Now basically the loop will iterate through all the elements until it finds the "N" status and assigns the corresponding EpisodeID.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论