Extracting JSON and using it in if condition - jmeter

I need to add a condition in my thread where my next HTTP Request will execute only if the previous response has message message that says 'You can proceed with the booking.'
first response
I tried adding regular expression extractor to get MessageInfo using the below expression.
"MessageInfo": "(.*?)"
I also tried with JSON extractor using below json path expression:
$..MessageInfo
Below is the Expression that i added in if condition:
if condition
However, my execution stops after the first response and does not move into the if condition. Any help would be appreciated.

You need to use a Function or Variable which resolves to true in the If Controller in order to be able to run its children. You're giving a string which isn't equal to true therefore If Controller's children are never run
Therefore you need to use your expression in i.e. __jexl3() function
${__jexl3("${MessageInfo}"=="You can proceed with the booking.",)}
More information: 6 Tips for JMeter If Controller Usage

Related

Facing issue while handling if controller

I am facing issue while using if controller in my jmeter script.
i used condition "${Response}"=="Test unsuccessful" in if controller.
Response variable contains complete response and i'm checking test unsuccessful in response if not it should execute next. however its not executing next even though we are not getting correct response.
How to handle this?
I don't think you can use the condition like "${Response}"=="Test unsuccessful", you can use a JMeter Variable if it resolves to true (or false), however if you want to compare 2 strings and basing on result conditionally execute (or don't execute child sampler(s)) you need to use a function which compares the strings and returns true or false, i.e. __jexl3() would be a good candidate:
${__jexl3("${Response}"=="Test unsuccessful",)}
More information: 6 Tips for JMeter If Controller Usage

Extracted Regex Value not getting passed into next POST request body in Jmeter

In my first request I am able to extract the value using Regular Expression Extractor which is clearly visible into the debug sampler. The value is extracted by setting the following options in Regular Expression Extractor:-
Name of Created Variable:- instanceUID
Regular Expression:- "InstanceUid":"(.*?)"
Template:-$1$
Match No:-1
Default Value:- (Blank)
The value that I want to pass in the next POST request is visible as:-
instanceUID_g1=2ab5dfb8-a217-4ff2-9025-523565b7b7ad
And the body for the next HTTP POST request is set like this:-
${"iInfo":{"InstanceUid":"${instanceUID_g1}","Registry":"${Registry}"}}
When this request seen in detail inside View Results Tree looks like:-
${"iInfo":{"InstanceUid":"${instanceUID_g1}","Registry":"AAX"}}
As seen the value of ${instanceUID_g1} did not get substituted in the POST body as was for variable ${Registry} which was taken from CSV config.
Being new to Jmeter can anyone suggest what did I miss?
Most probably your Regular Expression Extractor placement is not very correct, be informed about JMeter Scoping Rules concept
If you place Regular Expression Extractor as a child of the request - it will be applied to this request only
If you place Regular Expression Extractor at the same level as several requests - it will be applied to all of them
in the latter case it will be applied 1st to 1st sampler, then to Debug Sampler, then to 3rd sampler so on 2nd step it will be overwritten, most probably that's your problem
Also it appears that you're getting the data from JSON so it makes more sense to use JSON Extractor or JMESPath Extractor

How to configure Regular Expr. Extractor in While Controller to stop loop

I configured a While Controller with following condition:
${__javaScript("${Status}" != "BatchId not found")}
and a Regular Expression Extractor:
Name of created variable: Status
Regular Expression: (.+?)
Template: $1$
Match No.: 1
The request GetNextAsyncResponses in this While Controller checks the results of an async batch request until all results of this batch have been caught.
First I start the batch request and afterward the loop with the GetNextAsyncResponses request which checks if there are results of this batch.
When the batch request is finished I get a response body with string 'BatchID not found' which indicates that no more results are available.
The loop should stop GetNextAsyncResponses when the first response with the string 'BatchID not found' pops up.
But unfortunately, the loop never stops even if the string is shown after 4-5 loops.
I tried also following conditions which I found in similar cases without success.
${__jexl3("${BatchId}" != "BatchId not found")}
${__javaScript("${BatchId}".indexOf("BatchId not found") == -1,)}
I´m not sure what´s wrong ore missing here.
Any help would be appreciated.
Many thanks...
I would suggest updating the regex as below capture:
And then setting While Controller condition to ${__jexl3("${Status}" == "NOT_FOUND")}
The variable used in the While Controller must match the variable you define in the Regular Expression Extractor, to wit if you have Status in the Regular Expression Extractor you need to have the same in the While Controller
${__jexl3("${Status}" != "BatchId not found")}
or via __groovy() function
${__groovy(!vars.get('Status').equals('BatchId not found'),)}
More information: Using the While Controller in JMeter

How to use JMeter's while loop to validate a value from a Sampler which need to be executed again?

I am doing performance validation of HTTP requests. I have a request which returns JSON values in which, a key's value contains 'Planned' or 'Processing' or 'Completed'.
In first, it returns 'Planned', then it returns 'Processing' and finally it returns 'Completed'.
I use Regex extractor to that particular key's value.
I want to repeat that Sampler until it returns 'Completed'.
Please help me to achieve this using while loop or whatever possible.
Edit: After 100's of attempts only (few mins), the sampler returns 'Completed' until then, it returns 'Processing'.
#Bala
You can implement this using While loop
You have to hit the http request first and extract the Status..
now in the while loop use this Jmeter function to evaluate the status ${__javaScript("${Status}" != "Completed")}
Extract the status again in the loop
Here is the TestPlan

Manipulating the request body of HTTP thread based on the data extracted from the previous HTTP response

I want to manipulate the request body of HTTP thread based on the data extracted (using 'Regular Expression Extractor') from the previous HTTP response.
Here is the scenario:-
I have extracted the statusFlag and statusId from 'HTTP request 1' as:
Ref name: status
Reg. Exp: "statusFlag":"(\w+)","statusId":"(\w+)"
So, first I want to check that the value of statusFlag is 'New' or not.
If it is New then I have to proceed and feed statusId in next HTTP request or else display statusFlag mismatch.
Need help. Got stuck badly.
I believe Response Assertion is what you're looking for. Add it after the Regular Expression Extractor and configure it as follows:
Apply to: JMeter Variable -> statusFlag (or your reference name)
Pattern Matching Rules: Equals
Add New as a "Pattern to Test"
The assertion will check whether "statusFlag" is "New" and if not - it will fail the sampler and report the expected and the actual value.
Optionally you can add If Controller after the Response Assertion, use ${JMeterThread.last_sample_ok} as a condition and place 2nd request as a child of the If Controller - it will be executed only if "statusFlag" is new.
See How to Use JMeter Assertions in Three Easy Steps guide for more information on conditionally setting pass or fail criteria to requests in your JMeter test.
That's how your Jmeter project should look like.
Regular Expression Extractor stores extracted value in ct variable that can be accessed in If Controller as "${ct}" == "yourvalue" and, if true, can be also sent as a part of Request 2 body using the same ${ct} reference.
Jmeter project structure

Resources