First, store the response result of an interface, such as adding the BeanShell post processor (BeanShell postprocessor) after the HTTP request to store the response of the HTTP request:
1 Importorg.json.*;2 3 //gets the return value of the previous request4String response =prev.getresponsedataasstring ();5 //convert the return value to JSON6Jsonobject Responsejson =NewJsonobject (response);7 //Get Responsemessage8String message = responsejson.getstring ("Responsemessage");9Log.info ("Value of message:" +message);Ten One //Use the Vars.put () method to store the variable message AVars.put ("Message", message); - //Get Titlelink -String Titlelink = responsejson.getjsonobject ("Data"). GetString ("Titlelink"); theLog.info ("Titlelink Value:" +titlelink); - //Use the Vars.put () method to store the variable message -Vars.put ("Titlelink", Titlelink);
You can use ${message} and ${titlelink} to get the value of a variable in the other interfaces that need to use the variable message and Titlelink.
After the variable is stored, add the BeanShell assertion after the interface that needs the assertion, use Failrue to identify the assertion failure, and failuremessage indicate the reason for the assertion failure, such as:
1 // Use the Vars.get () method to get the value of the variable 2 String message= vars.get ("message" ); 3 4 if (!message.equals ("Success" 5 Failure = true ; 6 failuremessage = "Rule resolution failed" 7 }else { 8 failuremessage = "rule resolved successfully" 9 }
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ------------
2. Use the BeanShell assertion directly after the interface that needs the assertion, use Failrue to identify the assertion failure, failuremessage indicate the reason for the assertion failure, such as:
1 Importorg.json.*;2 3 //gets the return value of the previous request4String response =prev.getresponsedataasstring ();5 //convert the return value to JSON6Jsonobject Responsejson =NewJsonobject (response);7 //Get Responsemessage8String message = responsejson.getstring ("Responsemessage");9Log.info ("Value of message:" +message);Ten One if(!message.equals ("Success")){ AFailure =true; -Failuremessage = "Rule parsing failed, message not equal to success"; - return; the } - - //Get Titlelink -Object Titlelink = responsejson.getjsonobject ("Data"). Get ("Titlelink"); +Log.info ("Titlelink Value:" +titlelink.tostring ()); - + A if(Titlelink.tostring (). Equals ("null") | | "". Equals (Titlelink)) { atFailure =true; -Failuremessage = "Rule parsing failed, Titlelink is empty"; -}Else if(!titlelink.tostring (). StartsWith ("http") &&!titlelink.tostring (). StartsWith ("https")){ -Failure =true; -Failuremessage = "Rule parsing failed, titlelink not empty, but not beginning with http or https"; -}
The response data for the interface is:
1 {2: "Responsecode": "1",3: "Responsemessage": "Success",4: "Responsetype":NULL,5: "Data":6 : {7:: "City": "Shanghai",8:: "Rentunit": "Month",9:: "source": "Personal Listings",Ten:: "title": "Xu Xian Road 1188 Lane Xu Jing Qingpu xu Jing Rent", One:: "Belonger": "Personal", A:: "Housingtype": "Residential", -:: "Floor": "High", -:: "Rentprice": "17000", the:: "Titlelink": "Http://sh.grfy.net/rent/d-34612565.html", -:: "Decoration":NULL, -:: "Direction":NULL, -:: "Issplit": "No", +:: "IMGs":NULL, -:: "Publishtime": "2018-07-25t23:20:33.471", +:: "Contactmobile":NULL, A:: "website": "Http://sh.grfy.net/rent/list_2_0_0_0-0_0_0-0_0_2_0_{}_.html", at:: "Address": "Xu Jing", -:: "ContactName": "Ms. Wang", -:: "Housetype": "4 Room 2 Hall 2 bathroom", -:: "Estate": "Xu Jing", -:: "Roomarea": "177", -:: "Collecthousetype": "Residential", in:: "Collecttype": "Rental", -:: "District": "Qingpu", to:: "Totalfloor": "Total 20 Stories", +:: "Region": "Shanghai", -:: "Isregister": "No", the:: "desc": "Yanlord West Garden 4 Room 2 Hall 2 bathroom housing highlights new on the complete balcony first rent at any time rental requirements family rent for one year tenants stable and normal house description community environment good, property management mature, private clubhouse implementation complete, near Metro 17th Xu Xian Station, surrounding Many international schools, Ren Heng's house quality is guaranteed. No intermediary fees. " * : } $}
The assertion failed as follows:
BeanShell assertion instance of JMeter