This page is mainly discussing about the different function that the Javascript is supporting in-order to work with of the response of a call. The functions are an advance and help our validation super simple to iterate and reach to the values that we are looking for in the response. we will discuss all these advance scripting through example using an open or public API
Includes() – String.includes(sequence)
This Js function is used to check that a full or partial sequence of string is present or not. The function takes two values source.includes(targetSequence). Please refer to the following example
var isTheSequencePresent=false;
pm.test("Checkin the string- [postman, javascript] is present or not" ,function(){
if(("postman, javascript").includes("java")){
isTheSequencePresent=true
}
if(!isTheSequencePresent){
pm.expect.fail("FAILD: - The expected string sequence not present");
}
})
Loop – _.each(object,Instant)
This is a one of the loop statement that Js support to iterate through a object of array and access each value and working with the values for any kind of computation. The application of _.each will be presented as follow through example
var cityList = [
"addis",
"dire",
"torino"
]
var targetCity= "canada";
var isTheCityPresent=false
pm.test("Check the country is present or not" ,function(){
_.each(cityList, (eachCity)=>{
if(eachCity == targetCity){
isTheCityPresent=true;
}
})
if(!isTheCityPresent){
pm.expect.fail("FAILD: - The expected city/country not present");
}
})
hasOwnProperty
This is a function used to check an array of object contains a certain property or not. It returns a boolean false or true. True refers that the property is present in an array of object , false the property is not present.
var employeeList = [
{
name:"Abebe",
profession:"Engineer"
},
{
name:"Lele",
profession:"Nurse",
salary:50000
}
]
var isAllEmployeeHasSalary=true;
pm.test("Check that the Field 'Salary' Present or Not" ,function(){
_.each(employeeList, (eachEmployee)=>{
console.log(eachEmployee.hasOwnProperty('name'))
if(!eachEmployee.hasOwnProperty('name')){
isAllEmployeeHasSalary=false;
}
})
if(!isAllEmployeeHasSalary){
pm.expect.fail("FAILD: - Not all employeeds have salary");
}
})
User Defined Functions
Functions are useful for setting up instruction to execute and let the accessibility of such computation easy to use it across our program. Using function let us to re-use the same functionality across the program, as well as avoid repeating the same kind of function to be redundant across our program that could help to achieve reusability.
Function has got a name, return type, as well as parameters. The name of the function can be any stated using different naming conventions such as Camel, Pascal or Snake and so forth. The return type of the function can be any type, interfere, float, string, and Parameters are useful to pass value/argument for the function to take or consider those inputs in the computation.
Syntax : -
function function_name(paarsers){
//Function Body
}
Example : -
function checkNameLength(name){
if(name.length>0)return true;
return false;
}
Calling : -
console.log(checkNameLength("Testing code"))