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
2
Answers
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:
Now basically the loop will iterate through all the elements until it finds the "N" status and assigns the corresponding EpisodeID.
You need to remove this bit:
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?