JMeter---BeanShell implements interface pre- and post-operations

In JMeter , you can use BeanShell scripts to implement pre- and post-interface operations.

The following are the steps to use Bean Shell script to implement interface pre- and post-operations:

1. Add a BeanShell pre-processor or post-processor to the test plan.

Right-click the interface request that needs to add pre or post operations, and select "Add" -> "Pre Processors" or "Add" -> "Post Processors" -> "BeanShell PostProcessor".

Insert image description here

2. Write pre- or post-operation logic in the BeanShell script.

Scripts can be written using Java syntax. In the preprocessor, you can use variables to set request parameters or set global variables. In the post-processor, the response results are obtained and processed accordingly.

For example, you can set a variable to "value" via vars.put("myVariable", "value"); You can also use prev.getResponseDataAsString() to obtain the response result of the previous request.

Run the test plan. The BeanShell script will be executed before or after each request is executed.

It should be noted that the BeanShell script will be executed once before or after each request is executed. Therefore, if a variable is set in the preprocessor, the value of the variable can be obtained in the postprocessor.

Here are some common examples of writing custom scripts to perform usage:

1. Preprocessor example: setting request parameters

// 在前置处理器中设置请求参数 
String username = "testuser"; 
String password = "password123"; 
vars.put("username", username); 
vars.put("password", password);

In this example, the username and password are set as variables and stored in JMeter variables using the vars.put() method. In interface requests, you can reference these variables using username and {username} and username and {password}.

2. Post-processor example: extracting response results

// 在后置处理器中提取响应结果 
String response = prev.getResponseDataAsString(); 
log.info("Response Data: " + response);

In this example, the prev.getResponseDataAsString() method is used to obtain the response result of the previous request and print the result. You can log the results to JMeter's log using the log.info() method.

3. Preprocessor example: setting global variables

// 在前置处理器中设置全局变量 
String token = "abcdef123456"; 
props.put("token", token);

In this example, a global variable is set to a fixed value and stored in a JMeter global property using the props.put() method. Global variables can be shared and accessed throughout the test plan.

4. Post-processor example: verify response results

// 在后置处理器中验证响应结果 
String response = prev.getResponseDataAsString(); 
if (response.contains("success")) { 
    vars.put("result", "Pass"); 
} else { 
    vars.put("result", "Fail"); 
}

In this example, the response result of the previous request is converted into a string and verified based on the content of the response result. Depending on the verification result, a variable named "result" is set to "Pass" or "Fail".

The above examples only demonstrate some uses of BeanShell scripts in pre- and post-processors. More complex scripts can be written according to specific needs to achieve the desired operations.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!   

Guess you like

Origin blog.csdn.net/nhb687096/article/details/133096139