Create variable from Collection Pre-request script level

Hi,

I created some function for creating variables from json file. It works that it iterate thru json file and if find key-value pair then creating a variable. Now I want to move this function to pre-request scripts of my collection, that i can use this function in all request in that collection. But when I move this function to Pre-request script of my collection, and the try to invoke it e.g. in tests of some request in this collection, in console I can see that this function works, but at the end no variables was created. Can I somehow create variables from Pre-request script of my collection?

For example I have this function in my pre-request script of the collection:
createVar = {

myFunc: function createVar()

{

    pm.globals.set("variable", "variable");

}

}
and in tests of some request I add createVar.myFunc() and that not create any variables.

Hey @Rafal_Moscibrocki :wave: Welcome to the Postman Community :tada:

In order to be able to access a function defined in pre-request/test script from somewhere else, you would need to store it as a variable.

So in one of your pre-request/test scripts, you can define and set as a variable, e.g.

const createVar = () => {
    pm.globals.set('variable', '12345')
}

pm.globals.set('createVar', String(createVar))

Then from elsewhere, you can invoke it using eval()

const createVarCall = eval(pm.globals.get('createVar'))
createVarCall()

Hope this helps! :smiley:

Hi @taehoshino Thanks for nice welcome :smiley:

I tried to use your way, but I think I can’t use it in my functions.

Look, I created two function one is to iterate through json file, and if it is at the “end” and this path is in array with paths to fields which I want to test, run function to create variable and create variable:

checkFile = {

myFunc: function checkFile(file, path, arrayWithPath)

{

    path = path || 0;

    var mainPath = path;

    for(key in file)

    {

        if(mainPath === 0)

        {

            path = key;

        }

        else

        {

            path = mainPath + "." + key;

        }

        if(typeof file[key] != 'object')

        {

            if(arrayWithPath.includes(path))

            {

                createVariable.myFunc(path, file[key]);

            }

        }

        else

        {

            if(Object.prototype.toString.call(file[key]).match(/\[\w+ (\w+)\]/)[1].toLowerCase() === 'array')

            {

                var array = file[key];

                for(i = 0; i < array.length; i++)

                {

                    if(typeof array[i] === 'object')

                    {

                        checkFile(array[i], path, arrayWithPath);

                    }

                    else

                    {

                        if(arrayWithPath.includes(path))

                        {

                            createVariable(path, array[i]);

                        }

                    }

                }

            }

            else

            {

                checkFile(file[key], path, arrayWithPath);

            }

        }

    }

}

}

createVariable = {

myFunc: function createVariable(variableName, variableValue)

{

    var variable = pm.globals.get(`${variableName}`);

    if (typeof variable == 'undefined')

    {

        var variable = new Array();

        variable.push(variableValue);

        pm.globals.set(`${variableName}`, variable);

    }

    else

    {

        variable.push(variableValue);

        let finallVariable = [...new Set(variable)];

        pm.globals.set(`${variableName}`, finallVariable);

    }

}

}

My idea to use it is that I paste this

checkFile.myFunc(response,0,arrayWithPath)

into test section in my request and in response I get json, and using this function I iterate thru this json i create variables for paths which I interested.

When this functions are in Pre-request this variables isn’t created, its work only when this two functions are in tests sections.

And I have no idea why it’s not working, because I can create variables from the prerequests :thinking:

@taehoshino it working! :tada: :muscle:

your way is perfect :clap: at the beginning I created variable only from my first function, to do this working I need to create variable from both functions and then in test run only the first one to check file like you write and everything works great.

Thanks for help, without this I wouldn’t do this!

1 Like