Home > other >  Before and after the jmeter beanshell commonly used built-in functions
Before and after the jmeter beanshell commonly used built-in functions

Time:01-07

1. BeanShell PreProcessor pre processor unique function: (running) before send the request
(1). Access to the request of the participation in the body content
Guide package import org, apache jmeter. Config. The Arguments;//jmeter4.0 and above their own, to guide package a
The Arguments argss=sampler. GetArguments ();
String argss_str=argss. GetArgument (0). The getValue ();

(2). Access to the request of the API path
String path=sampler. GetPath ();

2. BeanShell PostProcessor post processor unique function: (running)
after returns a response(1). Access to the requested response content
String response=prev. GetResponseDataAsString ();

3. The json string type into a json object type
(1). Ali fastjson - 1.2.70 jar package
Guide package import com. Alibaba. Fastjson. *;//download fastjson - 1.2.70 jars, in the lib directory and restart jmeter available
JSONObject JsonObj=JSON. ParseObject (JsonStr)

(2). Org. Json_1. Version 0.0 v jar package
Guide package import org. Json. *;
JSONObject JsonObj=new JSONObject (JsonStr)

4. Get a json object in the object, array, field
Eg: JsonObj: {
Keyinfo: {
"Documentno" : "PYJK20200106123456789
"},
Dealinfo: [{
"Businesscode" : "N99
"},
{
"Businesscode" : "N98
"}]
}
Alibaba package, for example
JSONObject keyinfo=JsonObj. GetJSONObject (" keyinfo ");//get a json object objects in the
String documentno.=JsonObj getJSONObject (" keyinfo "). Get String (" documentno ");//retrieve field values in the json object
JSONArray dealinfo=JsonObj. GetJSONArray (" dealinfo ");//get a json object in the array
JSONObject keyinfo=JsonObj. GetJSONArray (" dealinfo "). GetJSONObject (0);//get a json array of objects, index said which one object
String businesscode=JsonObj. GetJSONArray (" dealinfo "). GetJSONObject (0). Get String (" businesscode ");//access to an array of objects in the field values

5. Set the variable, but only to a string type value (variable stored in jmeter memory)
1. Set up local variables () on a single thread group
Vars. The put (" a ", "123");//set the variable a to 123
Vars. The put (" documentno documentno);//sets the variable documentno to local variable
Vars. The put (" keyinfo, "keyinfo. ToString ());//sets the object keyinfo converted to a string to a local variable
(2) to obtain a local variable (in the same thread group can get)
String a=vars. The get (" a ");//a="123"
String documentno=vars. The get (" documentno ");//documentno="PYJK20200106123456789"
JSONObject keyinfo=JSON. ParseObject (vars. The get (" keyinfo "));//get the keyinfo string into object

(3). Set the global variable (ACTS on the test plan of all the thread group)
Props. The put ();//usage and local variable function vars. The put (consistent)
(3). Access to global variables (under the test plan are available to all of the thread group)
String a=props. Get (" a ");//usage and local variable function vars. Get consistent ()

6. To form a large object variables VO
1. The new object
JSONObject keyinfo=new JSONObject ();
(2). Add the key to the object value
-Keyinfo. Put (" documentno PYJK20200106123456789 ", "");
(3). The new array
JSONArray dealinfo=new JSONArray ();
(4). To increase the object in the array, field (order, index 0,,)
Dealinfo. Add (dealinfo);
Dealinfo. Add (dealinfo);
Dealinfo. Add (" aa ");//add field
5. Add object to the object, an array
JSONObject VO=new JSONObject ();
VO. Put (" keyinfo, "keyinfo);
VO. Put (" dealinfo dealinfo);

7. To sort ASSIC yards JSONObject (), to the JSONObject key - the value of the put () object, automatic sorting by ASSIC yards, not to sort order code, to a fixed order, but in the new object, use the new JSONObject (new LinkedHashMap ()), the specific reason here don't do this, if you have any friends would like to know can search the difference between the map and LinkedHashMap,

8. Compare two objects are equal
Alibaba jars, for example
The import com. Alibaba. Fastjson. *;

Public isObjValEqual (a_obj b_obj) {
The Object a_key=a_obj. KeySet ();//get a key object of all the key, set of array
The Object b_key=b_obj. KeySet ();//get all the keys in the object b key, set of array
The Object key_list=maxArray (a_key b_key);//to get the length of the large array collection
For (the Object key: key_list) {//traverse each key key
String a_value=https://bbs.csdn.net/topics/a_obj.get (key);//get the key corresponding to the value of
String b_value=https://bbs.csdn.net/topics/b_obj.get (key);
if(! A_value. Equals (b_value)) {
The info (" two objects "+ key +" corresponding values are not consistent ");
return false;
}
}
return true;
}

9. Hash256, md5 algorithm function
Guide package import org.apache.com mons. Codec. Digest. DigestUtils;//download org.apache.com mons. Codec_1. 3.0 v version package
String md5=DigestUtils. Md5Hex (STR) toLowerCase ();//Upper Upper and Lower lowercase
String hash256=DigestUtils. Sha256Hex (STR) toLowerCase ();



  • Related