2

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!!!

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125

3 Answers3

3

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);
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • thats great can you please share sample code or blog which leads me an idea that how to achieve same – Shubham Jain Jan 25 '18 at 07:24
  • Thanks it is move me ahead for my current task. can you suggest a better way to assert in same. means in Jmeter which can also refect on report – Shubham Jain Jan 25 '18 at 08:11
  • Is it possible to have an example that actually asserts something? Or maybe just a link to the reference in Javascript? Thanks a lot – eMarine Feb 25 '19 at 22:09
1

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

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
1

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
Dmitri T
  • 159,985
  • 5
  • 83
  • 133