I am new to jmeter and would like to know if it is possible to call a custom assertion message from a central location.
Thread group 1
http sample
xpath extraction
custom assertion message
Thread group 2
http sample 1
xpath extraction
custom assertion message
http sample 2
xpath extraction
custom assertion message
The custom message use the xpath extraction to get the response code, if the success message code returned then the custom message is ignored.
If the response code is not a success response, we extract the actual response code and use in the custom assertion message.
I would like to call the custom message from one central location instead.
The custom assertion is below:
if (!"SuccessResponseCode".equals(vars.get("xpathResponseCode")) && prev_result != null){
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage("The response code is "+vars.get("xpathextractionResponseCode").toString()+" and the expected is expectedResponseCode"+ctx.getThreadGroup().getName()+" test case.");
}
So I would like to call an external custom message
Custom assertion
Thread group 2
http sample 1
xpath extraction
JS232 - call custom message
http sample 2
xpath extraction
JS232 - call custom message
Just put your Assertion at the same level as Thread Groups.
Assertions follow JMeter Scoping Rules so this way a single assertion will be applied to all samplers in its scope:
Related
I am trying to get the error message from the network tab in JMeter:
I have inserted the response assertion looking for a 200 or 201. However, when a test fails, it only tells me it is looking for one of the values, but not a message like in the response section of the network tab. For example, if it is a bad request, I see the 400, but no message like "uid cannot be empty, name cannot be empty". Is there a way to pull this information in JMeter?
With "normal" Response Assertion you cannot, if you want to apply some custom logic you need to perform some custom scripting.
For example if you want to add response body to the Assertion message you can play the following trick:
Add JSR223 Assertion after the Response Assertion
Put the following code into "Script" area:
prev.getAssertionResults().each { assertionResult ->
if (assertionResult.isFailure()) {
assertionResult.setFailureMessage(assertionResult.getFailureMessage() +
System.getProperty('line.separator') +
'Response data:' +
System.getProperty('line.separator') +
prev.getResponseDataAsString())
}
}
That's it, the above code will add the response data to any failing assertion, hopefully this is what you're looking for.
Going forward you might want to migrate all the assertion logic into the JSR223 Assertion and Groovy, see Scripting JMeter Assertions in Groovy - A Tutorial article for more details.
I want a common Response assertion for 200 response code which should be applicable for each sampler request.
However it is also required for some sampler requests to overwrite that from sampler level as (response code 204,500).
Is there any possible way to achieve this?
JMeter Assertions obey Scoping Rules so if you put an Assertion on the same level as Samplers - it will be applied to all samplers
If you need to override expected response code for a specific sampler I would recommend changing the response code to 200 via JSR223 PostProcessor like:
if (prev.getResponseCode().equals('500')) {
prev.setResponseCodeOK()
}
this way the above Groovy code will change the individual sampler status code from 500 to 200 so "global" assertion will still be successful.
In Response Assertion you can add more Pattern To Test, so add 3: 200,204,500
And check the Or checkbox that you will allow to assert either one.
Note: Field to check is Response Code
Note: Pattern Matching rule can be Equals or Contains
I'm using a jmeter script with while controller condition as
${__javaScript("${status}"=="Progress")}
Followed by one http request and from that request again I will extract the status.
If the status is progress it will contine one http request inside loop else if the status success it will exit the loop. But i want to add assertion to ensure whether I get the status as success in the http request or not.
Because if i use assertion as Success in that http sampler, it checks all the reqest and obviously it will return false, and i need to add assertion only to the final http sampler of the while controller.
And so I can get error percentage with failing request in aggregate report and view results tree . Can anyone share your ideas please.
I would recommend using Response Assertion to verify if ${status} variable value is Progress OR Success
Add Response Assertion as a child of the HTTP Request sampler
Configure it as follows:
Apply to: -> JMeter Variable -> status
Pattern Matching Rules: Matches
Patterns to Test: Progress|Success
Given you use "Matches" rule JMeter will treat the pattern as a Perl5-style regular expression so if ${status} variable will be equal to Success or Progress the sampler will pass and otherwise it will be marked as failed.
See How to Use JMeter Assertions in Three Easy Steps article to learn more about using Assertions in Jmeter tests.
I want the request to pass in both the cases if response contains "Completed" or "Progress, Will take time to process".
But if I include both this assertions in response assertion, it will take it as 'and'. It will pass only if both are satisfied.
Here any one of this is sufficient. Please suggest.
You will need to go for an assertion which supports scripting, i.e. Beanshell Assertion
Add Beanshell Assertion as a child of the request which returns either "Completed" or "Progress" messages
Put the following code into "Script" area:
String response = new String(ResponseData);
Failure = !(response.contains("Completed") || response.contains("Progress, Will take time to process"));
Where:
ResponseData - byte array which holds parent sampler response
Failure - boolean which indicates whether parent sampler should be failed or not.
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on how to use JMeter and Java API from Beanshell test elements and extend your JMeter tests with scripting.
Jmeter 3.2 allow to check using Or condition
Response Assertion now allows to work on Request Header, provides a "OR" combination
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.