Skip to content

Token: Random Number

authorjapps edited this page May 13, 2020 · 12 revisions

Contents

Flavors of Random Number

We can use ${RANDOM.NUMBER} or ${RANDOM.NUMBER.FIXED} depending on our business usecases

Usecases of Random Number

Use case1: ${RANDOM.NUMBER}

If we want the an ID or any field or part of a field to be a random value inside a scenario, and this value is further random if used more than once inside that scenario

Use case2: ${RANDOM.NUMBER.FIXED}

If we want an ID or any field or part of a field to be a random value inside a scenario, and this value is fixed or same, if used more than once inside that scenario

Using ${RANDOM.NUMBER}

"steps":[{
      "name": "mytest",
      "url": "/orders",
      "operation": "POST",
      "request": {
        "headers": {
          "Content-Type": "application/json;charset=UTF-8"
        },
        "body": {
          "orders": [
            {
              "orderId": "${RANDOM.NUMBER}",
              "amount": 5,
              "productName": "product1"
            },
            {
              "orderId": "${RANDOM.NUMBER}",
              "amount": 10,
              "productName": "product2"
            }
          ]
        }
      },
      "assertions": {
        "status": 200

      }
    }]

after replacing RANDOM.NUMBER placeholders,we get the following request:

request:
{
  "headers" : {
    "Content-Type" : "application/json;charset=UTF-8"
  },
  "body" : {
    "orders" : [ {
      "orderId" : "6432473468588586502",
      "amount" : 5,
      "productName" : "product1"
    }, {
      "orderId" : "6249704932535842532",
      "amount" : 10,
      "productName" : "product2"
    } ]
  }
} 

Every instance of RANDOM.NUMBER is unique

Using ${RANDOM.NUMBER.FIXED}

Sample step:

 {
      "name": "mytest",
      "url": "/orders",
      "operation": "POST",
      "request": {
        "headers": {
          "Content-Type": "application/json;charset=UTF-8"
        },
        "body": {
          "orders": [
            {
              "orderId": "${RANDOM.NUMBER.FIXED}",
              "amount": 5,
              "productName": "product1"
            },
            {
              "orderId": "${RANDOM.NUMBER.FIXED}",
              "amount": 10,
              "productName": "product2"
            }
          ]
        }
      },

      "retry":{
        "max": 2,
        "delay": 500
      },

      "assertions": {
        "status": 200

      }
    },

after RANDOM.NUMBER.FIXED placeholder are replaced we get the following request:

request:
{
  "headers" : {
    "Content-Type" : "application/json;charset=UTF-8"
  },
  "body" : {
    "orders" : [ {
      "orderId" : "6598115625161692428",
      "amount" : 5,
      "productName" : "product1"
    }, {
      "orderId" : "6598115625161692428",
      "amount" : 10,
      "productName" : "product2"
    } ]
  }
} 

RANDOM.NUMBER.FIXED placeholders get the same random number value within the step

Using ${RANDOM.NUMBER:10}

We can limit random numbers' length. In this case random numbers length(digit count) would be 10.

Conclusion

Please visit the "Hello World" repo which has some working examples of these random-number usages.

Blogs

Clone this wiki locally