I am testing a web application which has a following flow :
- User logs in
- On successful login, an access token is issued.
- Every request after login has the access token to get a resource.
In my JMeter test plan, I added a cookie manager and I could extract this access token from the response header of the login request. I want to set this access token as a cookie the test plan.
I added this after extracting the access token in a BSF PostProcessor : vars.put('COOKIE_access_token', actual_token); and it is seen as a cookie variable in the debug sampler.
But the subsequent requests after login do not have this access token in their cookie data, and as a result are again redirected to login page.
How can I set this token as a cookie which will be used for all further requests?
I am testing a web application which has a following flow :
- User logs in
- On successful login, an access token is issued.
- Every request after login has the access token to get a resource.
In my JMeter test plan, I added a cookie manager and I could extract this access token from the response header of the login request. I want to set this access token as a cookie the test plan.
I added this after extracting the access token in a BSF PostProcessor : vars.put('COOKIE_access_token', actual_token); and it is seen as a cookie variable in the debug sampler.
But the subsequent requests after login do not have this access token in their cookie data, and as a result are again redirected to login page.
How can I set this token as a cookie which will be used for all further requests?
Share Improve this question edited May 12, 2017 at 21:29 cweiske 31.2k15 gold badges147 silver badges205 bronze badges asked Jan 14, 2016 at 10:24 Mahesh SMahesh S 612 silver badges5 bronze badges2 Answers
Reset to default 6Defining variable does not add the cookie itself. You need to insert cookie into Cookie Manager to make this work, like:
- Add a Beanshell PreProcessor as a child of the request which fails
Put the following code into the PreProcessor's "Script" area:
import org.apache.jmeter.protocol.http.control.Cookie; sampler.getCookieManager().add(new Cookie("access_token", "actual_token", "domain", "path", true, Long.MAX_VALUE));
Replace domain
, path
, true
(stands for "secure") and Long.MAX_VALUE
(expires) with your own values.
See How to Use BeanShell: JMeter's Favorite Built-in Component for example of manipulating cookies programatically.
import org.apache.jmeter.protocol.http.control.Cookie;
sampler.getCookieManager().add(new Cookie("access_token", "actual_token", "domain", "path", true, Long.MAX_VALUE));
This code really worked. This helped me.
Who ever wants to pass missing cookie(session or any cookies) in request cookie data , instead of adding header manager please use the above code in bean shell pre processor.
Thank you DMITRI T