在Postman中重用{{$randomInt}}
我的第一个请求是:在Postman中重用{{$randomInt}},postman,postman-collection-runner,Postman,Postman Collection Runner,我的第一个请求是:GEThttp://example.com?int={{$randomInt}。 我需要对同一地址运行第二个请求(其中包含其他测试),所以我需要保存生成的变量。我怎么做 在第一次请求之后,我在“Tests”沙箱中尝试pm.variables.get(“int”),但此代码无法看到intvar 在预请求中创建随机数。沙箱到第一个请求: postman.setGlobalVariable('int',Math.floor(Math.random()*1000)) 也没有帮助,因为我
GEThttp://example.com?int={{$randomInt}
。
我需要对同一地址运行第二个请求(其中包含其他测试),所以我需要保存生成的变量。我怎么做
在第一次请求之后,我在“Tests”沙箱中尝试pm.variables.get(“int”)
,但此代码无法看到int
var
在预请求中创建随机数。沙箱到第一个请求:
postman.setGlobalVariable('int',Math.floor(Math.random()*1000))代码>
也没有帮助,因为我需要在URL中使用这个参数,而“Pre-req.”块在请求之后但在测试之前运行
那么,如何在第一个请求之前生成随机变量,并将其存储在第二个请求中使用呢?如果您在第一个请求的请求前脚本中设置了该值:
pm.globals.set('int', Math.floor(Math.random() * 1000))
Or
// Using the built-in Lodash module
pm.globals.set("int", _.random(0, 1000))
您将能够引用它,并在任何请求中使用{{int}
语法。如果您在第一个请求中添加此内容,然后在URLhttp://first-example.com?int={{int}
该值将持续存在,您可以在第二个请求中再次使用它http://second-example.com?int={{int}}
每次使用{{$randomInt}
时,它将在运行时生成一个新值。是否需要使用{{$randomInt}
一次,然后在第二个请求中使用不同的值。这个问题有点混乱,很难理解。是的,我知道我需要使用已经生成的变量,而不是第二次使用{{randomInt}}
。所以我在Pre-req中使用了错误的语法。谢谢酷,希望解决方案有帮助。:)