I. Business background of using JMeter Cookie

Business background:

In our work, we often encounter the following pressure test scenarios:

Login after the query, recharge, purchase… And other businesses for pressure testing;

Is this a familiar scene? Many business operations rely on login, but a user usually generates a cookie every time he logs in, and must carry his cookie in subsequent operations, and the server verifies each subsequent request.

The first thing that comes to mind is to extract cookies from the login response using the regular expression and then invoke them in other operations.

No way!

Because cookies are not generated in the response result of the login, but are carried in the response header, they cannot be extracted using regular expressions.

So what do you use to get it? Let’s take a look at the official documentation of JMeter:

II. Interpret the official documents of JMeter

The official JMeter documentation reads as follows:

Here’s a simple translation:

The Cookie Manager component has two main functions:

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

If you have an HTTP request and response that contains a cookie, the Cookie Manager will automatically store that cookie and be able to use that cookie for all subsequent requests on that particular site.

Each JMeter thread has its own “cookie store.” So, if you’re testing a site that uses cookies to store session information, then each JMeter thread has its own session. Note that such cookies are not displayed in the Cookie Manager, but they can be seen in the View Results Tree.

JMeter checks if the cookies received are valid for the URL. This means that cross-domain cookies cannot be stored. If you have hacking, or if you want to store cross-domain cookies, need in jmeter. Poperties of Settings: “CookieManager. Check. Cookies = false”.

Cookies received can be stored as JMeter thread variables. If you want to save cookies as variables, you need to set:

“CookieManager. Save. Cookies = true”.

In addition, by default, cookies in JMeter are stored with a name prefix of “COOKIE_” in order to avoid anomalies caused by local variable names. If you don’t want to use the prefix, it is necessary to define attribute “CookieManager. Name. Prefix =” (one or more Spaces). If a prefix is used, the value of a cookie named TEST can be obtained by ${COOKIE_TEST}.

Second, you can manually add a cookie to the cookie manager. However, if you do so, the cookie will be shared by all JMeter threads. Note that such cookies have a long life cycle after they are created.

Null cookies are ignored by default. This can be changed by setting the JMeter property: CookieManager.delete_null_cookies =false. Note that this also applies to hand-defined cookies- any such cookies are removed from the Cookie Manager upon update.

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

If there are multiple Cookie Managers in a Sampler scope, there is currently no way to specify which one to use. Also, cookies stored in one Cookie Manager cannot be used by other Cookie Managers, so use more than one Cookie Manger with caution.

Do you find it hard to understand?

That’s all right, let’s sum it up:

Cookie Manager has two main functions:

1. Automatically manage cookies:

Just as browsers can automatically store and send cookies, if the response to an HTTP request contains cookies, the Cookie Manager automatically stores those cookies and uses the value of those cookies in all subsequent requests to the site. An area where each thread stores its own cookies.

You can’t see the autosaved cookie in Cookie Manager. You can see the cookie Data that was sent in the View Results Tree.

To keep the Cookies to the thread variable, to define the attribute “CookieManager. Save. Cookies = true”. The thread variable is called Cookie_ + Cookie. Attribute CookieManager. Name. The prefix = can be used to modify the default COOKIE_ value.

2. Manually manage cookies:

Manually add cookies to Cookie Manager, and the values of these cookies are shared by all threads.

An easy way to do this is to export cookies using Firefox’s Firebug and import them into JMeter’s Cookie Manager, but you can also manually Add cookies via the Add button.

Tips:

1. It should be noted that the fields and paths must be filled in, especially the fields; Because the current version of JMeter does not support cross-domain requests by default, if you do not fill in the set Cookie will not be carried.

2. Add an HTTP Cookie Manager to the thread that needs to fetch a Cookie. It can default to null, but it must be added, otherwise the Cookie variable will not be stored

${COOKIE_xxxx}; ${COOKIE_xxxx}; ${COOKIE_xxxx}

4. Currently, JMeter cannot have multiple Cookie Managers in a sampler at the same time

5, want to cross domain store cookies, you need to set up CookieManager. Check the cookies = false

Passing cookies inside thread group

Now that you know the Cookie Manager, let’s return to the business scenario:

How to implement the query interface (must first login, carrying a cookie) for the test?

This is a very simple scenario, but if you follow the above method directly, you will find that the result will report an error. Because JMeter does not save cookies by default, you need to add Cookie Manager, as follows:

As mentioned above, adding an empty Cookie Manager will automatically save cookies and pass them between the same thread group, and then running the script again will pass.

But we don’t need to log in at the same time operating pressure measurement, that is to say, I only need to use a user login time, circulation and for other business operations, then need to log in and query operation on two threads in the group, but the jmeter said in the official document cookies cannot cross domain transfer, that is not Shared in the thread group, So how do you do that?

Pass cookies across thread groups

The official documentation says that the cookie variable is valid within the thread and not outside the thread, but the actual test of the same thread group can be directly accessed by other threads.

If it is the case of cross-thread group and cannot be accessed directly, other ways can be used to share the cookie variable. Several ways have been tested, and the following way is the simplest:

Through JMeterUtils. SetProperty and JMeterUtils. GetPropDefault method implementation

1. Add the Beanshell PostProcessor postprocessor to the request that generated the cookie, and add the following script to store 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 the COOKIE_ prefix) and cookie value, respectively

2. Add a BeanShell Preprocessor preprocessor to a request from another thread group that needs to use cookies, and add the following script to fetch the value of the cookie and store it in a thread variable:

import org.apache.jmeter.util.JMeterUtils; String value = JMeterUtils. GetPropDefault (" cookie_name "); Vars. The put (" cookie_name ", value);

The cookie_name here is the same as the name set previously.

Then in the thread group can use ${cookie_name} to get the value of the cookie, that is to achieve across the thread group to pass the cookie.