简体   繁体   中英

Postman Test script to set environment

I need help with Postman test script, when i make an API call to vRealize Automation, i get the response Body value different form previous deployment. the data will be in different array, its randomly change.

here is what i have in the test script:

var data = JSON.parse(responseBody);

var Array1 = data.content
for(var i = 0; i < Array1.length; i++){
console.log("" + data.content[i].data.ip_address);
}

the console will prints:

undefined
192.168.245.211
undefined

the deployment after, the condole prints:

192.168.245.212
undefined
undefined

and the last deployment :

undefined
undefined
192.168.245.213

so the value located either on data.content[0].data.ip_address or data.content[1].data.ip_address or data.content[2].data.ip_address

what I'm trying to do is:

1) I want to ignore undefined value if I decide to print it to the console. 2) I want to replace console.log with pm.environment.set to set the ip address no matter where is the value located on array 0,1 0r 2

You could use an if condition in the loop to check the items, if the item is not undefined, set that as the environment variable.

_.each(pm.response.json().content, (arrItem) {
    if(arrItem.data.ip_address !== undefined) {
        pm.environment.set("ip_address", arrItem.data.ip_address) 
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM