postman - postman.setNextRequest() 中的目标请求无法设置环境变量
问题描述
我postman.setNextRequest("login")
用来调用登录请求。在登录测试中,我从令牌登录响应中设置环境变量。这行不通。登录测试成功,但环境变量token不存在。
集合中的预请求脚本:
var email = "foo@bar.com";
var password = "mypassword";
pm.environment.set("email", email); // success to set env variable
pm.environment.set("password", password); // success to set env variable
postman.setNextRequest("login");
postman.setNextRequest(null);
登录请求中的测试脚本:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
var dt = pm.response.json();
pm.environment.set("token", dt.token); // failed to set env variable
});
解决方案
我通过在运行收集之前在 Collection Runner 中启用Persist Variables复选框来解决此问题。
推荐阅读
- python - 使用“导入模块名称”时 - 变量值和函数(如果有)存储在哪里?
- api - 是否有任何替代 Newsapi 的 react-native
- scrapy - 蜘蛛运行良好,但没有刮掉一些结果
- apache-spark - Spark 是否适合在作业中处理来自 sql db 的数据?如何避免在作业中处理相同的数据?
- android - 如何在 Android 中将视频播放器视图拖到画中画模式?
- python - 正则表达式搜索字符串中带有双引号的单词
- python - Python 调用 PyFIles
- python-3.x - 验证用户帐户后刷新令牌为空
- c# - JetBrains Rider 中的自定义空断言
- powershell - 如何从类方法访问全局变量