Identify in Tests if running through Monitors or Manually triggered

Good day everyone,

In tests script, how do I know if the request was ran through the monitors or is called manually using the Postman app?

1 Like

I donโ€™t believe there is an easy way to do that, what are you trying to do with this information?

We want part of our test to run only when itโ€™s running thru the monitors.

My only thought here is setting a variable to drive the workflows.

If you create an environment variable called โ€˜isLocalRunโ€™ you can set the initial value to false and the current value to true.

image

In your tests, you can check the value of that variable to see how to branch your workflow. A monitor will always read the initial value, but running locally is going to read the current value.

This is possible by checking if your Postman script is running in AWS by checking the IP address using NodeJS.

/**
 * Checks if we are running in AWS. This is useful for reducing console output in Postman monitor.
 * @function in_aws
 * @return {Boolean}       True iff running on AWS server; False otherwise.
 */
function in_aws(){
    let ip_addr,
    in_aws = false;

    pm.sendRequest("http://api.ipify.org", (err,res) => {
        if(err){
            console.log("While trying to get IP address, got error: " + err.message);
        } else {
            ip_addr = res;
        }
    });

    const aws_ip_ranges = "https://ip-ranges.amazonaws.com/ip-ranges.json";
    // check if ip is in aws ip ranges
    pm.sendRequest(aws_ip_ranges, (err,res) => {
        if(err){
            console.log("While trying to get AWS IP ranges, got error: " + err.message);
        } else {
            const aws_ip_ranges = JSON.parse(res);
            for(let i = 0; i < aws_ip_ranges.prefixes.length; i++){
                if(ip_addr.includes(aws_ip_ranges.prefixes[i].ip_prefix)){
                    in_aws = true;
                    break;
                }
            }
        }
    });
    return in_aws;
}
Object.prototype.in_aws = in_aws; //When defined at a collection level, this makes in_aws available to all down-script contexts. https://stackoverflow.com/a/68998344/1757756