Global functions / scripts for use in Test scripts

There’s some standard utility code that we need to use over and over again in our tests. It would be nice to put these functions in some global library in postman. It seems that the collection level “Pre-request Scripts” or “Tests” may allow this but I haven’t figured out how yet. Is there documentation somewhere on the postman site that I can reference for this?

Hi @kevinmmd15

Welcome to the community :wave:

The below resource should help you in re-using Pre-scripts for the entire collection:

https://learning.postman.com/docs/postman/scripts/pre-request-scripts/#re-using-pre-request-scripts

While regards to global libraries, I found out few requests on similar lines on GitHub and below are the links that point to it:

Feel free to upvote the post and add any additional comments that would help the team consider them. Thank you :slight_smile:

Hi @kevinmmd15

I have recently created a tutorial about this that may be helpful.

2 Likes

Hi all,

how to verify if my pre-script is working fine i am using put request to update the changes and then needed those changes for next api ?

const url = pm.environment.get('RESOURCE_PREFIX')
const lockid = pm.environment.get('gid_5')
const unlocker = pm.environment.get('s_Uker')
const tok = pm.environment.get('ACCESS_TOKEN')
pm.sendRequest({

      url: pm.environment.get('RESOURCE_PREFIX') + "/user/lock/lockid/unlocker/unset",
    method: 'PUT',
header:{
'Accept':'application/json', 'Content-Type':'Content-Type:application/x-www-form-urlencoded',
"key":"Authorization","value":"Bearer **tok**","enabled":true
  }
});
(function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

Hello,

I was intending to write a function for logging in, in the PRE-REQ section.
When I run the pm.sendrequest as Pre- Req section it gives the same error, when I write that as a function or set it in a global/environment variable I get an error.

Function declaration in the Preq tab –

//try one
pm.environment.set(“Login”, function Login() {

pm.sendRequest({

    url:  pm.environment.get("url")+"/api/systems/"+pm.globals.get("ID")+"/login", 

    method: 'POST',

    header: {

        'Accept': 'application/json',

        'Content-Type': 'application/json',

        'UserID': 'admin',

        'DomainLogin': '',

        'ClientTime': '2020-10-28T07:29:19.724+10:00',

        'MessageDetails': '{"QueryParameters":[{"Key":"Version","Value":"1004.9.2.372"},{"Key":"Device","Value":"Windows (windows-10)"},{"Key":"Browser","Value":"Chrome (86.0.4240.75)"},{"Key":"ClientTime","Value":"2020-10-28T07:29:19.723+10:00"}],"SortOrder":[],"Filters":[]}',  

        'X-XSS-Protection': '1', 

        'Password': '7878',

        'Connection': 'keep-alive'

        }

}, function (err, res) {

    

        pm.globals.set("Authorization", res.json().AuthToken);

        console.log(pm.globals.get("Authorization"));

});

} + ‘; Login();’);

//second try
globalThis.Log = function(){

pm.sendRequest({

    url:  pm.environment.get("url")+"/api/systems/"+pm.globals.get("ID")+"/login", 

    method: 'POST',

    header: {

        'Accept': 'application/json',

        'Content-Type': 'application/json',

        'UserID': 'admin',

        'DomainLogin': '',

        'ClientTime': '2020-10-28T07:29:19.724+10:00',

        'MessageDetails': '{"QueryParameters":[{"Key":"Version","Value":"1004.9.2.372"},{"Key":"Device","Value":"Windows (windows-10)"},{"Key":"Browser","Value":"Chrome (86.0.4240.75)"},{"Key":"ClientTime","Value":"2020-10-28T07:29:19.723+10:00"}],"SortOrder":[],"Filters":[]}',  

        'X-XSS-Protection': '1', 

        'Password': '7878',

        'Connection': 'keep-alive'

        }

}, function (err, res) {

    

        pm.globals.set("Authorization", res.json().AuthToken);

        console.log(pm.globals.get("Authorization"));

});

}

Preq section of the API–

//First try as an environment Var

eval(pm.environment.get(“Login”));

//second try as function

Log();

ERROR::
Error details are currently disabled.
To enable it, please set TraceConfiguration.DisplayErrorTraces to true.
For example by overriding your Bootstrapper’s Configure method and calling
environment.Tracing(enabled: false, displayErrorTraces: true);."}