My testcase is working fine for if condition but at same time it also try to validate with other set of data which is not required in if condition instead of going into else part of the code.
var cseresponsedata = JSON.parse(responseBody);
pm.test('Number of Objects = ’ + cseresponsedata.data.objects.length, function () {
});
for(var objcount = 0; objcount < cseresponsedata.data.objects.length; objcount++ ) {
pm.test('Number of signals = ’ + cseresponsedata.data.objects[objcount].signals.length, function () {
});
for(var signal = 0; signal < cseresponsedata.data.objects[objcount].signals.length; signal++){
for(var al_rec = 0; al_rec < cseresponsedata.data.objects[objcount].signals[signal].allRecords.length; al_rec++){
var cseresID = cseresponsedata.data.objects[objcount].signals[signal].allRecords[al_rec].fields.resource_id
pm.test("To check Alert Resource ID matched with signal resource id. " , function () {
if(pm.expect(cseresID).to.equal((pm.environment.get("resource_id"))))
{
description = cseresponsedata.data.objects[objcount].signals[signal].allRecords[al_rec].fields.description
pm.test("Alert description matched with CSE field description.", function () {
pm.expect(description).to.equal((pm.environment.get("description")));
});
alert_type = cseresponsedata.data.objects[objcount].signals[signal].allRecords[al_rec].fields.alert_type
pm.test("Alert type detail matched with CSE alert type.", function () {
var type_desc = (pm.environment.get("a_type"));
pm.expect('e'+alert_type).to.equal(type_desc);
});
severity_cse = cseresponsedata.data.objects[objcount].signals[signal].allRecords[al_rec].fields.severity
pm.test("Severity score matched with CSE severity score.", function () {
pm.expect(severity_cse).to.equal((pm.environment.get("severity")));
});
score_cse = cseresponsedata.data.objects[objcount].signals[signal].allRecords[al_rec].fields.score
pm.test("Alert score matched with CSE alert score.", function () {
pm.expect(score_cse).to.equal((pm.environment.get("score")));
});
insightid = cseresponsedata.data.objects[objcount].readableId
postman.setEnvironmentVariable("Insight_ID", insightid);
signalid = cseresponsedata.data.objects[objcount].signals[signal].id
postman.setEnvironmentVariable("Signal_ID", signalid);
postman.setNextRequest(null);
}
/*else{
pm.response.to.have.status(201);
pm.expect.fail('This failed because ...');
}*/
else
{
console.log("ELSE BLOCK");
pm.test("Check resid match or not:",function(){
pm.expect(cseresID).to.equal((pm.environment.get("resource_id")));
})
}
});
}
}
Response of the code
Number of Objects = 10
PASS
Number of signals = 1
PASS
Alert description matched with CSE field description.
PASS
Alert type detail matched with CSE alert type.
PASS
Severity score matched with CSE severity score.
PASS
Alert score matched with CSE alert score.
PASS
To check Alert Resource ID matched with signal resource id.
PASS
Number of signals = 1
FAIL
To check Alert Resource ID matched with signal resource id. | AssertionError: expected ‘212-1’ to equal ‘214-1’
PASS
Number of signals = 2
FAIL
To check Alert Resource ID matched with signal resource id. | AssertionError: expected ‘211-1’ to equal ‘214-1’
FAIL
To check Alert Resource ID matched with signal resource id. | AssertionError: expected ‘210-1’ to equal ‘214-1’
PASS
Number of signals = 1
FAIL
To check Alert Resource ID matched with signal resource id. | AssertionError: expected ‘156-1’ to equal ‘214-1’