I'm using the packaged app version of Postman to write tests against my Rest API. I'm trying to manage state between consecutive tests. To faciliate this, the Postman object exposed to the Javascript test runtime has methods for setting variables, but none for reading.
我正在使用Postman的打包应用程序版本来编写针对我的Rest API的测试。我试图在连续测试之间管理状态。为了实现这一点,暴露给Javascript测试运行时的Postman对象具有设置变量的方法,但没有用于读取的方法。
postman.setEnvironmentVariable("key", value );
Now, I can read this value in the next call via the {{key}} structure that sucks values in from the current environment. BUT, this doesn't work in the tests; it only works in the request building stuff.
现在,我可以通过从当前环境中吸取值的{{key}}结构在下一次调用中读取此值。但是,这在测试中不起作用;它只适用于请求构建的东西。
So, is there away to read this stuff from the tests?
那么,有没有从测试中读取这些东西?
2 个解决方案
#1
78
According to the docs here you can use
根据这里的文档你可以使用
environment["foo"] OR environment.foo
globals["bar"] OR globals.bar
to access them.
访问它们。
ie;
即;
postman.setEnvironmentVariable("foo", "bar");
tests["environment var foo = bar"] = environment.foo === "bar";
postman.setGlobalVariable("foobar", "1");
tests["global var foobar = true"] = globals.foobar == true;
postman.setGlobalVariable("bar", "0");
tests["global var bar = false"] = globals.bar == false;
#2
3
Postman updated their sandbox and added a pm.*
API. Although the older syntax for reading variables in the test scripts still works, according to the docs:
邮递员更新了他们的沙箱并添加了pm。* API。虽然测试脚本中用于读取变量的旧语法仍然有效,但根据文档:
Once a variable has been set, use the
pm.variables.get()
method or, alternatively, use thepm.environment.get()
orpm.globals.get()
method depending on the appropriate scope to fetch the variable. The method requires the variable name as a parameter to retrieve the stored value in a script.设置变量后,使用pm.variables.get()方法,或者使用pm.environment.get()或pm.globals.get()方法,具体取决于获取变量的适当范围。该方法需要变量名作为参数来检索脚本中的存储值。
#1
78
According to the docs here you can use
根据这里的文档你可以使用
environment["foo"] OR environment.foo
globals["bar"] OR globals.bar
to access them.
访问它们。
ie;
即;
postman.setEnvironmentVariable("foo", "bar");
tests["environment var foo = bar"] = environment.foo === "bar";
postman.setGlobalVariable("foobar", "1");
tests["global var foobar = true"] = globals.foobar == true;
postman.setGlobalVariable("bar", "0");
tests["global var bar = false"] = globals.bar == false;
#2
3
Postman updated their sandbox and added a pm.*
API. Although the older syntax for reading variables in the test scripts still works, according to the docs:
邮递员更新了他们的沙箱并添加了pm。* API。虽然测试脚本中用于读取变量的旧语法仍然有效,但根据文档:
Once a variable has been set, use the
pm.variables.get()
method or, alternatively, use thepm.environment.get()
orpm.globals.get()
method depending on the appropriate scope to fetch the variable. The method requires the variable name as a parameter to retrieve the stored value in a script.设置变量后,使用pm.variables.get()方法,或者使用pm.environment.get()或pm.globals.get()方法,具体取决于获取变量的适当范围。该方法需要变量名作为参数来检索脚本中的存储值。