Best way to validate entire Json response in JMeter - jmeter

I would like to know best way to verify entire Json response in Jmeter. Can somebody help. Thanks.

The easiest way is use JSR223 Assertion and JsonSlurper class like:
Put the anticipated JSON into expected JMeter Variable using i.e. User Defined Variables test element like:
Add JSR223 Assertion as a child of the request you want to validate
Put the following code into "Script" area:
def expected = new groovy.json.JsonSlurper().parseText(vars.get('expected'))
def actual = new groovy.json.JsonSlurper().parse(prev.getResponseData())
if (expected != actual) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Mismatch between expected and actual JSON')
}
If there will be any difference between expected and actual JSON - the sampler will be marked as failed

Related

Is it possible to remove empty query string parameters in jMeter?

I want to test an endpoint using jmeter, that has a copule of query string parameters, one of which is optional, loading the values from a CSV file. The problem is, can I avoid sending the query string parameter if I don't have a value for it?
It is but it will require some Groovy coding
Add JSR223 PreProcessor as a child of the request which query string you want to modify (or according to JMeter Scoping Rules if you want to apply the approach to more than one request)
Put the following code into "Script" area:
def newData = new org.apache.jmeter.config.Arguments()
0.upto(sampler.getArguments().size() - 1, { idx ->
def arg = sampler.getArguments().getArgument(idx)
if (!arg.getValue().equals('')) {
newData.addArgument(arg)
}
})
sampler.setArguments(newData)
That's it, the PreProcessor will be executed before the HTTP Request sampler and will remove all arguments which don't have their respective values

Jmeter assertion result listener as variable

My JMeter test plan looks like this:
HTTP Request
- Assertion
HTTP Request
- Assertion
HTTP Request
- Assertion
Assertion Result Listener
I'd like to define all assertion results from the listener as a variable and use that variable in a POST call to JIRA, so the description contains an overview of all assertions and failure and pass of each assertion.
Assertion Result Listener
I know I can save the assertion results to a file and upload that, but I need the assertion results as text in the JIRA. Any ideas how I can do that?
edit: this is for a functional test suite.
Add JSR223 Listener to your Thread Group
Put the following code into "Script" area
def result = vars.get('result')
StringBuilder builder = new StringBuilder()
if (result != null) {
builder.append(result).append(System.getProperty('line.separator'))
}
prev.getAssertionResults().each { assertionResult ->
builder.append(prev.getSampleLabel()).append(System.getProperty('line.separator'))
if (assertionResult.isFailure()) {
builder.append('\t').append(assertionResult.getFailureMessage()).append(System.getProperty('line.separator'))
}
}
vars.put('result', builder.toString())
props.put('result', builder.toString())
Add tearDown Thread Group to your Test Plan
Refer the generated string holding the assertion results using __P() function as ${__P(result,)}
Demo:
See Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter.

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 Get Body Data before Sending

for example i have http body data like this
{
"signature" : "${signatureCreate}",
"paramA" : "1A02",
"paramB" : "aaa",
"paramC" : "asass"
}
how could i get all params (paramA, paramB, paramC) in my BeanShell Preprocessor? i have to get all these 3, encrypt it, and put it back in "signature" param
i also tries using JSR223 PreProcessor like this (only to try to get the paramA value, but still no luck)
def body = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
vars.put("signatureCreate", body.paramA);
thanks in advance
Your code should work fine (given you choose Groovy in the "Language" dropdown), just add Debug Sampler and View Results Tree listener to your Test Plan, you will be able to see signature variable value in the "Response Data" tab of the Debug Sampler
More information: How to Debug your Apache JMeter Script

How to change child sample result to successful in jmeter?

I've made login process with the help of jmeter. In of of request samplers I'm expecting to get response code "401". I've added BeanShell Assertion
if (ResponseCode.equals("401") == true) {
SampleResult.setResponseOK();
SampleResult.setSuccessful(true);
}
And my Results Tree is looking like this now.
My question is - what i need to add to BeanShell in order to make child of the second sample green (passed) as well as it's parent sample?
The easiest way is using Response Assertion configured like:
If you are still looking for Beanshell solution - you need to process all sub-results along with the main result so you should amend your code like:
import org.apache.jmeter.samplers.SampleResult;
//process main sample
if (SampleResult.getResponseCode().equals("401")) {
SampleResult.setResponseCodeOK();
SampleResult.setSuccessful(true);
}
//process all subsamples
for (SampleResult subResult : SampleResult.getSubResults()){
if (subResult.getResponseCode().equals("401")){
subResult.setResponseCodeOK();
subResult.setSuccessful(true);
}
}
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using Beanshell in JMeter test scripts.

Resources