Reputation: 203
I'm having a JSON array with n number of elements like productName, productId. I would like to generate unique id in productId for each element and for each request.
Currently, I'm reading productId
from .csv
file but for each request same productId is applied for all the elements. For example:
test.csv
productId
10
11
12
13
14
In JMeter it substitute like below for request 1:
[
{
"productName": "Apple",
"productId": "10"
},
{
"productName": "Apple",
"productId": "10"
},
{
"productName": "Apple",
"productId": "10"
},
{
"productName": "Apple",
"productId": "10"
}
]
request 2:
[
{
"productName": "Apple",
"productId": "11"
},
{
"productName": "Apple",
"productId": "11"
},
{
"productName": "Apple",
"productId": "11"
},
{
"productName": "Apple",
"productId": "11"
}
]
But the way I'm expecting is, first request should be
[
{
"productName": "Apple",
"productId": "10"
},
{
"productName": "Apple",
"productId": "11"
},
{
"productName": "Apple",
"productId": "12"
},
{
"productName": "Apple",
"productId": "13"
}
]
And second request should be like below and so on,
[
{
"productName": "Apple",
"productId": "14"
},
{
"productName": "Apple",
"productId": "15"
},
{
"productName": "Apple",
"productId": "16"
},
{
"productName": "Apple",
"productId": "17"
}
]
productId should generate with some random id for each request and apply random id for all the elements in the json. How can we achieve this in JMeter?
Upvotes: 1
Views: 1647
Reputation: 168197
As per CSV Data Set Config documentation:
By default, the file is only opened once, and each thread will use a different line from the file. However the order in which lines are passed to threads depends on the order in which they execute, which may vary between iterations. Lines are read at the start of each test iteration. The file name and mode are resolved in the first iteration.
If you want to generate a random number you can just go for __Random() function which produces a random number within the given range:
[
{
"productName": "Apple",
"productId": "${__Random(1,2147483647,)}"
},
{
"productName": "Apple",
"productId": "${__Random(1,2147483647,)}"
},
{
"productName": "Apple",
"productId": "${__Random(1,2147483647,)}"
},
{
"productName": "Apple",
"productId": "${__Random(1,2147483647,)}"
}
]
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction
Upvotes: 1
Reputation: 1841
Another solution could be using the __CSVRead function instead of CSV Data Set Config element.
Note :
[
{
"productName": "Apple",
"productId": "${__CSVRead(productIds.csv,0)}${__CSVRead(productIds.csv,next)}"
},
{
"productName": "Apple",
"productId": "${__CSVRead(productIds.csv,0)}${__CSVRead(productIds.csv,next)}"
},
{
"productName": "Apple",
"productId": "${__CSVRead(productIds.csv,0)}${__CSVRead(productIds.csv,next)}"
},
{
"productName": "Apple",
"productId": "${__CSVRead(productIds.csv,0)}${__CSVRead(productIds.csv,next)}"
}
]
Upvotes: 0
Reputation: 1841
You can generate a random unique number with JMeter function __UUID
Replace the productId with the following
${__UUID}
Example
[
{
"productName": "Apple",
"productId": "${__UUID}"
},
{
"productName": "Apple",
"productId": "${__UUID}"
},
{
"productName": "Apple",
"productId": "${__UUID}"
},
{
"productName": "Apple",
"productId": "${__UUID}"
}
]
Upvotes: 3