首页 > 解决方案 > 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
});

标签: postman

解决方案


我通过在运行收集之前在 Collection Runner 中启用Persist Variables复选框来解决此问题。


推荐阅读