Skip to content

Instantly share code, notes, and snippets.

@swateek
Last active December 7, 2023 23:09
Show Gist options
  • Save swateek/6d35e534226f2240250916d578acab71 to your computer and use it in GitHub Desktop.
Save swateek/6d35e534226f2240250916d578acab71 to your computer and use it in GitHub Desktop.
All POSTMAN functions that can be used to run tests
// pm.globals.clear(); // Be careful with this usage
// Save common tests in a global variable
postman.setGlobalVariable("commonTests", () => {
pm.test("Common Tests - Response Time", function(){
pm.expect(pm.response.responseTime).is.lessThan(500);
});
pm.test("Common Tests - Content Type", function(){
pm.expect(pm.response.headers.get('Content-Type')).equals('application/json');
});
});
// Cleanup
postman.setGlobalVariable("tmp_cleanup", () => {
const clean = _.keys(pm.environment.toObject())
_.each(clean, (arrItem) => {
if (arrItem.startsWith("tmp_")) { // remove all env variables starting with "tmp_"
pm.environment.unset(arrItem);
}
});
pm.collectionVariables.clear();
});
// Unsetting a value for variable/collection/environment variable
postman.setGlobalVariable("unsetKeyForVarType", (varType, key) => {
if(varType == "VAR"){
return pm.variables.unset(key);
}else if(varType == "COLL"){
return pm.collectionVariables.unset(key);
}else{
return pm.environment.unset(key);
}
});
// Setting a value for variable/collection/environment variable
postman.setGlobalVariable("setKeyForVarType", (varType, key, currVal) => {
if(varType == "VAR"){
return pm.variables.set(key, currVal);
}else if(varType == "COLL"){
return pm.collectionVariables.set(key, currVal);
}else{
return pm.environment.set(key, currVal);
}
});
// Fetching a value from variable/collection/environment variable
postman.setGlobalVariable("getValFromVarType", (varType, key) => {
if(varType == "VAR"){
return pm.variables.get(key);
}else if(varType == "COLL"){
return pm.collectionVariables.get(key);
}else{
return pm.environment.get(key);
}
});
// Add Id of created object to variable
postman.setGlobalVariable("addIdOfCreatedObject", (varType, key, val) => {
var currVal = eval(globals.getValFromVarType)(varType, key);
if(currVal == undefined){
currVal = val;
}else{
currVal = currVal + "," + val;
}
eval(globals.setKeyForVarType)(varType, key, currVal);
});
// Get single object from variable
postman.setGlobalVariable("getObjIdForKey", (varType, key) => {
var currVal = eval(globals.getValFromVarType)(varType, key);
if(currVal == undefined){
return false;
}
var currValArr = currVal.split(",");
return currValArr.shift();
});
// Remove Id of deleted object from variable
postman.setGlobalVariable("remIdOfDeletedObject", (varType, key, val) => {
var currVal = eval(globals.getValFromVarType)(varType, key);
if(currVal == undefined){
return false;
}
currValArr = currVal.split(",");
var index = currValArr.indexOf(val);
if (index !== -1) {
currValArr.splice(index, 1);
}
if(currValArr.length > 0){
eval(globals.setKeyForVarType)(varType, key, currValArr.toString());
}else{
eval(globals.unsetKeyForVarType)(varType, key);
}
});
// If you should execute more
postman.setGlobalVariable("ifExecMore", (varType, methodType, respCode, key, ctrKey) => {
var execMore = false;
var err = null;
var currVal = eval(globals.getValFromVarType)(varType, key);
var ctrVal = eval(globals.getValFromVarType)(varType, ctrKey);
if(currVal == undefined || ctrVal == undefined){
return [true, execMore];
}
if(parseInt(currVal) == NaN || parseInt(ctrVal) == NaN){
return [true, execMore];
}
var currValArr = currVal.split(",");
if(methodType == "POST"){
if(currValArr.length < ctrVal){
execMore = true;
}
if(execMore && (respCode < 300)){ // allowing 200, 201, etc.
execMore = true;
}else if (respCode > 300){
err = true;
execMore = false;
}else{
execMore = false;
}
}else if(methodType == "DELETE"){
if(currValArr.length > 0){
execMore = true;
}
if(execMore && (respCode < 300)){ // allowing 200, 201, etc.
execMore = true;
}else if (respCode > 300){
err = true;
execMore = false;
}else{
execMore = false;
}
}else{
console.error("Unsupported Method Type supplied to function");
execMore = false;
}
return [err, execMore];
});
eval(globals.tmp_cleanup)();
// tmp_cleanup
// In "TEST" part of POSTMAN request
eval(globals.tmp_cleanup)();
// commonTests
// In "TEST" part of POSTMAN request
eval(globals.commonTests)();
// addIdOfCreatedObject
// In "TEST" part of POSTMAN request
eval(globals.addIdOfCreatedObject)("VAR", "sample_created_ids", "newid1");
eval(globals.addIdOfCreatedObject)("COLL", "sample_created_ids", "newid1");
eval(globals.addIdOfCreatedObject)("ENV", "sample_created_ids", "newid1");
// remIdOfDeletedObject
// In "TEST" part of POSTMAN request
eval(globals.remIdOfDeletedObject)("VAR", "sample_created_ids");
eval(globals.remIdOfDeletedObject)("COLL", "sample_created_ids");
eval(globals.remIdOfDeletedObject)("ENV", "sample_created_ids");
// getObjIdForKey
// In "TEST" part of POSTMAN request
var id = eval(globals.getObjIdForKey)("VAR", "sample_created_ids");
var id = eval(globals.getObjIdForKey)("COLL", "sample_created_ids");
var id = eval(globals.getObjIdForKey)("ENV", "sample_created_ids");
// setKeyForVarType
// In "TEST"/"PREREQUEST" part of POSTMAN request
eval(globals.setKeyForVarType)("VAR", "sample_key", "sample_val");
eval(globals.setKeyForVarType)("COLL", "sample_key", "sample_val");
eval(globals.setKeyForVarType)("ENV", "sample_key", "sample_val");
// unsetKeyForVarType
// In "TEST"/"PREREQUEST" part of POSTMAN request
eval(globals.unsetKeyForVarType)("VAR", "sample_key");
eval(globals.unsetKeyForVarType)("COLL", "sample_key");
eval(globals.unsetKeyForVarType)("ENV", "sample_key");
// addIdOfCreatedObject
// In "TEST" part of POSTMAN request
eval(globals.addIdOfCreatedObject)("VAR", "sample_created_ids", "newid1");
eval(globals.addIdOfCreatedObject)("COLL", "sample_created_ids", "newid1");
eval(globals.addIdOfCreatedObject)("ENV", "sample_created_ids", "newid1");
// getValFromVarType
// In "TEST"/"PREREQUEST" part of POSTMAN request
eval(globals.getValFromVarType)("VAR", "sample_key");
eval(globals.getValFromVarType)("COLL", "sample_key");
eval(globals.getValFromVarType)("ENV", "sample_key");
// ifExecMore
// In "TEST" part of POSTMAN request - POST request
var ifExecMoreArr = eval(globals.ifExecMore)("COLL", "POST", pm.response.code, "tmp_ids_str", "tmp_total_objs");
if(ifExecMoreArr[0]){ // error = true
postman.setNextRequest(null); // stop execution
}else if(!ifExecMoreArr[0] && ifExecMoreArr[1]){ // error = null, execMore = true
postman.setNextRequest("Create New Object");
}else{ // error = null, execMore = false
// fall to next request
}
// In "TEST" part of POSTMAN request - DELETE request
var ifExecMoreArr = eval(globals.ifExecMore)("COLL", "DELETE", pm.response.code, "tmp_ids_str", "tmp_total_objs");
if(ifExecMoreArr[0]){ // error = true
postman.setNextRequest(null); // stop execution
}else if(!ifExecMoreArr[0] && ifExecMoreArr[1]){ // error = null, execMore = true
postman.setNextRequest("Delete New Object");
}else{ // error = null, execMore = false
// fall to next request
eval(globals.tmp_cleanup)(); // put this in the last request
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment