Iteration Data File vs. Dynamic Variables

Hi. Is there a way to know if a Collection run or a Single request run is using an iteration data file or not?

Basically, my use case is that if an iteration data file is being used, the test data will be sourced from that (JSON/CSV) data file. Otherwise, the test will use Dynamic Variables for the test data instead.

Currently, the way I do this is I define an environment variable ā€œuse_data_fileā€ flag and set it to true or false, which I then check in the script to control the logic.

Thank you for any suggestions.

Hi @randyroman,

Variables in Postman follow precedence as below
Lowest to highest.
global , collection , environment , data , and local

For example, if the environment variable and local variable is having the same variable name then the value of the local variable will be picked.

For more info, you can refer to the below documentation.

Hope this helps :slight_smile:

Thanks, @pranavdavar for the response. I understand the variable scoping. However, what I wanted to find out is if there is a way to know via a script if an iteration data file is being used in a particular run. Below is a snippet of the code to give more context:

if (pm.collectionVariables.get("use_datafile") === 'true') {
    console.log("Using data file!");
    pm.environment.set("firstname", pm.iterationData.get("first_name"));
    pm.environment.set("lastname", pm.iterationData.get("last_name"));
 else {
    console.log("Using dynamic variables!");
    pm.environment.set("firstname", pm.variables.replaceIn('{{$randomFirstName}}'));
    pm.environment.set("lastname", pm.variables.replaceIn('{{$randomLastName}}'));
}

I am using a collection variable ā€œuse_datafileā€ in the if condition, which I have to set manually each time I want to toggle between using a data file vs. dynamic variables for my test data. I was hoping there is a way to determine via script if a data file is in use so I could avoid this manual setup. :blush:

The reason why I am doing this is because a data file is not available when I am just running an individual request, and not the entire collection via postman UI or Newman. Hence I need to fallback to using dynamic variables. :sweat_smile:

Hi @randyroman,

You may use the postman function to check if the iteration variable exists or not using the below function.

console.log(pm.iterationData.has(ā€˜testā€™));

Instead of manually switching, you may use the above function, and the output of the function is boolean.

Hope this helps :slight_smile:

1 Like

Your script will look like

if (pm.iterationData.has("first_name")) {
    console.log("Using data file!");
    pm.environment.set("firstname", pm.iterationData.get("first_name"));
    pm.environment.set("lastname", pm.iterationData.get("last_name"));
 else {
    console.log("Using dynamic variables!");
    pm.environment.set("firstname", pm.variables.replaceIn('{{$randomFirstName}}'));
    pm.environment.set("lastname", pm.variables.replaceIn('{{$randomLastName}}'));
}
1 Like

Thank you @pranavdavar for the suggestion. This should work fine to address the problem I have. Cheers!

2 Likes