Capture dynamic values from the various response - jmeter

HTTP request body in JMeter
{"guid":"3659174697251947","syncId":"${C_SyncID1}:8:9474","changes":{"_CreatedDate":{"value":1674476500333},"IsDeleted":{"value":false},"CheckinQuantityEdit":{"value":"0"},"OrderQuantity":{"value":"1"},"LineStatus":{"value":"Purchaseorder"},"CheckinPrice":{"value":"65.62"},"ProductName":{"value":null},"LineNumber":{"value":"1"},"RequiresMobileSync":{"value":true},"CheckInQuantity":{"value":"1"},"NativeMobile.OrderItem_DynamicCustomerProduct":{"value":null},"NativeMobile.OrderItem_Order":{"value":"26177172834105120"},"NativeMobile.OrderItem_CustomerProduct":{"value":"28710447624499413"},"isDeletedOnMobileDevice":{"value":false},"ProductCode":{"value":null}}},
for the above request we need to capture - NativeMobile.OrderItem_Order":{"value":"26177172834105120 and -- guid":"3659174697251947", for the product NativeMobile.OrderItem_CustomerProduct":{"value":"28710447624499413"},
Response 1
from the below response we need to capture order guid 26177172834105120 based on OrderNumber":{"value":"J40000099VN0001"}
So if the OrderNumber":{"value":"J40000099VAN0001" guid will be guid":"26177172834105120"
"OrderNumber":{"value":"J40000099VAN0001"},"JobNumber":{"value":"jbdemo"},"FulfilmentMethod":{"value":"Collection"},"DeliveryAttended":{"value":null},"OrderAPI":{"value":"ICON"},"CustomerOrderTypeAtSubmission":{"readonly":true,"value":"JobOrder"},"OrderDateSubmitted":{"value":1674476500321},"SubOrderType":{"value":null},"NativeMobile.Order_OrderingEntity":{"value":"31806672368314430"},"ICONAPIStatus":{"value":null},"OrderPostStatus":{"value":"Success"},"isDeletedOnMobileDevice":{"value":false},"OrderNumberInteger":{"value":"99"}},"guid":"26177172834105120","hash":"kxniswgHZITElfZ5lHihQiGnuLsuLM4e407HOTy8ih0=","
Response 2
If NativeMobile.OrderItem_Order":{"value":"26177172834105120" and
NativeMobile.OrderItem_CustomerProduct":{"value":"28710447624499413"}
Then only guid will be guid":"3659174697251947"
{"changes":{},"commits":[],"committedObjectsOmitted":false,"deletes":[],"hasMoreItems":false,"newpersistable":[],"objects":[{"attributes":
{"ProductCode":{"value":null},"NativeMobile.OrderItem_Order":{"value":"26177172834105120"},"NativeMobile.OrderItem_DynamicCustomerProduct":{"value":null},"RequiresMobileSync":{"value":false},"AmountMatched":{"readonly":true,"value":"0"},"CheckInQuantity":{"value":"1"},"LineNumber":{"value":"2"},"CheckinPrice":{"value":"91.55"},"LineStatus":{"value":"Purchaseorder"},"ProductName":{"value":null},"isDeletedOnMobileDevice":{"value":false},"OrderQuantity":{"value":"1"},"CheckinQuantityEdit":{"value":"0"},"NativeMobile.OrderItem_CustomerProduct":{"value":"28710447624499413"},"IsDeleted":{"value":false},"_CreatedDate":{"value":1674476500342},"FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251947","hash":"Brd19q5Z1yYq0L8U1noeIsvcthz89N/pvTg/5EqeBnY="
Please help me out her to capture the values 3659174697251947 and 26177172834105120

You can extract guid for the NativeMobile.OrderItem_CustomerProduct==28710447624499413 using JSON Extractor, the relevant JSONPath query will be:
$.[?(#.changes.['NativeMobile.OrderItem_CustomerProduct'].value == '28710447624499413')].guid
Pretty much similar you can get the NativeMobile.OrderItem_Order
$.changes[?(#.['NativeMobile.OrderItem_CustomerProduct'].value == '28710447624499413')].['NativeMobile.OrderItem_Order'].value
With regards to responses 1 and 2 - I cannot help you because your JSON is incomplete.
More information:
JSONPath Operators
How to Use the JSON Extractor For Testing

Related

How can be remove a value from jmeter response using regular expression

Response is {"d":"14049,171681785,347225778"} and I want to extract the value 171681785 from response
Can somebody help me how to write regular expression to extract 171681785 from the above response?
Since 14049 can change, i can advise you to use JSON Extractor indeed. After getting the value of d with $.d, you can use this code in JSR223 Post Proccesor to get the value in the middle.
String[] array = vars.get("yourVariableName").split(",");
vars.put("theValueYouWant", array[1]);

Extract value with applied condition in jmeter using regular expression Extractor

I want to extract every text message where nickname is Mack Dack. how can i do that using regularExpression extractor
{"messages":[{"from":{"nickname":"Mack Dack","participantId":2,"type":"Agent"},"index":2,"type":"ParticipantJoined","utcTime":1600262148000},{"from":{"nickname":"Mack Dack","participantId":2,"type":"Agent"},"index":4,"text":"Starting the bot: ToBi","type":"Message","utcTime":1600262151000},{"from":{"nickname":"Sonaltest Garg","participantId":1,"type":"Client"},"index":5,"text":"Starting the bot: ToBi","type":"Message","utcTime":1600262161000},{"from":{"nickname":"Mack Dack","participantId":2,"type":"Agent"},"index":7,"text":"Welcome! I am Watson. How can I help?","type":"Message","utcTime":1600262163000},{"from":{"nickname":"Sonaltest Garg","participantId":1,"type":"Client"},"index":8,"text":"Welcome! I am Watson. How can I help?","type":"Message","utcTime":1600262174000}],"chatEnded":false,"statusCode":0,"alias":"118","secureKey":"a20dfd835cd8d2199017","userId":"00765F620FFE00A2","chatId":"000BEaFP3FBS003W","nextPosition":9}
my purpose is to select only those text message which are coming from mack dack and compare them with my local json file if both message are same then send message which is same by Sonaltest Garg
Using regular expressions for parsing JSON is not the best idea, normally you should rather be using JSON Extractor but in particular your case you won't be able to get benefits of JsonPath as when it comes to JSON Arrays querying parent attributes using children as filter is not possible.
So I would rather recommend using JSR223 PostProcessor and the following Groovy code:
def messages = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..[?(#.type == "Message")]')
messages.findAll { message ->
message.from.nickname == 'Mack Dack'
}.eachWithIndex { message, index ->
vars.put('message_' + (index + 1), message.text)
}
As the result you will get the following JMeter Variables:
message_1=Starting the bot: ToBi
message_2=Welcome! I am Watson. How can I help?
Check out Top 8 JMeter Java Classes You Should Be Using with Groovy article to learn what these prev and vars shorthands mean

How to conditionally extract data with the if controller after sampling?

I am extracting the HTML response code from a samplier. I would like to use the if controller to conditionally extract more information if the right response code is returned.
So teh Get Message Response Extractor would save the response code to the variable: GetMessageResponse.
Then the If Controller would check if GetMessageResponse is 200:
If this is true then extract more information like this:
However I am not getting anything in ResponseText, what am I doing wrong?
You can do it in one shot if you switch to the JSR223 PostProcessor, the relevant Groovy code would be:
import com.jayway.jsonpath.JsonPath
if (prev.getResponseCode() == '200') {
def responseText = JsonPath.read(prev.getResponseDataAsString(),'$.MessageObj.Text').get(0)
vars.put('ResponseText', responseText)
}
else {
vars.put('ResponseText','Response code is: ' + prev.getResponseCode())
}
References:
Jayway JsonPath
Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
In JMeter what you do is extract whatever the response and set Default Value field to something that will be filled when response will not contain extraction, for example for JSON Extractor:
What you show will not work because you put Extractors in IfController, as there is no Sampler, nothing will happen due to scoping rules.
Also when you'll need for another thing to use If Controller, no need to extract response code, just use:
${JMeterThread.last_sample_ok}

JMeter - Using response data as variable

The response I get back from a post is just a plain text guid seen here I'd like to pull this guid and use it in a variable for my following Get statement:
post response data
And I've tried a bunch of configurations in my regular expression extractor.
But it just ends up pulling Null when what I want is that guid.
Null
I'm new to jmeter - so thank you in advance for the help.
If you need the whole response use the following Regular Expression Extractor configuration:
Reference Name: response
Regular Expression: (?s)(^.*)
Template: $1$
As per How to Extract Data From Files With JMeter guide:
() = grouping
(?s) = single line modifier
^ = line start
. = wild-card character
* = repetition
So it will return the whole response.

JMeter: Parse JSON and count

I am using JMeter to test a web application. The application returns JSON that looks like the following:
{"type":"8","id":"2093638401"}
{"type":"9","id":"20843301"}
{"type":"14","id":"20564501"}
I need to get a count based on type.
I have tried adding foreach controller with a regular expression extractor, but Im not sure I have done it correctly:
Apply to: Main sample only
Response field to check: Body
Reference name: match_type
Regular Expression: "type":"(\d)"
Template: $1$
Match no.: -1
Im new to JMeter so Im not sure if Im doing any of this correctly.
Thanks
If you want to operate on both type and id in single sampler, I think simple regex and ForEach controller won't be sufficient. You will have to write two regex extractor followed by while controller with BSF processor (javascript or beanshell) to extract both the values and export them to jmeter variable. Something of following type
- First Request
- Regex extractor for type
- Regex extractor for id
- BSF processor (to initialize the loopcount=0 and the total_matches of matches that you found)
- while controller (loopcount < total_matches)
- BSF processor
- export/set current_type = type_$loopcount
- export/set current_id = id_$loopcount
- increment loopcount
- USE current_type and current_id in whatever sampler you like
== Update ==
This http://goo.gl/w3u1r tutorial depicts exactly how to go about it.

Resources