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

Management of jmeter Learning Guide Cookies

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

Share

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

The main content of today's article is introduced.

First, the business background of using Jmeter cookie

II. Interpretation of cookie Manager in Jmeter official documents

3. Pass cookies within the thread group

4. Passing cookies across thread groups

Jmeter video: https://edu.51cto.com/course/14305.html

1 Business background of using Jmeter cookie

First, business background:

In our work, we often encounter the following stress testing scenarios:

After logging in, query, recharge, purchase. And other business to carry out stress testing.

Is this kind of scene familiar? Many business operations rely on login, but a user usually generates a cookie each time he logs in, and his cookie must be carried with him in the next operation, and the server verifies each subsequent request.

The first thing that comes to mind is to extract the cookies with a regular expression from the login response and then call it in other operations.

no way!

Because the cookies is not generated in the response result of the login, but is carried in the response header, it cannot be extracted with regular expressions.

So what do you use to get it? Let's first take a look at what the official jmeter documentation says:

2 interpretation of Jmeter official documents

The official Jmeter document reads as follows:

Let me translate it briefly:

The cookie Manager component has two main functions:

First, it can store and send cookies like a web browser.

If you have a HTTP request and response that contains a cookie,cookie manager, the cookie is automatically stored and can be used in all subsequent requests on that particular site.

Each thread of JMeter has its own "cookie store". So, if you are testing a website that uses cookie to store session information, then each JMeter thread has its own session. Note that this cookies is not displayed in the cookie Manager, but you can see them in the View result Tree.

JMeter checks whether the received cookies is valid for URL. This means that cross-domain cookie cannot be stored. If you have eavesdropping, or want to store cross-domain cookies, you need to set "CookieManager.check.cookies = false" in jmeter.poperties.

The received Cookies can be stored as a thread variable of JMeter. To save cookies as a variable, you need to set:

"CookieManager.save.cookies = true".

In addition, cookies in jmeter is stored with the name prefix "COOKIE_" by default to avoid exceptions caused by local variable renaming. If you don't want this prefix, you need to define the attribute "CookieManager.name.prefix =" (one or more spaces). If a prefix is used, the value of a cookie named TEST can be obtained through ${COOKIE_TEST}.

Second, you can manually add a cookie to the cookie manager. However, if you do this, the cookie will be shared by all JMeter threads. Note that the life cycle of such a cookies is long after it is created.

Cookies with null values is ignored by default. This can be changed by setting the property of JMeter: CookieManager.delete_null_cookies = false. Note that this also applies to manually defined cookies-. Any such cookies will be removed from the cookie manager when updated.

Also note that the cookie name must be unique-if a cookie name has the same name as an existing cookie, it replaces the original cookie.

If there are multiple cookie manager in a sampler scope, there is currently no way to specify which one to use. Also, the cookie saved in one cookie manager cannot be used by other cookie manager, so be careful with multiple cookie manger.

Do you find it difficult to understand?

It doesn't matter. Let's sum it up.

Cookie Manager has two main functions:

1. Automatically manage cookie:

Just like browsers can automatically store and send Cookie, if the response to an http request contains Cookie, then Cookie Manager automatically saves those Cookie and uses the values of these Cookie in all subsequent requests sent to the site. Each thread stores its own area of cookie.

The auto-saved cookie is not seen in the cookie manager, and we can see the requested Cookie Data in the view result tree View Results Tree.

To save the Cookies to a thread variable, define the property "CookieManager.save.cookies=true". The thread variable name is COOKIE_ + Cookie name. The property CookieManager.name.prefix= can be used to modify the default value of COOKIE_.

2. Manage Cookie manually:

Add Cookie to Cookie Manager manually, and the values of these Cookie will be shared by all threads.

It is relatively simple to use firefox's firebug to export cookies, and then import it into jmeter's cookie manager. Of course, you can also manually add cookies here through the Add button.

Tips:

1. It is important to note that the domain and path must be entered, especially the domain. Because the current version of Jmeter does not support cross-domain requests by default, the set Cookie will not be brought if it is left empty.

2. Add a Http Cookie manager to the thread that needs to fetch Cookie, which can be empty by default, but you must add it, otherwise the cookie variable will not be stored.

3. In this way, other operation components in the same thread (group) can be obtained directly through ${COOKIE_xxxx}.

4. Currently, jmeter cannot have multiple cookie manager in one sampler.

5. To store cookies across domains, you need to set CookieManager.check.cookies = false

3 pass cookies within the thread group

Now that we know about cookie manager, let's go back to the business scenario:

How to realize the stress test of the query interface (you must log in first and carry cookie)?

This is a very simple scenario, but if you directly follow the above method, you will find that the result will report an error, because jmeter does not save cookies by default, and you need to add cookie manager, as shown below:

As above, add an empty cookie manager to automatically save the cookie and pass the cookie between the same thread group, and run the script again at this point.

But we do not need to stress test the login operation at the same time, that is to say, I only need to log in once with one user, and then cycle through other business operations. At this time, I need to put the login and query operations into two thread groups respectively. But the jmeter official document says that cookies cannot be passed across domains, that is, it cannot be shared in thread groups. How can it be implemented?

4 passing cookies across thread groups

The official document says that the cookie variable is valid within the thread and invalid outside the thread, but other threads that actually test the same thread group can access it directly.

In the case of cross-thread groups, which cannot be accessed directly, you can share cookie variables in other ways. Several methods have been tried, and the following is the simplest:

Realized by JMeterUtils.setProperty and JMeterUtils.getPropDefault methods

1. Add the beanshell postprocessor post processor to the request for generating cookie, and add the following script to save the cookie in the Jmeter attribute parameter:

Import org.apache.jmeter.util.JMeterUtils

JMeterUtils.setProperty ("cookie_name", "cookie_value")

Where cookie_name and cookie_value are replaced by your corresponding cookie name (without COOKIE_ prefix) and cookie value, respectively

2. Add the beanshell preprocessor pre-processor to the request of another thread group that needs to use cookie, and add the following script to take out the value of cookie and store it in the thread variable:

Import org.apache.jmeter.util.JMeterUtils

String value = JMeterUtils.getPropDefault ("cookie_name")

Vars.put ("cookie_name", value)

The name of the cookie_name here is the same as the one set above.

In this thread group, you can then use ${cookie_name} to get the value of cookie, that is, to pass cookie across thread groups.

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

Internet Technology

Wechat

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

12
Report