Anoop Mishra
Anoop Mishra

Reputation: 31

How can i add random value using jmeter in Json

I am testing an API using JMeter.

But if I have sent multiple requests it's not working because of user_id show duplicate.

In the Test plan, I add a variable and define value.

Using BeanShell PostProcessor I try to make generate random value.

But all time as I define value not update the random value.

{

  "user": {

    "user_id": 100113,

    "rest_id": 4589445,

    "rest_name": "chetan",

    }

}

Using HTTP header manager and HTTP Request. In HTTP Request body I passed this JSON

For a single request, it's working fine. But if I have sent multiple requests it's not working because of user_id show duplicate. how can I change every time user_id with the help of JMeter? Right now I am doing manually. Every time I have to change user_id.

Upvotes: 2

Views: 4902

Answers (3)

Badr Bellaj
Badr Bellaj

Reputation: 12841

you could be to work with Random Variable, an element available in JMeter’s config elements:

Thread group -> Add -> Config Element -> Random Variable enter image description here

jmeter, random variable element

The sampler looks like this:

jmeter , load testing, random variable sampler

Setting it as follows:

enter image description here

then call in the body or url the random variable using ${random}

Link

Upvotes: 0

sunny_teo
sunny_teo

Reputation: 1999

Use CSV Data sec config to parameterize user id. Input userids in csv then pass the column name of the csv in the json as "user_id": ${USERIDFROMCSV}

For more info on csv data set config use, check link:- https://guide.blazemeter.com/hc/en-us/articles/206733689-Using-CSV-DATA-SET-CONFIG

Also, there are functions which can be used like Random,RandomString. For more information check:- https://jmeter.apache.org/usermanual/functions.html

Upvotes: 0

piyushpatel974
piyushpatel974

Reputation: 46

Use this random function ${__RandomString(6,1236547890,)}. It will generate random value of 6 digit each time.

For more details visit below link: https://jmeter.apache.org/usermanual/functions.html

Upvotes: 1

Related Questions