JMeter's BeanShell assertion---data processing

Source: Internet
Author: User
Tags prev

In the interface test, the validation of the response data is a very important part, when using JMeter for interface testing, there are a variety of respone verification methods, such as response assertions, BeanShell assertions, and so on, BeanShell assertion can be customized assertions, free and flexible script implementation of assertions.

1. 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.

Here's how to use BeanShell for assertions and data processing, if we have the following response data:

1 {2     "message": "Cannot send a scheduled task less than the current point in time",3     "StatusCode":4 }

(1). we use the Jsonobject object to get the JSON data, first we need to download the Org.json jar package, then import the jar package in the test plan, and put the jar package in JMeter's Lib directory, the following verifies Whether the value of statuscode is equal to:

1 Importorg.json.*;2 3 //gets the return of the last request4String jsonstring =prev.getresponsedataasstring ();5Jsonobject Responsejson =NewJsonobject (jsonstring);6 7 //determine if the return value is the same as expected8 if(Responsejson.getint ("StatusCode")! = 200) {9     //The assertion failure is set to true, that is, the use case fails, and the failuremessage is displayed in the result treeTenFailure =true; OneFailuremessage = "StatusCode return value is incorrect"; A}

(2). What do you need to do if you want to verify that the value of message in Respone is the same as expected?

1 Importorg.json.*;2 3 //gets the return of the last request4String jsonstring =prev.getresponsedataasstring ();5Jsonobject Responsejson =NewJsonobject (jsonstring);6 7String fbpcontent = responsejson.getstring ("message");8 if(!fbpcontent.equals ("Cannot send a scheduled task less than the current point in time")) {9     //The assertion failure is set to true, that is, the use case fails, and the failuremessage is displayed in the result treeTenFailure =true; OneFailuremessage = "message is inconsistent with actual value"; A}

If we have the following response response data:

1 {2"StatusCode": 200,3"Data": [4         {5"I": "50356",6"N": "Project One",7"V": "2.0",8"IconUrl": "",9         },Ten         { One"I": "45280", A"N": "Project Two", -"V": "3.0", -"IconUrl": "", the         }, -         { -"I": "14656", -"N": "Project Three", +"V": "2.6", -"IconUrl": "", +         }, A         { at"I": "66213", -"N": "Project Four", -"V": "5.0", -"IconUrl": "", -         } -     ] in}

(3). We need to parse the value of the array data, how to parse it?

1 Importorg.json.*;2 Importjava.util.Arrays;3 4 //gets the return of the last request5String jsoncontent =prev.getresponsedataasstring (); 6 7Jsonobject response =NewJsonobject (jsoncontent); 8Jsonarray groups = Response.getjsonarray ("Data")); 9String Strdata=groups.tostring ();TenLog.info (strdata)

Now there are more complex formats of respone data:

1 {  2"Prioritygroups": {  3"Proid": 1234,  4"Name": "Project One",  5"Groups": [  6       {  7"id": "50356",  8"Items": [  9           {  Ten"Proid": 1360,   One"N": "PC Side",   A"Index": 1 -           },   -           {   the"Proid": 1361,   -"N": "iOS Side",   -"Index": 2 -           },   +           {   -"Proid": 1362,   +"N": "Android",   A"Index": 4 at           }   -         ]   -       }   -     ]   -   },   -"Promotion": {   in"Proid": 1364,   -"Cusid": 84,   to"Name": "Project Two",   +"From": 1470821215,   -"To": 1470907615,    the"Status": 1,   *"ObjectId": 1069,   $"CreateBy": 394,  Panax Notoginseng"EFF": 1470821215000,   -"Createtime": 1470821155000 the   }   +}

(4). What do we need to do to parse the data in groups?

1 ImportOrg.json.JSONArray; 2 Importorg.json.JSONException; 3 ImportOrg.json.JSONObject; 4 5String jsoncontent =prev.getresponsedataasstring (); 6 7Jsonobject response =NewJsonobject (jsoncontent); 8Jsonarray groups = Response.getjsonobject ("Prioritygroups"). Getjsonarray ("groups")); 9String strgroups = groups.tostring ();

JMeter's BeanShell assertion---data processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.