In my JMeter test plan, I added below as JSR223 Assertion to format the JSON response.
import groovy.json.JsonSlurper;
def failureMessage = "";
def jsonResponse = null;
def headers = null;
def responseTime = null;
JsonSlurper JSON = new JsonSlurper();
prev.setResponseData(groovy.json.JsonOutput.prettyPrint(prev.getResponseDataAsString()))
try {
jsonResponse = JSON.parseText(prev.getResponseDataAsString());
log.info("Wards response: "+jsonResponse);
headers = SampleResult.getRequestHeaders();
responseTime = SampleResult.getTime();
} catch (Exception e) {
failureMessage += "Invalid JSON.\n"
}
if (!"200".equals(prev.getResponseCode())) {
failureMessage += "Expected <response code> [200] but we got instead [" + prev.getResponseCode() + "]\n\n";
}
if (!"OK".equals(prev.getResponseMessage())) {
failureMessage += "Expected <response Message> [OK] but we got instead [" + prev.getResponseMessage() + "]\n\n";
}
if (!jsonResponse.keySet().containsAll(["wards"])) {
failureMessage += "The json config element has wrong structure. No wards key\n\n";
}
if (!jsonResponse.wards.getAt(0).keySet().containsAll(["id","name","shortName"])) {
failureMessage += "The wards element has wrong structure. Key(s) are missing or wrong \n\n";
}
if (!jsonResponse.wards.size==95) {
failureMessage += "Wards count is wrong..! :[" + jsonResponse.wards.size + "]\n\n";
}
if (!(headers.contains("ticketheader") && headers.contains("Content-Type") && headers.contains("Host") && headers.contains("User-Agent"))) {
failureMessage += "Headers are not correct..!";
}
if (failureMessage?.trim()) {
AssertionResult.setFailureMessage(failureMessage);
AssertionResult.setFailure(true);
}
// 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);
}
Then I added below Beanshell script to get the Assertion Test results thorough email body.
import org.apache.jmeter.assertions.AssertionResult;
AssertionResult[] results = prev.getAssertionResults();
StringBuilder body = new StringBuilder();
for (AssertionResult result : results) {
body.append(result.getFailureMessage());
body.append(System.getProperty("line.separator"));
}
vars.put("body", body.toString());
But in result email, I'm getting below error.
*javax.script.ScriptException: groovy.json.JsonException: Lexing failed on line: 1, column: 1, while reading 'j', no possible valid JSON value or punctuation could be recognized.*
In my test plan I need to use both scripts to format the JSON and send the email. How do I resolve this issue?
Your script is failing because you're not getting valid JSON, proper JSON object starts with
{
or[
and you're gettingj
, try using a service like JSONLint which will highlight all problematic places in your responseSince JMeter 3.1 you should not be using Beanshell so I believe you can put your Beanshell code to the JSR223 Assertion, it will work without changes.