Jmeter (52) - From entry to advanced level - passing parameters across thread groups in jmeter (detailed tutorial)

1 Introduction

All the articles shared before have only one thread group, and parameters are only passed in one thread group. So if we need to pass parameters in two thread groups, how do we do it? Today I will explain to my friends or children how to transfer parameters between thread groups.

2. What is passing parameters across thread groups in jmeter?

As the name suggests: use the jmeter tool to pass parameters between two or more thread groups.

3. Why are parameters passed between thread groups?

First of all, when we receive a performance testing task and need to stress test an interface, we do not need to log in every time (if you log in every time, it will affect the test results), then when encountering this situation, we have to log in The interface obtains the token (session, cookie), and then uses another thread group to perform stress testing.
Secondly, you can imagine a scenario. Suppose we are testing a shopping cart program. Before adding something to the shopping cart, we need to log in first (such a logical design is incorrect. Every time you select an item to add to the shopping cart, you need to log in again. Log in, who will use your APP with such a design?). In a normal scenario, our login status can be maintained for a period of time, and there is no need to log in every time before adding a shopping cart. In this case, when we test, we need to write the login and adding the shopping cart to two thread groups. , and then the problem arises. The two thread groups cannot share parameters directly.

The above two situations are practical problems often faced in work, so parameters need to be passed between thread groups.

If you want to learn automated testing, I recommend a set of videos to you. This video can be said to be the number one automated testing tutorial on the entire network played by Bilibili. The number of people online at the same time has reached 1,000, and there are also notes that can be collected and distributed by various channels. Master technical communication: 798478386    

[Updated] A complete collection of the most detailed practical tutorials on Python interface automation testing taught by Bilibili (the latest practical version)_bilibili_bilibili [Updated] A complete collection of the most detailed practical tutorials on Python interface automated testing taught by Bilibili (practical version) The latest version) has a total of 200 videos, including: 1. Why interface automation is needed, 2. Overall view of request for interface automation, 3. Interface practice for interface automation, etc. For more exciting videos from UP master, please follow the UP account . icon-default.png?t=N7T8https://www.bilibili.com/video/BV17p4y1B77x/?spm_id_from=333.337

4. How to pass parameters between thread groups?

The scope of JMeter variables is limited to the thread to which they belong. This design is carefully considered to allow the test thread to operate independently. Sometimes users may need to pass variables between different threads (which may or may not belong to the same thread group). In Jmeter, in addition to basic variables, there is another kind of property, which is its own property. We can use properties to complete variable transfer between thread groups. Properties are shared by all JMeter threads, so when one thread sets a property, other threads can read the updated value.

5. Actual combat

 Without further ado, Brother Hong would like to quickly practice it. The specific steps are as follows:

1. Create a test plan and rename it to "JMeter Passing Parameters Across Thread Groups", then add a thread group and rename it to "Login". As shown below:

2. Select the thread group, right-click to add the sampler "HTTP Request", rename it to "Get Token", and then configure the sampler. As shown below:

 3. Select the thread group, right-click to add a listener and "View Result Tree", save the script, run jmeter, and view the result tree (test whether you can successfully obtain the token, Brother Hong has already obtained it here, and then wait for other threads) set for later use). As shown below:

From the third step, you can see that Brother Hong has successfully obtained the token. The next step is to extract the token, and then set the token to the attribute so that other thread groups can call it.

4. Extract the token. The regular extractor is used here. You can use other extractors, as long as it is extracted. First, select "Regular Tester" in the result tree, then enter the regular expression to extract the token. As shown below:

 5. As can be seen from the above, the filled in regular expression can be successfully extracted to the token, then select Get token, right-click to add the regular expression extractor and then copy the regular expression tested here to the regular expression extractor for relevant configuration. As shown below:

6. According to the above idea, if you extract it, you need to add it to the attribute. Brother Hong uses the post-processor here - BeanShell post-processor. Select Get token, right-click to add the BeanShell post-processor, and then configure it. Add script. As shown below:

Reference script:

String token = bsh.args[0];
${__setProperty(newtoken,${token},)};
log.info("token:" + token);

7. Add " log.info("token:" + token); " to the script of the BeanShell post-processor above. Then check in the log whether the value of the token is obtained. After running the test plan script, check Log (click the yellow icon with an exclamation mark in the upper right corner), as shown below:

 

8. From the log above, we clearly found that the token value was not extracted. Checking the regular expression extractor found that the matching number was -1. There is a problem here. Change it to 1, run the test plan, and check the log again, as follows As shown in the figure: 

9. From the above figure, we find that the value of the token has been extracted into the attribute, and then the token can be passed to other thread groups to call it. Then continue to create a thread group, right-click to add the configuration element "HTTP Cookie Manager" and configure it, as shown in the following figure:

10. Right-click to add the sampler "HTTP Request" and rename it to "View Role" to configure it, as shown in the figure below:

 11. Add the listener "View Result Tree", as shown in the figure below:

12. Save the test plan script, and after running jmeter, view the result tree (you can see the list of roles obtained), as shown in the following figure:

 

Okay, now jmeter's cross-thread group passing parameters have been fully implemented.

6. Summary

Here is an explanation of how to pass tokens across threads, such as cookies, sessions, and other parameters. You can just copy the cat and the tiger. Well, that’s it for today, thank you for your patience in reading.

Guess you like

Origin blog.csdn.net/Faith_Lzt/article/details/133352165