问题
My test sends a request and receives a json
response which has an array
in it. I want to iterate through each element of the array and send another request using the value in the array.
the overall structure looks like this
The response received for the first request is
{"result":"success","additional-info":"{\"supported-tags\":[{\"course\":\"something\",\"subject\":\"something2\",\"topic\":\"something3\"},
...,
...]}"}
Using JMESPath
, I first extract additional-info
and store the value in AdditionalInfoContainingGetSupportedTags
Then I extract the array using JMESPath
and store the value in SupportedTags
Then I use ForEach
controller iterating over SupportedTags
I extract value in outging request like this "tags":["existing/${TagObject.course}-${TagObject.subject}-${TagObject.topic}"],
But I don't think the Foreach
gets started as I don't see anything in the tree
The Debug Sampler
doesn't show anything as well (unless I am not using it correctly).
UPDATE - by increasing trace level of jmeter
in 'log4J2.xml' at root level (from INFO
to TRACE
), I found this issue 2020-12-09 17:13:48,507 DEBUG o.a.j.c.ForeachController: ForEachTagController No entries found - null first entry: SupportedTags_1
. Still working on how to resolve it.
UPDATE 2 - I have done some more plumbing though to be honest, it isn't neat.
ForEach
controller expects variables of type SupportedTags_1
, SupportedTags_2
etc. so I have created a beanshell
script which exports such variables.
import com.eclipsesource.json.*;
//prev.setSuccessful(false);
try {
String jsonString = prev.getResponseDataAsString(); //response as string
log.info("received json string: "+jsonString);
JsonObject responseAsJsonObject = JsonObject.readFrom(jsonString); //convert response string as json
log.info("converted to object: "+responseAsJsonObject);
String additionalInfoString = responseAsJsonObject.get("additional-info").asString(); //get additional info string from json object
log.info("additional info as string: "+additionalInfoString);
JsonObject additionalInfoJsonObject = JsonObject.readFrom(additionalInfoString); //convert additional info string to json
log.info("additional info as object: "+additionalInfoJsonObject);
JsonArray supportedTagsJsonObject = additionalInfoJsonObject.get("supported-tags").asArray();
log.info("supported tags as array: "+supportedTagsJsonObject);
log.info("array length "+supportedTagsJsonObject.size());
for (int i = 0; i < supportedTagsJsonObject.size(); i++) {
log.info("putting value "+"SupportedTags_"+(i+1)+"="+ supportedTagsJsonObject.get(i));
vars.putObject("SupportedTags_"+(i+1), supportedTagsJsonObject.get(i));
}
// prev.setSuccessful(true);
} catch (Exception e){
log.info("error in processing beanshell script: ", e);
prev.setSuccessful(false);
}
I am now able to get the loop running but the issue is that I can't extract the values in subsequent requests because I am exporting a json object from the beanshell
script but I can't access it like this in the request - "tags":["existing/${TagObject.course}"]
. This gets printed literally.
- What am I doing wrong?
- Is there a way to print the value of extracted
JMESPath
variables?
回答1:
Maybe not the best approach but I solved the problem through Beanshell
script. I extract the object in the script, convert it in the right format and export the right format
import com.eclipsesource.json.*;
//prev.setSuccessful(false);
try {
String jsonString = prev.getResponseDataAsString(); //response as string
log.info("received json string: "+jsonString);
JsonObject responseAsJsonObject = JsonObject.readFrom(jsonString); //convert response string as json
log.info("converted to object: "+responseAsJsonObject);
String additionalInfoString = responseAsJsonObject.get("additional-info").asString(); //get additional info string from json object
log.info("additional info as string: "+additionalInfoString);
JsonObject additionalInfoJsonObject = JsonObject.readFrom(additionalInfoString); //convert additional info string to json
log.info("additional info as object: "+additionalInfoJsonObject);
JsonArray supportedTagsJsonObject = additionalInfoJsonObject.get("supported-tags").asArray();
log.info("supported tags as array: "+supportedTagsJsonObject);
log.info("array length "+supportedTagsJsonObject.size());
for (int i = 0; i < supportedTagsJsonObject.size(); i++) {
log.info("putting value SupportedTags_"+(i+1)+"="+ supportedTagsJsonObject.get(i));
JsonObject convertedToTag = supportedTagsJsonObject.get(i).asObject();
log.info("object is "+convertedToTag);
// log.info("will put tag "+convertedToTag.getString("course","no course")+", "+convertedToTag.getString("subject","no subject")+", "+convertedToTag.getString("topic","no topic");
String tag = convertedToTag.getString("course","no course")+"-"+convertedToTag.getString("subject","no subject")+"-"+convertedToTag.getString("topic","no topic");
String courseCheck = convertedToTag.getString("course","no course");
log.info("course is "+courseCheck);
if(courseCheck.equals("coding")) {
log.info("will put tag "+tag);
vars.putObject("SupportedTags_"+(i+1), tag);
} else if (courseCheck.equals("no course")){
log.info("error as course object doesn't exist in the json");
throw Exception("error as course object doesn't exist in the json");
}
else {
log.info("not putting tag as it doesn't start with coding"+tag);
}
}
// prev.setSuccessful(true);
} catch (Exception e){
log.info("error in processing beanshell script: ", e);
prev.setSuccessful(false);
}
USe in the request like this "tags":["existing/${TagObject}"],
来源:https://stackoverflow.com/questions/65216384/foreach-controller-not-getting-triggered-in-jmeter