0
votes

I have this kind of request

{ 
"ID":"112345"
"SERVICE":"56AA77"
}

The API response could be positive or negative with different element.

positive response:

{
"RESPONSE":"OK"
"TEXT":"DONE"
}

negative response:

{
"RESPONSE":"KO"
"DESCRIPTION":"NOT FOUND"
"ERROR_CODE":"100"
}

how can I tested with JMeter using only one HTTP Request Sampler and only one CSV file ?

Actually I'm using two csv files:

positive:
TEST_ID,TEST_DESC,ID,SERVICE,RESPONSE,TEXT

negative:
TEST_ID,TEST_DESC,ID,SERVICE,RESPONSE,DESCRIPTION,ERROR_CODE

is it possible to use only one file ? like this:

TEST_ID,TEST_DESC,ID,SERVICE,RESPONSE,TEXT,DESCRIPTION,ERROR_CODE

how Jmeter can handle this?

UPDATE:

I've two JSON Assertion objects Positive JSON check

Negative JSON check

is it possible to create BeanShell groovy to call one of those based on the ResponseCode ?

if (prev.getResponseCode().equals("200") == true) { 

checkResponsePositive

}
else
{
checkResponseNegative
}

could someone help me with the right syntax?

2

2 Answers

1
votes
  1. Add JSR223 Assertion as a child of the HTTP Request sampler
  2. Put the following code into "Script" area:

    def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
    
    if (vars.get('RESPONSE').equals('OK')) {
        if (!vars.get('TEXT').equals(response.TEXT)) {
            AssertionResult.setFailure(true)
            AssertionResult.setFailureMessage('TEXT field mismatch')
        }
    
    } else {
        if (!vars.get('DESCRIPTION').equals(response.DESCRIPTION)) {
            AssertionResult.setFailure(true)
            AssertionResult.setFailureMessage('DESCRIPTION field mismatch')
    
        }
        if (!vars.get('ERROR_CODE').equals(response.ERROR_CODE)) {
            AssertionResult.setFailure(true)
            AssertionResult.setFailureMessage('ERROR_CODE field mismatch')
        }
    }
    
  3. The above code will have 2 branches:

    • when RESPONSE JMeter Variable is OK it will validate TEXT
    • when RESPONSE JMeter Variable is KO it will validate DESCRIPTION and ERROR_CODE from the response against the relevant JMeter Variables.

More information:

1
votes

Yes it is possible. So, I am assuming you have used sample variables and used them in regular extractor or any extractor or may be using coding. Now, if you used it then it can check the sampler response and write whatever values it may find. For positive they may not find anything, so may write "XXX_NOTFOUND" or null depends on the data extraction approach and code.

In the below, I have used dummy sampler for positive request and put all the regular extractor below it.

enter image description here

Once you check the csv report generated via view result tree, you can see that variables which are found has been reported and others as "xxxxx_NotFound". This "xxxxx_NotFound" is the value I have provided in the regular expression extractor. In short, for a particular request, you can use multiple extractor and write the output in a single csv. If values found then it will write else it will write what you have given in the default.

enter image description here