beanShell assertion

Users can customize the assertion in jmeter- "beanShell assertion" in. Freedom and flexibility to achieve their assertions script 

beanShell assertion Interface Description 

BeanShell variables that can be called directly, without adding the prefix. 

1.log print log log.info ( "print log in the console"); 

2.SampleResult get SampleResult object, you can get the information you want through this object 

3.Response Get Response object, corresponding information can be acquired by this object 

4.Failure see whether the interface call is successful, be successful if it returns false, true is a failure 

5.FailureMessage failure information, the time is not set failure information is empty. This information can be set 

6.ResponseData Get response body type is byte [] 

7.ResponseCode successfully returns an interface code 200 

8.ResponseMessage get msg success is OK 

9.ResponseHeaders get an interface header information returned from the server 

10.RequestHeaders obtain header information requested by the client 

11.SampleLabel get the name of interface requests 

12.SamplerData acquisition request url and body 

13.ctx representatives context information can be used directly 

14.vars can be called directly, the acquired data becomes jmeter variable (put), you can also get user-defined variables (get) 

eg: 

1. Add a beanShell assertion at http sample 

2. Write a script 

 

1 import org.apache.jmeter.assertions;

2 import org.apache.jmeter.samplers.SampleResult;

3 import org.apache.jmeter.assertions.AssertionResult;

4 import org.json.*;

5

6 String response_data = prev.getResponseDataAsString (); // Get Interface response data returned 7 JSONObject data_obj = new JSONObject (response_data); // converted into json

// 8 determines if the code is equal to 0 is equal to 0 then take field inside 9 String code = data_obj.get ( "code");

10 if(code.equals(“0”))

11 {

12 String user_name = data_obj.get(“data”).get(“user”).get(“userName”).toString();

13 // my_name here is that user-defined variables or interface returns acquired before 14 if (user_name.equals ($ {my_name})) {

15 Failure = false; // set to false indicates a successful run the interface, in the result tree is green sample

16 // 17 ...... do follow-up action.

18 }

19 else{

20 21} // do other actions

22 }

23 else

24 {

25 Failure = true; // run directly interfaces Analyzing failure indicates failure of the sample in the result tree red 26 prev.setStopThread (true); // if assertion fails, the interface does not need to run again later, direct suspension 27}

Guess you like

Origin blog.csdn.net/qq_35577990/article/details/93735385