Jmeter – 9 ways to parameterize!

Whether the jmeter tool is used for interface testing or performance testing, parameterization is a knowledge point that must be mastered and is very useful. Parameterized usage scenarios:

1) Multiple requests have the same IP address. If the server address is changed, the script needs to change the IP of each request.
2) Register an account and do not allow duplicate accounts; when you want to register users in batches
3) Simulate multiple user logins and need to use different user information to log in
4) The output result of the previous request is used to pass in the parameters of the next request, such as the token information obtained by logging in, and the parameter call used to submit the bill request.

1. User-defined variables

1. Select Add-Configuration Component-User-defined Variable from the right-click shortcut menu.

The values of all parameters defined in user-defined variables cannot change during the execution of the test plan. Therefore, generally only the parameters in the test plan that do not need to change with iterations (parameters that only take a value once) are set. here. For example, the application’s tid and vid.

Picture

The tid and vid in the sampler are replaced by ${tid} and ${vid}. Even if the deployment server of the application under test changes, you only need to simply modify the values of the tid and vid parameters.

2. There are also user-defined variables under the test plan. The usage method is the same as above.

Picture

2. User parameters

Right-click the shortcut menu and select Add-Preprocessor-User Parameters

Different from user-defined variables, the parameter values in the user parameter list can change during the execution of the test plan.

Picture

As shown in the figure, username and password have two different sets of values. Through the four buttons at the bottom right of the interface, you can add parameters to the list, add parameter values, delete parameters and delete parameter values.

The “Update once per iteration” option controls the change rules for parameter values. If this option is selected, the value of the parameter remains unchanged in each iteration, and the next value is taken at the beginning of a new iteration.

If this option is deselected, the parameter value takes the next available value when each sampler within its scope makes a request.

If the value range of the parameter is small, it is more appropriate to use user parameters; if the value range of the parameter is large, it is recommended to use the csv data set config method.

3. Function Assistant

1. CSV function assistant
Click “Options” in the menu bar—->Function Assistant dialog box, see the picture below: The CSV file column number starts from 0, the first column is 0, the second column is 1, the third column is 2, and so on.

Picture

Copy the generated parameterized function, open the login request page, and find the field we want to parameterize in the parameterization on the right. Here we parameterize the user name and password. The first column is the user name, and the column number is 0; the second column The column is the password, and the column number is 1; just modify the column number of the corresponding parameterized field in the function.

Picture

2. Random
The Options-Function Assistant dialog box lists how to use system functions. After generation, just pass in the parameters and use them directly.

Picture

3. The following introduces common system functions:

1.__time gets the current time

The first parameter is the time format, and the second parameter is the parameter name to store the current time value. For example, enter yyyy-MM-dd HH:mm:ss in the first parameter, and click Generate to get the correct function expression. Mode.

2.__Random random number

3.__StringFromFile reads records from the local csv file and stores them in parameters

Compared with CSV Data Set Config, the power of this function is that it can support reading data from multiple files. The first parameter is the file name (use an absolute path), the second parameter is a parameter list to store the obtained data, and the third and fourth parameters are the starting and ending sequence numbers of the data file respectively.

Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036 [password: csdn999]

4. CSV Data Set Config configuration component

Right-click shortcut menu Add-Configuration Component-CSV Data Set Config

Picture

Filename:Text file (data pool) to obtain parameter values. File names can use either absolute paths or relative paths. When using a relative path, jmeter will look for the file in the path of the current test plan.

File Encoding: File encoding method, the default is ANSI. If the text file is saved in other ways, please use the corresponding encoding method (such as utf-8).

Variable Names: A parameter list that retrieves content from a text file. The parameter list can contain multiple parameters, separated by commas.

Delimiter: The delimiter that separates record fields in text files, “t” represents the tab key.

Allow quoted data: When the value of this item is False, CSV Data Set Config will take the value of the corresponding field in the text file as the parameter value unchanged; when it is Trues, it will process the value of the corresponding field in the text. For the part quoted by double quotes, take the content in the double quotes as the parameter value. Unless the value of a field in the CSV file contains a set delimiter, this item can be set to False.

Recycle on EOF: When the value of this item is True, recycling of values is allowed. When all the records in the file have been fetched, the value is fetched from the first line of the file again. When it is False, the execution behavior of the test plan is determined based on the value of stop thread on EOF.

Stop thread on EOF?: When Recycle on EOF is true, no matter what value this item takes, the execution behavior of the test plan is the same; when Recycle on EOF is False, when the value of this item is True, After the file record is fetched, the thread stops running. If the value of this item is False, the value of the given parameter when the thread is subsequently executed is .

Sharing mode:

All threads, all threads of this test plan share the records in the csv file (all threads take the records in the file in order).

Current thread group, all threads in each thread group in the test plan share records in the csv file.

Current thread, each thread uses csv records independently.

The following can be used directly within the parameters:

Picture

5. Attributes of jmeter

When running a test plan in command line mode, the user can specify that values entered from the command line replace certain values in the test plan. For example: Use the command line to specify the number of threads and running time of the thread group.

Picture

The __property function has three parameters, namely the attribute name, the variable name to store the attribute value (optional), and the attribute default value (optional).

Command Line:

Jmeter –n –t test.jmx –Jthreadcount=10 –Jduration=60

-J means to set the value of the property. The above command line means: 10 threads are running, and the running time is 60 seconds.

6. Regular extraction

You need to use the parameter content returned from the previous request:
1. First add a post-processor and regular expression extractor. You can use Debug Postcessor to view the request parameter results.

Picture

2. Just include the reference name in the next request.

Picture

7. Need to use the parameter content returned by the previous thread

1. First, use the __setProperty function in thread group 1 to set the jmeter attribute value (this value is the global variable value), and set the required variable value such as ${oldtoken} to the jmeter attribute value, that is, newtoken. Example:

Picture

String oldtoken=bsh.args[0];

{__setProperty(newtoken,{oldtoken},)}

2. Use the _p function in thread group 2 to call the jmeter attribute and obtain the attribute value, thereby realizing cross-thread group parameter value transfer.

Just call this attribute directly in the parameter or url, method: ${__P(newtoken,)}

Picture

8. Parameters need to be base64 encrypted

1. First add a post-processor, BeanShell PreProcessor, and use Debug Postcessor to view the request parameter results.

Picture

2. Script area:

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

String cursor64= bsh.args[0];

String queryId64 = bsh.args[1];

BASE64Encoder encode = new BASE64Encoder();

String cursorbase64 = encode.encode(cursor64.getBytes());

System.out.println(cursorbase64);

vars.put(“cursor64”, cursorbase64);

vars.put(“queryId64”,querybase64);

3. Just use the put parameters directly in the request.

Picture

9. Parameter AES and other encryption – BeanShell

Write the code into a jar package, put the jar package under the jmeter directory apache-jmeter-2.13libext, add a BeanShell PreProcessor under a sampler of jmeter, import our jar package in the beanshell PreProcessor, and call the addition and solution inside Password method, save the result in a jmeter variable. The following two methods are the most commonly used in beanshell:

· vars.get(String paramStr): Get variable value

· vars.put(String key,String value):, save data into jmeter variables

import com.pingan.ff.account.user.utils.*;

//encryption

System.out.println(“encryption”);

String password = “123123”;

String encode = SecurityUtils.getKey(password);//Call the method in the tool class to encrypt

System.out.println(“Set my encode”);

vars.put(“encode”,encode);//Save the value into the jmeter variable encode

String getEncode=vars.get(“encode”);

System.out.println(“Get my encode: ” + getEncode);

Picture

Store the encrypted password in a jmeter variable, and then use it through ${encode} in the http sampler:

Picture

Finally, I would like to thank everyone who has read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software testing interview document

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.