In API interface test, it is very important to check the interface request respone.
When using JMeter for interface testing, there are a variety of respone checks, such as response assertions, BeanShell assertions, and so on, BeanShell assertions can be customized assertions, free and flexible to implement assertions with scripts
What is BeanShell
Small embedded Java source code interpreter with object scripting language features to dynamically execute standard Java syntax
Run its internal scripts to handle Java applications, and you can execute Java code dynamically while running your Java application, because BeanShell is written in Java and runs on the same virtual machine application, so you can freely refer to the object script and return the result
First look at this respone.
{
"message": "Cannot send a scheduled task less than the current point in time", "
statusCode":
Now you need to verify that the value of StatusCode is 200
Import org.json.*;
Import Java.util.Arrays;
Gets the return of the last request
String jsonstring = prev.getresponsedataasstring ();
Jsonobject Responsejson = new Jsonobject (jsonstring);
Determine if the return value is consistent with the expected
if (Responsejson.getint ("StatusCode")! =) {
//Assert the assertion failure to true, i.e. the use case fails and is displayed in the result tree failuremessage
Failure = true;
Failuremessage = "The return value of the statuscode is incorrect";
}
What to do if you want to verify that the value of message in Respone is consistent with expectations.
Import org.json.*;
Import Java.util.Arrays;
Gets the return of the last request
String jsonstring = prev.getresponsedataasstring ();
Jsonobject Responsejson = new Jsonobject (jsonstring);
String fbpcontent = responsejson.getstring ("message");
if (!fbpcontent.equals ("Cannot send a timed task less than the current point in Time")) {
//Assert the assertion failure to true, i.e. the use case fails and displays failuremessage
Failure = true in the result tree;
Failuremessage = "message is inconsistent with the actual value";
}
Existing respone data in this format
{
"StatusCode": $,
"data": [
{
"I": "50356",
"n": "Project One",
"V": "2.0", "
iconUrl": "",
},
{
"I": "45280",
"n": "Project Two", "
V": "3.0",
"ICONURL": "",
},
{
"I": " 14656 ",
" n ":" Project Three ",
" V ":" 2.6 ",
" ICONURL ":" ",
},
{
" I ":" 66213 ",
" n ":" Project Four ",
"V": "5.0",
"ICONURL": "",
}
]
}
Need to parse the value of the array data, how to parse it.
Import org.json.*;
Import Java.util.Arrays;
Gets the return of the last request
String jsoncontent = prev.getresponsedataasstring ();
Jsonobject response = new Jsonobject (jsoncontent);
Jsonarray groups = Response.getjsonarray ("data");
String strdata= groups.tostring ();
Log.info (strdata)
Now there are more complex formats of respone data
{"
prioritygroups": {
"proid": 1234,
"name": "Project One",
"groups": [
{
"id": "50356",
"items ": [
{
" proid ": 1360,
" n ":" PC-side ","
Index ": 1
},
{
" proid ": 1361,
" n ":" iOS Side ",
"index": 2
},
{
"proid": 1362,
"n": "Android",
"index": 4
}
]
}
]
},
"promotion": {
"Proid": 1364,
"cusid": +,
"name": "Project Two",
"from": 1470821215,
"to": 1470907615,
"status": 1,
"objectId": 1069,
"CreateBy": 394,
"eff": 1470821215000,
"createtime": 1470821155000
}
}
If you need to parse the data in groups, how do you do it?
Import Org.json.JSONArray;
Import org.json.JSONException;
Import Org.json.JSONObject;
String jsoncontent = prev.getresponsedataasstring ();
Jsonobject response = new Jsonobject (jsoncontent);
Jsonarray groups = Response.getjsonobject ("Prioritygroups"). Getjsonarray ("groups");
BeanShell for data parsing, you can also use the methods provided by Gson
Prev.getresponsedataasstring is a method provided by JMeter that can tune the response string of the last request
response = prev.getresponsedataasstring ();
Use the method provided by Gson to parse the JSON
jsonparser parser = new Jsonparser ();
Jsonobject responseobj = (jsonobject) parser.parse (response);