Jmeter interface test - MD5 encryption - request verification

Table of contents

Foreword:

Part 1: Prepare Jmeter first

Part 2: Write MD5 encryption-request signature verification script 

 Part 3: Execute the script


Foreword:

JMeter is a commonly used interface testing tool. For interfaces that need to be encrypted and verified, we can use the MD5 encryption algorithm to encrypt and request signature verification

Part 1: Prepare Jmeter first

1. Before you start writing scripts, make sure your Jmeter is running properly. If you have not installed Jmeter, you can refer to the following methods:

  1. Jmeter needs java operating environment, so you need to download JDK, JDK download address: https://www.oracle.com/technetwork/java/javase/downloads/index.html (it is better to use JDK 8 or above version, you can use the above link Download the latest JDK version)

  2. To install JDK, please refer to Baidu tutorial: https://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html (note that java environment variables must be configured correctly)

  3. Download and install Jmeter, Jmeter download address: http://jmeter.apache.org/download_jmeter.cgi. Jmeter installation process (refer to Baidu tutorial): https://jingyan.baidu.com/article/acf728fd68e7bef8e510a3cb.html (note that Jmeter5.0 version needs to configure environment variables, and JDK 8 and above are required)

  4. Import the jar package required for MD5 encryption. The name of the jar package is: commons-codec-1.9.jar, Jmeter itself has the function of MD5 encryption, but you need this jar package, you can check whether the lib folder under the Jmeter installation path has this jar package: D:\ apache-jmeter-5.0\apache-jmeter-5.0\lib. If not, download one from the Internet and put it in the lib folder.

  5. Permanent Chinese Jmeter. Find jmeter.properties under the Jmeter installation path: D:\apache-jmeter-5.0\apache-jmeter-5.0\bin\jmeter.properties. Open with an editor (right click and open with notepad): find #language=en, add below: language=zh_CN save and restart Jmeter

Part 2: Write MD5 encryption-request signature verification script 

1. The new things that need to be created to write the script are as follows:

  1. Add a thread group: Test Plan - Add - Thread (User) - Thread Group

  2. Add a preprocessor BeanShell PreProcessor under the thread group: Thread Group - Add - Preprocessor - BeanShell PreProcessor (BeanShell PreProcessor is mainly used to complete the splicing of encrypted strings and MD5 encryption)

  3. Add an HTTP header manager under the thread group: Thread group - add - configuration element - HTTP header manager. It is used to set the parameters in the request header.

  4. Add an HTTP request under the thread group: Thread Group - Add - Sampler - HTTP Request.

  5. Finally, add view result tree and aggregate report: thread group - add - listener - view result tree/aggregate report The purpose of view result tree is to check whether our request is successfully accessed, whether the requested content and the returned content are Correct aggregation report: mainly used to count the total number of requests, average request time, response time, Err number, Err rate and other data.

2. After the model of the newly created script is successful, start to fill in the content of the script and complete the MD5 encryption function of the script.

  • Add the following content to the pre-processor BeanShell PreProcessor: (Jmeter 5 comes with org.apache.commons.codec.digest jar, so you only need to import it) (The order of string splicing rules is: according to the first letter of the parameter, Sort the parameters in the order of AZ and splicing, you can refer to the sorting rules of the parameters in the figure) (the connection between characters is similar: appVersion=V1.0.0&clientType=XXX&productId=XXX&source=XXX, note: different companies have different splicing rules , you need to ask about the development of specific splicing rules)

// Import the jar package needed for MD5 encryption.
import org.apache.commons.codec.digest.DigestUtils;
// declare the string you need to concatenate
String requestTime = "${__time(,)}"; //Generate timestamp
String appVersion ="XXX";
String clientType ="XXX";
String productId ="XXX";
String source ="XXX";
//Concatenation of strings that need to be encrypted
String str = "appVersion="+appVersion+"&clientType="+clientType+"&productId="+productId +"&requestTime="+requestTime+"&source="+source+"istarkid2018";
// encrypt the concatenated string
String sign = DigestUtils.md5Hex(str);
vars.put("signStr", sign.toString()); //Provide the generated signStr to beanshell external component reference
vars.put("requestTime", requestTime.toString()); //Provide the generated timestamp to beanshell external component reference
vars.put("appVersion", appVersion.toString()); //Provide the generated version number to beanshell external component reference
vars.put("clientType", clientType.toString()); //Provide the generated clientType to beanshell external component reference

There are many methods for generating timestamps, and different companies use different methods for generating timestamps. For this point, you need to inquire about the development of specific generation rules. Here are several generation rules:

Generate a thirteen-digit timestamp: ${__time(,)}

Generate a ten-digit timestamp: ${__time(/1000,)}

Generate the current date, accurate to the timestamp of the day: ${__time(yyyy-MM-dd,)}

Generate the current date, accurate to the second timestamp: ${__time(YMDHMS,)} 

  • Enter all the parameters that need to be verified in the HTTP information header input interface signature verification: for example: token, singa, etc. Pass in according to the specific situation

  • In the HTTP request, add: server IP, request method, interface path, interface parameters and other values. 

 Part 3: Execute the script

1. Click: the green button on the menu bar to execute, the first newly created test plan will prompt you to save first, just save the already built test plan to any location

2. Set the number of concurrency and perform pressure testing;

3. After setting, click Execute to perform stress test. After the stress test is completed, you can view the stress test results in the report.

 As someone who has been here, I also hope that you will avoid some detours. Here I will share with you some necessities on the way forward for automated testing, hoping to help you. (WEB automated testing, app automated testing, interface automated testing, continuous integration, automated test development, big factory interview questions, resume templates, etc.), I believe it can make you better progress!

Just leave [Automated Test] [Automated Test Communication]: 574737577 (remark ccc) icon-default.png?t=N5K3http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=78pGJgx2Fbq3Wv_MrJkBr4Me9CWufea7&authKey=PvfVee5Yu%2F%2FtpxyVtk8QSgpeOPORA2o MLGWJTDqfGCtQ%2BRMv8MY1T% 2BN9UleFs9Q%2B&noverify=0&group_code=574737577

 

 

Guess you like

Origin blog.csdn.net/Free355/article/details/131326813