I need to test some functionality x amount of times, let’s say 10 times for this purpose.
Is it possible to change the body in the request that is being sent through pm.sendRequest(), each iteration?
I do have a body in the main request that and I use like this:
const req = {
url: `${host}
method: ‘POST’,
header: {
},
body: {
mode: ‘raw’,
raw: pm.request.body.raw
}
}
And inside the main body-request I use variables that i.e, sets random numbers on some of the fields, like this:
"amount": "{{amount}}"
But when I run the request, the body doesn’t change, so I get the same response 10 times.
I have declared the variable amount:
let amount = Math.floor(Math.random() * 99).toFixed(2);
pm.environment.set(‘amount’, amount);
I.e: my body looks like this:
{
“amount”: {{amount}}
}
And I want my pm.sendRequest() to modify this body each iteration, which means, I want the response to be a unique amount each iteration, but when I run it, the same amount returns each iteration. Seems like the body doesn’t change inside my iteration.
When sending this, I expect two different amounts, but the same amount are returned twice.
I have also stripped some stuff so pmtRes.paymentId refers to something else, so dont’t bother look too much at that.
into the prerequisites tab and run 2 iterations, and check that the above is printed out twice to confirm that it is indeed running the preReq each time?
let amount = Math.floor(Math.random() * 99).toFixed(2);
pm.environment.set("amount", pm.variables.replaceIn(amount));
works fine for me also. But I did notice that there was a 0.5ms delay in the value updating, so could you humor me and try:
let amount = Math.floor(Math.random() * 99).toFixed(2);
pm.environment.set("amount", pm.variables.replaceIn(amount));
setTimeout(() => {}, 2000); // 2 second delay
also do you need to save this value in the environment variables? can it just be discarded after use and stick in globals? (if so ill need to update your body to point to globals)
e.g.
let amount = Math.floor(Math.random() * 99).toFixed(2);
pm.globals.set("amount", pm.variables.replaceIn(amount));
Actually looking at this with very tired eyes (neary midnight) the issue here is with your code. Specifically the for loop your using.
The PreReq will get a value, you then do a loop to send 2 requests. At no point do you every call the prerequisites code again. This is why you have the same value over and over.
Can I ask why you are doing it this way and not via the runner?