1
votes

I am trying to explore Jmeter for API testing.

I am able to add Assert using response assertion of Jmeter.

But I like a platform in Jmeter where we can write our code to assert more complex scenario of testing API tests on API.

Similar as we can say we do use of groovy scripts in SOAP-UI or javascript in postman tests using Newman node module.

Is it possible in Jmeter?

Is there any plug-in available to achieve same in Jmeter.

I have tried to found out any tutorial or blog regarding same but land with no success.

If anyone has tried then please do share your experience, way, blog or tutorials which can lead me to achieve same in Jmeter that would really help.

Any workaround will be helpful and appreciated in advance!!!

3

3 Answers

3
votes

Of course you can execute Javascript (on server side) to assert API response

You just need to add JSR223 Assertion as a Assertion to your request and choose Language as javascript and write your assertion code by checking prev which is a SampleResult

Here a demo that get response search for string success and log the first position in string using javascript:

var response = prev.getResponseDataAsString();
var pos = response.search("success");
log.info(pos);
1
votes

The options are in:

  1. JSON Path Assertion where you can use JSONPath queries in order to test the response
  2. If you need more advanced logic you can go for JSR223 Assertion and Groovy language. Groovy has built-in JSON support so you will be able to apply any custom logic. More information: Scripting JMeter Assertions in Groovy - A Tutorial
0
votes

continuing the user7294900 answer you can write code something like below in JSR223 Assertion:

JAVASCRIPT

var responseBody = prev.getResponseDataAsString();
log.info(responseBody)
log.info(responseBody.code)

var jsonData = JSON.parse(responseBody);
log.info("my name from response = "+jsonData.name)

I have found Jmeter have inbuild function of Assert that is AssertionResult

Groovy

Use code as below:

import groovy.json.JsonSlurper;

def failureMessage = "";
def jsonResponse = null;

JsonSlurper JSON = new JsonSlurper ();

try {
    jsonResponse = JSON.parseText(prev.getResponseDataAsString());
    } catch (Exception e) {
    failureMessage += "Invalid JSON.\n"
}

log.info("***********Starting Assert************")
log.info("******************************************************")
log.info("my name ="+jsonResponse.name)

if(!"201".equals(prev.getResponseCode())){
    failureMessage += "Expected <response code> [201] but we got instead [" + prev.getResponseCode() + "]\n\n" ;
}

if(!"morpheus".equals(jsonResponse.name)){
    failureMessage += "Expected name is morpheus but we got instead [" + jsonResponse.name + "]\n\n" ;
    log.info("asset fail")
}

if(!"morpheus2".equals(jsonResponse.name)){
    failureMessage += "Expected name is morpheus2 but we got instead  [" + jsonResponse.name + "]\n\n" ;
    log.info("asset fail")
}

if(!"leader".equals(jsonResponse.job)){
    failureMessage += "Expected job is leader but we got instead [" + jsonResponse.job + "]\n\n" ;
    log.info("asset fail")
}

if(!"leader1".equals(jsonResponse.job)){
    failureMessage += "Expected job is leader1 but we got instead [" + jsonResponse.job + "]\n\n" ;
    log.info("asset fail")
}

// Print error messages if any
if (failureMessage?.trim()) {
    failureMessage += "URL: " + SampleResult.getURL() + "\n\n";     
    failureMessage += "JSON RESPONSE: " + jsonResponse + "\n\n";
    failureMessage += "REQUEST HEADERS: " + SampleResult.getRequestHeaders() + "\n\n";

    AssertionResult.setFailureMessage(failureMessage);
    AssertionResult.setFailure(true);    
}

Source:

https://www.blazemeter.com/blog/scripting-jmeter-assertions-in-groovy-a-tutorial