Operations 5 min read

Using Groovy to Manage Cookies in JMeter: A Step‑by‑Step Guide

This article provides a comprehensive tutorial on handling HTTP cookies in JMeter using Groovy scripts, covering the creation and configuration of CookieManager, adding and modifying cookies, executing JSR223 pre‑processors, and interpreting console output, supplemented with code examples and screenshots.

FunTester
FunTester
FunTester
Using Groovy to Manage Cookies in JMeter: A Step‑by‑Step Guide

The author shares a collection of JMeter tutorials, focusing on using Groovy to process assertions, variables, command‑line execution, request parameters, and regular‑expression extraction, as well as an analysis of JMeter throughput errors.

In JMeter, HTTP cookies are managed separately from HTTP headers via two distinct components: HeaderManager and CookieManager . The article explains the basic usage of CookieManager , including how to add, retrieve, and modify cookies.

Step‑by‑step instructions are provided: create a simple Thread Group and HTTP request, add an HTTP Cookie Manager , and attach a JSR223 PreProcessor containing Groovy code to manipulate cookies.

Groovy script example:

import org.apache.jmeter.protocol.http.control.*
// Get cookie manager
CookieManager cm = sampler.getCookieManager()
def a = new Cookie("FunTester", "FunTester323323", "account.cnblogs.com", "/", false, 1557598515)
cm.add(a)
Cookie c = cm.get(0)
log.info("-------Cookies : " + c.getName() + "----------- " + c.getValue())
c.setValue("FunTester")
log.info("-------Cookies : " + c.getName() + "----------- " + c.getValue())

The author notes that when constructing a Cookie object, only setting name and value is insufficient; the domain , path , and other parameters must also be provided for the cookie to be sent with requests.

Console output from the test run is shown to verify that the cookie was added and modified correctly:

2020-03-19 21:04:35,634 INFO o.a.j.e.StandardJMeterEngine: Running the test!
... 
2020-03-19 21:04:36,026 INFO o.a.j.m.J.处理cookie: -------Cookies : fds----------- 32423
2020-03-19 21:04:36,026 INFO o.a.j.m.J.处理cookie: -------Cookies : fds----------- FunTester

Finally, the article advises checking results with a View Results Tree listener and includes a disclaimer that the content originally appeared on the “FunTester” public account and should not be republished without permission.

JMeterGroovyPerformanceTestingCookieManagerJSR223
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.