Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the interface request with dynamic change of parameter name and number by Jmeter

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Demand analysis

When doing interface automation and performance testing, it is often encountered that some request parameters are based on the result of the previous request, the number of dynamically changed parameters, the parameter name may be the same, or the sequence may be incremented, and the number of parameters may be 10 or 20. As a result, it is difficult to simulate the request effect manually, and it is necessary to encapsulate the custom function to achieve the effect.

   Jmeter is an open source performance testing tool, and it is currently one of the most widely used performance testing tools in the industry. Jmeter is developed in Java. This paper introduces how to use Java to develop the extension functions you need.

At present, there are two popular development modes in the industry, and the second one is introduced in this paper.

1. Use the Jmeter secondary open source environment to develop the functions you need directly under the org.apache.jmeter.functions package, and then export the jar package to replace the ApacheJMeter_functions.jar file in the Jmeter directory.

2. Develop the custom function directly under the Maven project, and then package it. This method is simple and efficient.

1. Use Eclipse to create Maven projects, configure Pom files, and introduce dependency packages needed to extend function

2. Create a class, then inherit AbstractFunction, and implement four main methods

3. Compile and package the written project

4. Put the packaged function in the jmeter extension directory and call the custom function to check whether it is correct. JDK environment Jmeter depends on the package version Maven repository version 1.84.03.5.3

I. New Maven project

1.File "New" Project, select Maven "Maven Project, and click Next all the time. Here, the Package package name must end in .destroy format, otherwise the developed function cannot be read by Jmeter.

two。 Configure the Pom file, the Jmeter4.0 version of the Pom file configuration. As long as the Pom file is saved, the system will automatically download and associate the corresponding jar package. The Pom configuration file is as follows:

3. After saving the Pom file, the system automatically downloads the associated dependent file, as shown below:

4. At this point, due to the changes made to the pom file, an x will appear at the top of the project, and the compilation may report an error, so you need to execute Maven "Update Project to update the externally dependent Jar package.

Before updating:

After the update, the compilation and packaging are normal:

2. To write custom function code to extend Jmeter function, there are two main points.

1.1. The package declaration of a class that implements function must contain ".declare"

The JMeter design allows some core classes to be loaded when they can be run in a non-UI mode, and these classes will be loaded first. These classes are loaded through naming conventions. All classes that implement function must contain ".clients", so our custom implemented classes must contain ".packages", such as the package name declared below: "jmeter.test.functions"

1.2. First create a class under the Jmeter.test.functions package named Save_Body_Data, which needs to inherit the AbstractFunction class, and then implement the four methods of the parent class

Private static final List desc = new LinkedList (); / * this variable is used to get a description of the parameter value input * / private static final String KEY = "_ _ Media_Save_Body_Data" / * this is the function name displayed on the function helper * / static {desc.add ("Please enter the first parameter, similar to variable=value, where the variable value can be written in variable format in the format of ${value}. If there are multiple parameters, you only need to use & concatenate" + "similar to variable=value&variable1=value1, the first parameter can also be left empty, and then fill in the Body data"). Desc.add ("Please enter the second parameter, which is a changing sequence id, which needs to be consistent with the variable name obtained in jmeter"); desc.add ("Please enter the third parameter, which states whether the recursive parameter name is the same, otherwise the sequence is incremented, such as id [0], id [1]. If left empty, the default is the same"). Desc.add ("Please enter the fourth parameter, which is used to call the Body_Data in jmeter, and can be customized as needed. If left empty, it defaults to the second parameter name"); desc.add ("Please enter the fifth parameter, which is the concatenation character of the sequence such as%, &, or default is &") / * this description describes the input value of the function * /}

The main thing here is to define the variables desc and KEY and then display the description in the parameter description of the function, as shown in the following figure

The function of the setParameters function is to get the value entered by the interface of the function input box and pass the input value to execute.

Public void setParameters (Collection parameters) throws InvalidVariableException {/ * this function is used to get the input value * / / * here, the following 1 checkParameterCount 2 means that the minimum number of parameters accepted is 1, and the maximum number of parameters accepted is 5 parameters / checkParameterCount (parameters,1,5); values = parameters.toArray () / * store the value in the class variable, install the received parameter as array, and then use value [I] to take it out * /} the getReferenceKey function provides a function name public String getReferenceKey () {/ * this method provides a function name displayed in the Jmeter function helper * / return KEY;}

The getArgumentDesc function is used to get the description of the input parameters.

Public List getArgumentDesc () {/ * this function is used to get the description of the input parameters * / return desc } the execute function is used to obtain the input parameters, and then by processing the parameters as needed, this function is the most important module in the development and is finally executed. In this case, the main function is to obtain the number of variables returned by the previous interface, then encapsulate the variable names and parameter values, and finally connect them with characters to form the effect we need.

III. Compilation and packaging

After writing the code, you can compile and package it. Right-click on the project "Run As" Maven build

Type package in Goals, and then click Apply "Run to compile and package."

After compilation and packaging, a jar package is generated in the target directory, as shown in the following figure

Verify whether the developed function is normal or not

First put the Jmeter.test.functions function in the jmeter\ lib\ ext directory

Then start jmeter, enter the option "function Assistant dialog box, select _ Media_Save_Body_Data, enter the parameter value, and click the generate button (click generate, the system will generate a function, and then you can copy it to the place you need to call) this time only enter the second parameter value (this value must be the name of the variable defined by the previous API, and how to use the function later).

First, you need to add a JSON Extractor element to the previous interface to get the changed parameter values.

Then use BeanShell Sampler to call the generated function, and after the sample executes successfully, the field effect we want will be generated.

The manual request parameters are as follows

The parameters implemented through the code only need to use the parameter name = value and each parameter can be connected with &.

The execution result is as follows:

Introduction to function

1. The effect of the developed function is to pass in 5 parameters, of which only the second is required, and the other parameters can be empty.

2. The first parameter can be empty. When it is empty, the parameters and values in parameters need to be manually written in body data. This time, the parameter names that will not change are manually written in body data, or you can directly pass these parameters to the first parameter in the format of funid=imp_provider&destfunid=pur_apply_provider&parentId=$ {apply_id}.

& pagetype=import&eventcode=import&user_id=$ {user} & dataType=json

3. The second parameter is the obtained variable name, which must be the same as the parameter name defined in json path in jmeter. Only in this way can you obtain the changed parameters in jmeter.

4. The third parameter is to determine whether the format of the repeatedly changed parameter name is the same or the sequence is incremented, such as id [0], id [1], which is not the same by default. If it is a sequence increment, you only need to enter "No".

5. The fourth parameter is used to save the string encapsulated by the entire function. If it is left empty by default, all strings will be encapsulated in the second parameter. If it is filled in, it will be encapsulated in this parameter. You need to call the variable in body data by calling ${variable name}.

6. The fifth parameter is the connection mode of each parameter. If left empty by default, it is &

For the method of downloading Jmeter.test.functions.jar, see the introduction at the end of the article.

The complete source code is as follows:

    if the article is helpful to you, you can click on the follow function in the lower right corner. Welcome to forward and clap the brick. I will continue to launch articles related to the test and share the testing technology with you. Every original article is written attentively to put an end to plagiarism and copying. The official Wechat account is synchronized with the articles on this platform for easy reference.

QQ technology exchange group: please enter the verification information 51CTO to add the group.

Wechat QR code follow official account:

After following, reply to the resource download, and you can get the download address of all kinds of resources shared by yourself.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report