Postman Advance Scripts

Collection Level Functions

This is a function that is going to called and perform the interceded action at the scope that the function is visible. From the definition Global means that the function can be accessed at a level of collection. The collection level function can be access in a different level of folders or sub-folders. The declaration should be at Pre-Request Level containing the following Syntax

Syntax : - 
Object.prototype.functionName = function functionName(arg1,arg2, arg3,....){
}

Example : -
Object.prototype.getLength = function getLength(valueToCheck){
return valueToCheck.lenght;
}

Calling Function : -
console.log(_.getLength("postman"));

Argument – As Object

This is a data structure that could let us to pass an argument as an Object This means passing an argument as one or more than one set of values can easily possible. To let a function to support such passing of values, The argument has to be declared as function. The detail of the declaration is as follow

Syntax : -
funciton functionName (...arrayArgument){
//Function Body
}

Example : - 
var nameLists= ["Alex","Germa","Tigist"];

function checkNamePresence( nameToCheck,...nameList){
console.log(nameList[0]); //  ["Alex", "Germa", "Tigist"]
   for(let name of nameList){
    if(name.includes(nameToCheck)) 
        return true;
   }
   return false;
}

Calling : - 
console.log(checkNamePresence("Germa",nameLists));

Argument – As Array

This is another form of passing value to a function as Array. It has the same purpose as the above one. The data structure a little bit different, and still there is a possibility to access each element of the array and work with.

Syntax : -
funciton functionName (arrayArgument){
//Function Body
}

Example : - 
var nameLists= ["Alex","Germa","Tigist"];

function isPersonNamePresent (nameTocheck, nameList){
     console.log(nameList);// ["Alex", "Germa", "Tigist"]
    for(name of nameList){
        if(name==nameTocheck) return true;
    }
    return false;
}

Calling : - 
console.log(isPersonNamePresent("Germa",nameLists));

Casting

This is a functionality that could change the value to intended data type. This could let to get a related function to that data type. There are different build-in function that could cast values. Some of are the following

NoBuilt-In FunctionDescriptions
1parseInt(Parsing an value to Integer
2partFloatParsing a value to Floating
There are many other built-in function that could let us to Parse values.
Syntax : - 
partInt(value);
parseFloat(value);

Example : - 
var age ="45"
var salary = "2345.76"

console.log(parseInt(age)); // 45
console.log(parseFloat(salary)); // 2345.76

Math

Javascript supports a lot of Math related functions that could help us to work with numbers to reach the expected result we want to achieve. Some of the know function are the following

  • round This will round decimal digits to their approximate value.
  • getMax of Array
  • Many more
utilFunctionMaths : {

/** A Funciton rounding a target number  */
    getRoundNumber : function (targetNumber){
        return Math.round(targetNumber);
    },
getMaxNumber: function(targetNumber){
         return Math.max.apply(null,targetNumber)
        
    }
}
/* Getting Round Number */
let number = 4.3;
console.log(getRoundNumber(number));

/*Get the Max Number from Array */
const numberList=[4,3,20,55];
console.log(getMaxNumber(numberList));
Scroll to Top