Jmeter - Execute code based on Response assertion result - jmeter

Trying to put some conditional logic in a script to only execute certain steps based on successful validation of a response assertion.
My script:
Main execution thread
- Submit Http request
-- Response assertion code 200 (indicates request successfully submitted)
-- Response assertion contains string (I expect a specific string back if successful)
-- Json extractor (extract the string I tested for in the response for later use)
- Do a bunch of other stuff
My problem is that I only want to execute 'bunch of other stuff' if I get back the expected string (without it everything will fail anyway).
I need a conditional test to only execute if the second response assertion passed.
I've tried using "${JmeterThread.last_sample_ok}" but this gives me an error of If controller: error while processing ... BAD CONDITION...expected true or false
I'm assuming I've placed the 'if' at the wrong point, or just used the condition inappropriately.
Any advice?
UPDATE:
My script currently looks like this:
- If test record is version 0
-- Send http to version 0 api
--- Response code assertion 200
--- Response assertion message contains specific string
--- Json extractor to extract the data associated with the string
-- If "${JmeterThread.last_sample_ok}"
--- Do other stuff
I'm assuming I've placed the If containing 'last sample ok' at the wrong level, or that the fact there's a json extractor as the last previous step that that has upset things.

You're making a mistake in case. Code is "case sensitive".
Here is how If Controller should be configured:

Condition should be like ${JMeterThread.last_sample_ok}==true in If Controller.

Related

Jmeter Response Assertion is failing upon getting from Reg Ex Value

Hello I have a scenario where I need to validate the extracted value from my regex, unfortunately I'm encountering an error upon using Response Assertion. I would like to seek assistance to any one of you. Your response is highly appreciated. Thank you so much in advance
I am able to extract my regex upon running, but I encounter upon using that on my Response Assertion.
Screenshot:
Response Screenshot
Response Assertion Failure
Response Assertion Configuration
Expected Result: I just want to validate "userId = 14534"
If you want to just check the presence of 14534 text in the response - just configure the Response Assertion as follows:
In case you want to check whether 1st row of the user_id column of your SQL query result set is equal to 14534:
In the JDBC Request sampler define a variable name, i.e. user_id
Amend your Response Assertion configuration to Apply to a JMeter Variable called user_id_1:
More information: How to Use JMeter Assertions in Three Easy Steps

JSON assertion failing in JMeter

On sending an incorrect username/password, the server application sends this JSON:
{"result":"error","additional-info":"Incorrect username or password"}
I have made the assertion:
Server sends:
But the tree viewer seems to indicate that there is an error. The response log file has this message
timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
1606209008151,39,SignInRequest,401,Unauthorized,SignInUsers 1-1,text,**false**,**path can not be null or empty**,570,263,1,1,http://localhost:9000/ws/users/signin,37,0,21
What am I doing wrong in asserting the response?
UPDATE
I added json assertion but when I run the test, the screen looks like this (red color but not sure what the reason is)
Interestingly, if I change the validation from error to "error" then I can see reason (kind of expandable tree) which when using just error doesn't appear.
If you want to compare the full response - you need to go for Response Assertion.
however it's very sensitive to any changes, a whitespace or line break will cause the failure.
JSON Assertion allows executing arbitrary Json Path queries, so if you want to assert additional-info element you should go for something like:
and this one is for result:
You are validating the expected results, so you don't fail
Check Invert assertion checkbox if you want it to fail (or change to success response)
Invert assertion (will fail if above conditions met) Invert assertion (will fail if above conditions met)

Conditionally Save responses to a file based on HTTP code or body string

I'm a newbie in using Jmeter and searching for a solution solving the following problem: I have a suite that contains a set of atomic HTTP requests and the goal is to save response bodies into an xml file. Currently, I'm handling that with using "Save Responses to a file listener", but need to be able to save separately error and OK responses in different XML, based on response HTTP code and body string.
to illustrate the case with dummy code
if (HTTP response code == 200 OK && body does not contain "ERROR") then
save response to file (%path%\responsename_OK.xml
else
save response to file (%path%\responsename_ERROR.xml
My bad, Nikolay, I forgot you wanted it in xml format...
Well, the easiest way is to add 2 Simple Data Writers (SDW) to each HTTP Request where 1 SDW logs only errors and the other logs only successes.
The Pro's are
it's easy and once you set it up for 1 HTTP Request, you can simply copy it to every other HTTP Request
Each success or failure with be appended to the corresponding .xml file
You can define as much or as little detail as you want
The Con is you add 2 elements to each Request, thus increasing your test size twice as much as you would with a BeanShell Assertion. (if that's a concern for you.)
If you want a more detailed way, you could try a BeanShell Assertion scripted with XML Parsers and XPath Assertions like outlined here.
Use Response Assertion (in fact you will need 2: one for Response Code and another one for Response Body checking) in order to fail the request basing on these criteria
Use If Controller for setting a condition:
${JMeterThread.last_sample_ok} will be triggered if previous sampler is successful
!${JMeterThread.last_sample_ok} - will fire if previous sampler fails
N.B. you should have any Sampler under the If Controller so it could work.

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

Want to check for assertion failure in Jmeter

I am a beginner in JMeter. I want to check for Assertion Failures in my script. I want to continue my transaction for a number of iterations after a single log in attempt, and I want to log out only if an error occurs. For that, I want to check if an error occurred in the script.
Is it possible by comparing assertions in JMeter?
If not, is there are any other way to find that?
Define first indicator(s) that will mark response as erratic (Response Code, keyword, etc.).
Try to use Response Assertion to handle state of your request (success/failure) depending on indicators above 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 e.g.:
ThreadGroup
LOGIN REQUEST
...
YOUR HTTP REQUEST HERE
Response Assertion
Response Field to Test: Response Code
Pattern Matching Rules: NOT Equals
Patterns to Test: 200
Regex Extractor
IfController
Condition: ${JMeterThread.last_sample_ok} // will be TRUE if Response Assertion above is TRUE (i.e. response code != 200)
LOGOUT REQUEST
...
In addition to the answer above I would recommend you:
Add error checking to your script - assertions that the responses are
valid for a given reques
Use Firebug to view network traffic when you need to debug your test
script
Use a regular expression extractor to retrieve a dynamic value from a
response and re-use it in a later request
To get the idea you can follow JMeter error checking video tutorial.
FYI: Assertion details provided.
Hope this helps.

Resources