Parsing the contents of the response and passing the obtained value to the subsequent request, the common method is to add the post processor on the request that wants to parse response
This chapter describes two common components
- BeanShell postprocessor
- JSON Extractor
Add Post Processor: BeanShell postprocessor
Gets the string in the response and determines the content, when response contains "the wait operation timed out" or "Oops." Something went wrong ... sorry, the response of the request is considered incorrect
String response = prev.getresponsedataasstring ();
String code = Prev.getresponsecode ();
Log.info ("Respnse is" + response);
Log.info (code);
int result1 = Response.indexof ("The wait operation timed out");
int result2 = Response.indexof ("Oops. Something went wrong ... sorry ");
if (Code = = "$" && result1>=0 | | result2>=0) {
Failuremessage = "Ok,check current";
}
else{
Failure = true;
Failuremessage = "Error,check ERROR";
}
Parse the returned Jason data, get the value of the Name field assigned to the variable result
{"Body": {"apps": [{"Name": "111"},{"name": "222"}]}}
Import org.json.*;
The imported JSON package in the script needs to be downloaded to the network and put under \lib\ext
String response_data = prev.getresponsedataasstring ();
Jsonobject data_obj = new Jsonobject (response_data);
String apps_str = Data_obj.get ("Body"). Get ("apps"). ToString ();
Jsonarray Apps_array = new Jsonarray (APPS_STR);
String[] result = new string[apps_array.length ()];
for (int i=0;i<apps_array.length (); i++) {
Jsonobject app_obj = new Jsonobject (Apps_array.get (i). toString ());
String name = App_obj.get ("name"). ToString ();
Result[i] = name;
}
Vars.put ("Result", arrays.tostring (result));
Add Post Processor: JSON Extractor
Parse the returned Jason data, get the value of the Name field into the parameter to the request in the back
{"Body": {"apps": [{"Name": "111"},{"name": "222"}]}}
The value of the variable T1 is 111, and the value of the variable t2 is 222
Jmeter Getting started with Web performance testing (vi): Jmeter parsing response and passing value