0

I pass some a random integer in the value of a parameter in the request body -

"trans_id":"7q498992029699{{$randomInt}}"

What is the best way to get the final value of trans_id param in the Tests tab?

As per my observation, using {{$randomInt}} again in the request body gives a different random integer.

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144

2 Answers2

3

A way around is to store it to environment variable while sending the request, using Pre-request-script. And later get same environment variable in your test.

Body:

{
    "trans_id": "{{transId}}"
}

Pre-request-script:

var randomNumber = '7q498992029699' + _.random(0, 1000);
pm.environment.set("transId", randomNumner);

Test:

var tarnsId = pm.environment.get("transId");

Note: {{$randomInt}} and _.random(0, 1000) both are doing the same thing, it provides random number from 0-1000.

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
1

You could create your random int as a variable in the Pre-Request script of your request like this:

pm.globals.set('myRandomInt', Math.floor(Math.random() * 1000))

Or

// Using the built-in Lodash module
pm.globals.set("myRandomInt", _.random(0, 1000))

(I am using a global variable in this example but you can of course use an environment variable if you want to.)

Now you can re-use the variable {{myRandomInt}} in your request body and in your Tests tab.

0xFEFFEFE
  • 120
  • 10
  • You can simply use reference link instead of copying the code from another answer: https://stackoverflow.com/a/48184677/4753489 – Divyang Desai Mar 27 '19 at 12:47