I have a json key 'total' in api response, which will return values on condition. Need to assert if response value is >= 0 and 0. Please suggest a simple way to achieve this.
I have already tried Json assertion which is not of much help.
I expect 'total' to contain a value greater than 0 (zero) at certain time and 0 (zero) at certain time.
Use the below regEx to fetch the values:=
total=\d+
Put it in Regular Expression Extractor postprocessor.
It will fetch only 0 or any positive number else not found. Now, put if controller to work further with your scenerio.
Hope this helps.
Update:- Put assertion to check if the response contain the required parameter,if yes it will pass else fail. So, if negative then it will fail.
In this, you dont need to put regular expression extractor as you can put expected regEx directly in response assertion.
Related
I am trying to capture below value
FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251659"
and i used below expression
FullyMatched":{"readonly":true,"value":false}},"guid":"(.*?)"
But there is lot of other values with these boundaries and 3659174697251659 value occurrence changing in response every time when replay script in jmeter
Match count: 46
Match[1][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251656"
Match[1][1]=3659174697251656
Match[2][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251652"
Match[2][1]=3659174697251652
Match[3][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251557"
Match[3][1]=3659174697251557
Match[4][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251648"
Match[4][1]=3659174697251648
Match[5][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251553"
Match[5][1]=3659174697251553
Match[6][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251644"
Match[6][1]=3659174697251644
Match[7][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251549"
Match[7][1]=3659174697251549
Match[8][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251545"
Match[8][1]=3659174697251545
Match[9][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251636"
Match[9][1]=3659174697251636
Match[10][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251632"
Match[10][1]=3659174697251632
Match[11][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251537"
Match[11][1]=3659174697251537
Match[12][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251659"
Match[12][1]=3659174697251659
Match[13][0]=FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251655"
Please help me to handle this occurrence changing issues in response
It looks like you're trying to extract a value from JSON
JSON is not a regular language therefore it's not the best idea to use regular expressions
Consider switching to JSON Extractor, it provides much more powerful, robust, readable and reliable way to fetch the "interesting" data from JSON using JSONPath language.
Going forward consider including the full response, it's not possible to guess how does it look like from what you posted.
from below response we need to capture
if OrderNumber":{"value":"J-40-000093-VAN0001"} we need capture guid":"26177172834105110*
{"changes":{},"commits":[],"committedObjectsOmitted":false,"deletes":[],"hasMoreItems":false,"newpersistable":[],"objects":[{"attributes":{"_ShowSites":{"value":false},"ICONStatusTime":{"readonly":true,"value":1674457196799},"DeliveryDate":{"value":null},"NativeMobile.Order_DeliveryAddress":{"value":null},"NativeMobile.Order_CustomFlowItems_Temp":{"value":null},"Status":{"value":"Complete"},"RequiredByDate":{"value":1674529200000},"CollectionASAP":{"value":true},"TotalPriceAtOrderSubmission":{"value":"122.42"},"NativeMobile.Order_Checkout_JobOrder":{"value":"68116944363989153"},"DropToCommunalArea":{"value":false},"CustomerReference":{"value":"testing1"},"OrderType":{"value":"JobOrder"},"DeliveryAccessRestricted":{"value":false},"NativeMobile.Order_SGBranch":{"readonly":true,"value":null},"APIAttemptsCount":{"readonly":true,"value":"1"},"Address":{"value":null},"OrderEmailStatus":{"value":"Success"},"_CreatedDate":{"value":1674456764925},"HasAtLeastOneItem":{"value":true},"NativeMobile.Order_UserAccount_Operative":{"value":"66991044457146029"},"CheckInDate":{"value":1674457678401},"NativeMobile.Order_Job_WiderOrder":{"value":null},"RequiresMobileSync":{"value":false},"ICONOrderStatus":{"readonly":true,"value":"Pending"},"TotalOrderPriceAtCheckIn":{"value":"122.42"},"OrderNumber":{"value":"J-40-000093-VAN0001"},"JobNumber":{"value":"jbdemo"},"FulfilmentMethod":{"value":"Collection"},"DeliveryAttended":{"value":null},"OrderAPI":{"value":"ICON"},"CustomerOrderTypeAtSubmission":{"readonly":true,"value":"JobOrder"},"OrderDateSubmitted":{"value":1674456764928},"SubOrderType":{"value":null},"NativeMobile.Order_OrderingEntity":{"value":"31806672368314430"},"ICONAPIStatus":{"value":null},"OrderPostStatus":{"value":"Success"},"isDeletedOnMobileDevice":{"value":false},"OrderNumberInteger":{"value":"93"}},"guid":"26177172834105110","hash":"fh+MfPCaDRjTn9gjdva5Qx5uxU+6lBjGDcmIpFWJ0OY=","objectType":"NativeMobile.Order"},
from below response
if NativeMobile.OrderItem_Order":{"value":"26177172834105120"} and NativeMobile.OrderItem_CustomerProduct":{"value":"28710447624498114"} we need to capture guid":"3659174697251949
{"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":"4"},"CheckinPrice":{"value":"1"},"LineStatus":{"value":"Pick note printed"},"ProductName":{"value":null},"isDeletedOnMobileDevice":{"value":false},"OrderQuantity":{"value":"1"},"CheckinQuantityEdit":{"value":"0"},"NativeMobile.OrderItem_CustomerProduct":{"value":"28710447624498114"},"IsDeleted":{"value":false},"_CreatedDate":{"value":1674476500360},"FullyMatched":{"readonly":true,"value":false}},"guid":"3659174697251949","hash":"pzZHRHrMSglxWv+E8I+/1JFXiztj1NRykkEe3mWUhvM=","objectType":"NativeMobile.OrderItem"}
Please help me to capture the guids
Thanks
Wasn't that you who asked an identical twin of this question here? The solution would be exactly the same:
Take a look at JSON Extractor, the relevant JsonPath queries would be:
$.objects[?(#.attributes.OrderNumber.value == 'J-40-000093-VAN0001')].guid
$.objects[?(#.attributes.['NativeMobile.OrderItem_Order'].value == '26177172834105120' && #.attributes.['NativeMobile.OrderItem_CustomerProduct'].value=='28710447624498114')].guid
Currently I am having around 15 attributes in my flowfile. Out of these 15, i only want a few (all the attributes that have a prefix 'error_' in it. These 'error_*' attributes can have 2 sets of values, eighter- 'valid' or some error code, say- '945'. Now i want to iterate though all the attributes with prefix - 'error_' and if its value is 'valid', do nothing and if its value is having some error code, append the error code to a string separated by ';'. So basically, if I have 5 error_ attributes:
error_field1: '123'
error_field2: 'Valid'
error_field3: '567'
error_field4: 'Valid'
error_field5: '45'
I want my output as - '123;567;45'.
Please help me as i am new to Nifi and i am not sure on how to work with such complex EL.
There are a couple ways to perform this.
${anyMatchingAttribute('error_'):find('\\d+')}
You can use the anyMatchingAttribute() function to evaluate a predicate against multiple attributes, and use the regular expression find() method to check for the presence of digits. This will give you a boolean result, but won't enumerate & join all the values.
${allMatchingAttributes('error_'):join(';'):replaceAll('Valid;', '')}
If you don't need to recall and associate the error codes with the specific field where they were sourced, you can simply concatenate all of the attributes and then use a regular expression to remove the Valid values.
How to put a condition using response assertion in jmeter.
In loadrunner we have web_reg_find with savecount attribute. This helps us to for keeping condition in IF loop.
if textcount >0
transaction --> pass
else
transaction--> fail and exit iteration and continue
Similarly, how to get the count of text check and place the same in condition (IF loop) in jmeter.
Use any of the post processor extractor to fetch all the values in a variable. Like in regular expression we can use "-1" in "match no." to fetch all the values matching with the regular expression. So, you will get the count.
Now, put that as a condition in if controller like ${__groovy("${countVar}"=="10")}
In short,
1.Add a Regular Expression Extractor as a child of the request to fetch all values.
2.Add If Controller at the same level to check the condition.
More information: 6 Tips for JMeter If Controller Usage
If you provide "Match No" as -1 in the Regular Expression Extractor
you will have ${foo_matchNr} variable containing the number of matches.
Since JMeter 4.0 you have __isVarDefined() function which can be used to check whether a JMeter Variable is defined or not
Both approaches can be used in the If Controller
I am trying to test that a value is not 0 or a negative number, but my test keeps coming back as passed when the number is negative. This is what I have:
if (value > 0) {
Failure = false;
} else {
Failure = true;
}
I thought less than 0 would cover minus numbers, but it seems not to? Or there is something else wrong with this code?
First of all, can it be the case you have Response Assertion with "Ignore Status" box checked?
There is nothing wrong with your code, it should work fine.
Try adding debug(); directive as first line of your script and look into JMeter console for output or add some log.info(...) calls to your script to see the actual "value" of your value and some other helpful information like:
See How to Use BeanShell: JMeter's Favorite Built-in Component for comprehensive information on using and troubleshooting your Beanshell scripts.