JMeter - request executes only once but doesn't pass an extracted JSON variable - jmeter

I'm having some trouble with my JMeter test in regards to passing a variable to other requests in the same thread group.
I have 3 requests and one of them is a login request that is only executed once while other 2 requests are looping for one hour. The other 2 requests need to receive a variable 'access token' that is extracted from a login request response in order for them to return a status 200 response.
However, I notice in request header for those 2 requests that the variable 'access token' is not sent in the header although I receive that variable in login request response, extract it with JSON extractor and then send it in the request header.
This my test plan structure:
This is how I extract the variable from the login request:
...and this is how I send the 'access token' variable in request header for other 2 requests:
Everything worked fine until I added the If controller that executes the login request only once and I use this statement in If controller:
${__groovy(ctx.getThreadNum() == 0 && vars.getIteration() == 1,)}
The login request executes correctly and the 'access token' variable is located in the header response.
I can't figure it out, why doesn't variable gets passed to the other two requests?

Different threads don't share same JMeter variables as accessToken
So you need to saved value in JMeter property, for example add JSR223 Sampler
props.put("accessToken",vars.get("accessToken"));
And change your header value to get from JMeter property:
${__P(accessToken)}
vars - JMeterVariables - e.g.
vars.get("VAR1");
props - JMeterProperties (class java.util.Properties) - e.g.
props.put("PROP1","1234");

Related

ForEach Controller won't run multiple HTTP Requests

I have a ForEach Controller that loops over an array of values which works. Inside the ForEach block I have two HTTP requests and one extractor.
ForEach Loop {
+ HTTP Request 1 (This uses attribute from ForEach)
+ Extractor
+ HTTP Request 2 (This uses attribute from Extractor)
}
The first HTTP request runs and the extractor as well but the second HTTP request fails to run.
I've checked the logs and nothing is mentioned about this HTTP Request. I also tried moving the Extractor out from under the HTTP Requestor 1 but that also doesn't work.
There is no problem to have multiple Samplers as ForEach Controller children
The possible reasons for not executing the 2nd HTTP Request are in:
Your extractor fails somewhere somehow, double check that the variable is set and has expected value using Debug Sampler and View Results Tree listener combination
Your 2nd HTTP Request configuration is not correct, i.e. invalid characters in "Server Name" field or unsupported protocol or the request is actually being executed but takes too long, etc. I would recommend looking into jmeter.log file as normally it has enough troubleshooting information

How to pass access token generated in one request to all request in jmeter?

I had created a access token via first api call that i want to use in all other api call, it works for next call but it fails for third api call.
I am using Regular expression extractor, where i have created a variable named token, it passes in second api call in request header , but for third api call it is not taking it (it takes second api's response in the request)
This is due to JMeter Scoping Rules, if you have the Regular Expression Extractor at the same level as all Samplers - it will be applied to all Samplers, therefore when your Login request gets executed - your token variable gets overwritten with the Login sampler response.
If you want to extract the data only from the Login Token sampler - you need to make the Regular Expression Extractor a child of the Login Token sampler

How to pass post method response value through another post method request value

In Jmeter, how to use AUTH “access_token” for passing through header for another POST request ? Below are the step that i have done but it's not working ... How to use "access_token" value, that will use another post request dinamically ? Attached issues is here...
enter image description here
You need to add jp#gc-JSON Path Extractor in HTTP request, the particular HTTP request which is generating access token in its response data .
If response is like - {"access_token":"f5b06970-f00f-4b44-89c8-305738e19cba"}
In JSON path extractor add
Variable name - access_token (variable in which access_token will be stored)
2.JSON Expression - $.access_token (this will varry according to json response)
3.Default Value - NOT_FOUND
Now the next step is to use this variable named "access_token ". You can use it in your HTTP request for which you need to pass access token under HTTP header manager as access_token = ${access_token }
below link will help you a lot:
https://www.blazemeter.com/blog/advanced-usage-json-path-extractor-jmeter

Beanshell code to call an API, parse JSON response

I am trying to implement a logic where i do not want to send HTTP Requests unless an API returns 0 for a field. If i send the requests without monitoring response from this API, my test is invalid. API returns response in JSON, i can parse and extract the data i need to compare. Below is my test structure.
Thread Group
* While Controller
* CSV Data Set Config
* HTTP Request
* JSON Path Extractor
* Constant Throughput Timer
* While Controller Condition: ${__BeanShell(source("function.bsh”))} != “0”
I want to call this API after every 5 minutes and proceed with sending HTTP Requests when field value is 0. I don't want to check before every HTTP Request just once before sending the first request.
Can someone please help me with the beanshell code (function.bsh) to get API response and parse json response ? Was implemented using python as below
max_behind = 0
response = requests.get("https://api")
consumers = response.json().get("consumers")
for total_behind in consumers.iteritems():
max_behind += total_behind[1].get("total_behind")
As Jmeter developers suggested "Better to avoid scripting languages in Jmeter". So in your case use "If Controller" to resolve your blocker. Below are the details
Use regular expression to extract the "API return field" value, once you hold this value in a variable "valueIs". As shown in belo image
Use "If controller " and in condition enter the following value "${valueIs}==1", then add child requests ( next API call/calls) to "If controller". As shown below image

Jmeter If controller with http code

I want to use if controller in my jmeter load testing. The test is:
do a post and get back an access token.
use that access token to get the next link.
My issue:
I have the access token and have used the post-assertion->regular expression extractor and got the access token from he http response. But now I don't know how to use the if control and ask it do next test only if the http response code is 200. And second question is can i still pass my regular expression value of access token into the if loop's http header manager?
attaching the screen shot of my jmeter.
Try to use Response Assertion to handle state of Request_Access_Token request (success/failure) depending on Response Code returned and then use IfController along with pre-defined JMeterThread.last_sample_ok jmeter's variable - whether or not the last sample was OK - true/false.
Schema will look like below:
ThreadGroup
Request_Access_Token
Response Assertion
Response Field to Test: Response Code
Pattern Matching Rules: Equals
Patterns to Test: 200
Regex Extractor // your Access_Token extractor
IfController
Condition: ${JMeterThread.last_sample_ok} // will be TRUE if Response Assertion above is TRUE (i.e. response code = 200)
HttpRequest
// send extracted Access_Token along with request
...

Resources