Application of JMeter-BeanShell preprocessor and BeanShell postprocessor

1. What is BeanShell ?

BeanShell is written in Java, a small, free, downloadable, embedded Java source code interpreter . The JMeter performance testing tool also fully adopts the BeanShell interpreter and encapsulates it into a configurable BeanShell pre- and post-processor. processor, respectively

BeanShell Preprocessor (BeanShell preprocessor) and BeanShell Postprocessor (BeanShell postprocessor) can better handle the request pre-parameters and post-data acquisition in interface performance testing by writing code. The location of the original BeanShell configuration in JMeter is as shown below:

BeanShell Preprocessor (BeanShell preprocessor)

Insert image description here

BeanShell Postprocessor(BeanShell postprocessor)

Insert image description here

2. Basic application of BeanShell Preprocessor (BeanShell preprocessor)

When conducting interface performance testing, a commonly used scenario is that the input parameters of each API request change.

For example, some interface request parameters have timestamps, or in many cases, in order to ensure the security of interface requests, the backend needs to carry a sign parameter in the request, or even the sign is obtained through an encryption algorithm. In other words, such an interface basically requires different input parameter data to be constructed before each request. Therefore, when conducting concurrent testing of such an interface, parameterized request input parameters are required. Preprocessors such as BeanShell Preprocessor (BeanShell preprocessor) can easily construct parameterized input parameters. Take a look at the following examples: timestamps and signatures can be parameterized.

Add the BeanShell Preprocessor preprocessor and edit the following code in the script editing box:

// 导入MD5加密需要用到的jar包。
import org.apache.commons.codec.digest.DigestUtils;
 
// 声明你需要拼接的字符串
//String requestTime = "1680089472000"; //生成时间戳
String requestTime = "${__time(,)}"; //生成时间戳
log.info("=====请求时间戳:=====>>"+requestTime);
 
String yan = "N[8HXx!57Ivy%)#R";
//拼接需要加密的字符串
String str = yan + requestTime;
// 加密已拼接的字符串
String sign = DigestUtils.md5Hex(str);
log.info("=====sign签名:=====>>"+sign);
 
vars.put("sign",sign); //设置变量,将md5加密后的值传递给变量sign
 
vars.put("requestTime", requestTime); //将时间传递给变量requestTime

In the above BeanShell script, two strings are defined: requestTime and sign, and their values ​​are set. The value of requestTime is ${__time(,)}. This function can generate a timestamp according to the current time, and the value of sign is After md5 encryption, a 32-bit lowercase string is generated, and the value is passed to the variables requestTime and sign for subsequent interface request parameterization. Then log printing is also added to the script. After the interface request, you can view the log in Output parameter log information

The effect is as follows:

Insert image description here

This completes the writing of the BeanShell preprocessing script, and then we introduce the requestTime and sign parameters in the script into the interface request, as shown below:

Insert image description here

Finally, after the BeanShell preprocessing script and request parameterization are configured, we run JMeter to see the request effect and log printing, and request the interface twice:

Insert image description here

You can see that the timestamp and sign value of each request are different, and the request response results are all returned correctly. This completes the jmeter concurrent request, each time the request input parameters are different.

3. Basic application of BeanShell Postprocessor (BeanShell postprocessor)

BeanShell Postprocessor (BeanShell postprocessor) actually obtains response-related data through scripts.

It is used to do some scenarios that interact with other interfaces. For example, when a certain field of the response result of interface A is used as an input parameter of interface B, it can be extracted from interface A through BeanShell Postprocessor (BeanShell postprocessor). The data of the response result is then parameterized and passed to interface B or the extracted data is written to a file, etc. Of course, JMeter also provides post-processing components such as regular expression extractors and json extractors to extract response or request data. BeanShell Postprocessor (BeanShell post-processor) may be more convenient to customize the desired data through scripts.

Add BeanShell Postprocessor post-processor. For example, if we want to obtain the response status code, response header, response body and other data after the request, it is possible. Edit the script in the script editing box, as shown below:

Insert image description here

In this way, we can get the response status code, response body, response header and other data for subsequent test verification such as interface response assertion. The running results are as follows:

Insert image description here

The above is the basic application of BeanShell Postprocessor. Of course, you can also write scripts and logic that are more suitable for test scenarios to meet test needs.

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/2301_78276982/article/details/132856816