3--Postman--变量(environment&global)

(1) Environment

clear an environment variable:
pm.environment.unset("variable_key")--recommend
postman.clearEnvironmentVariable(variableName)
postman.clearEnvironmentVariables()
 
get an environment variable:
pm.environment.get("variable_key")--recommend
postman.getEnvironmentVariable(variableName)
 
set an environment variable:
pm.environment.set("variable_key", "variable_value")--recommend
postman.setEnvironmentVariable(variableName, variableValue)--string
 
(2) Global variables
 
clear a global varibale:
pm.globals.unset("variable_key")
postman.clearGlobalVariable(variableName)
postman.clearGlobalVariables()
 
set a global variable:
pm.globals.set("variable_key", "variable_value");
postman.setGlobalVariable(variableName, variableValue)
 
get a global variable:
pm.globals.get("variable_key")
postman.getGlobalVariable(variableName)
 
(3) Json & string 
JSON.parse()【从一个字符串中解析出json对象】
例子:
//定义一个字符串
var data='{"name":"goatling"}'
//解析对象
JSON.parse(data)
结果是:
name:"goatling"
JSON.stringify()【从一个对象中解析出字符串】
var data={name:'goatling'}
JSON.stringify(data)
结果是:
'{"name":"goatling"}'
 
Getting an environment variable (whose value is a stringified object):Json
Setting a nested object as an environment variable:String

(4) Get a variable

This function searches for the variable across globals and the active environment.

pm.variables.get("variable_key");

(5) Dynamic variables

Note that dynamic variables cannot be used in the Sandbox. You can only use them in the {{..}} format in the request URL / headers / body.

猜你喜欢

转载自www.cnblogs.com/lizhiyan/p/10044667.html