Sarah A
Sarah A

Reputation: 1233

Accessing HttpServletRequest and Response from JSR223 sampler within Jmeter

I am using a custom SDK with the following method:

public Optional<User> retrieveUser(HttpServletRequest request, HttpServletResponse response)

in the jmeter, I am using JSR223 sampler to access this method:

all the imports here
.
Optional<User> userProfile = new SdkClass().retrieveUser(request,response);

where request and response are HttpServletRequest and HttpServletResponse

In the step before JSR223, I have an HTTPRequest Sampler where the user logs in and a cookie gets created. My question is that how do I pass the request and response to

new SdkClass().retrieveUser(request,response); ?

The request should have the cookie that was created in the previous step

Upvotes: 1

Views: 1273

Answers (1)

Dmitri T
Dmitri T

Reputation: 168092

You can use the following code to get previous request and response details

import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;

HTTPSamplerProxy previousSampler = ctx.getPreviousSampler();
CookieManager cookieManager = previousSampler.getCookieManager();
HTTPSampleResult previousResult = (HTTPSampleResult)ctx.getPreviousResult();

See JavaDoc on the aforementioned classes:

Make sure you add HTTP Cookie Manager to your Test Plan elsewise you'll get null on attempt to access it via getCookieManager() method.

Upvotes: 2

Related Questions