I have collection tests logic, when its 403, the testscript level testcases are getting executed after the collectionlevel, I do not want to run the request level tests if the statuscode is 403,only collection level condition should execute.
code snippet
const currentRequestId = pm.info.requestId;
console.log(currentRequestId);
pm.collectionVariables.set(“previous_request”, currentRequestId);
const authHeader = pm.request.headers.get(“Authorization”);
console.log(“tests —1”)
if (responseCode.code == 403 && responseBody.includes(“test”)) {
pm.test(“collection Level Tests”, function () {
pm.sendRequest({
url: url,
method: ‘PUT’,
header: {
‘Accept’: ‘application/json’,
‘Content-Type’: ‘application/json’,
‘Authorization’: authHeader
},
body: {
mode: ‘raw’,
raw: body
}
}, function (err, res) {
console.log(res);
if (res.code == 204) {
console.log(res.code)
postman.setNextRequest(currentRequestId)
}
});
});
}
I really don’t recommend putting conditional formatting on tests.
You need to be in control of your test data. It needs to return the same result each and every time and you base your tests on those results.
If you want to test difference response codes, then you ensure that you have test data that will illicit that response. (Which means you might have more than one request to the same end point to test out all of these scenarios).
I’m not really sure what you are trying to do here.
It looks like if you get a “forbidden” status code, then you are sending a PUT request before running the same request again? Is this because you are not in control of the current status of your test data? In which case, you can use a pre-request script for that request to ensure its in the correct state.
I can’t see where you are setting “responseCode” or “responseBody”.
On a side note, you have wrapped your code in a test, but don’t actually have any assertions (pm.expect). So as far as I can tell, that test will always pass if its triggered. What are you actually trying to test at this point? “Collection Level Tests” doesn’t tell me much.
My recommendation is to take a step back and write down the steps you want your code to achieve. Once you have the steps, start filling it it with code. It will also help anyone else in trying to understand what you are trying to do and with troubleshooting as I’m not really sure what you are trying to achieve here.
Tip: If you are concerned about the status of test data, please use pre-request scripts to check it first, rather than relying on post request scripts to clean up after themselves. You can use sendRequest() for this purpose. Your request\test should be self contained and not rely on the arbitrary execution of another request\test unless you are creating a work flow.