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.
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
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
Related
I'm using Jmeter to test my web server, instead of using the jmeter's default
response time, I'd rather to use one specific header value that I extracted from response header by Regular Expression Extractor, how can Jmeter generate a table or graph base on that value(think that value as the real response time)
Use sample_Variable to generate the required data with simple data writer as csv. Then, replace the response time data generated in that sheet with your custom variable data in the sheet. Then, browser that file in the listeners as per your requirement.
I have 4 input parameters which I need to pass in my http request sampler. Everything is OK but the problem is how to first data i have to pass from the previous thread http response and rest 3 from the csv file. Please help.
It seems that you want to update a specific value of the CSV during execution. It would be wise to say that this can be achieved using if controller and User Parameters element. You can go through the below screenshots to resolve this problem.
CSV Data set config for reading csv values
CSV File
Here, I have added text "REPLACE_WITH_RESPONSE" so that we can match it in IF controller.
IF CONTROLLER
This will check if you want to replace this value or not.
Users Parameters
This element would be used to update the csv value with previous request response. In the below example, Date is the CSV value which is replaced with previous Response.
Hope the above solution will help you in resolving this problem.
I have 2 HTTP request: One to GET data from an api and the other to POST data to the api.
The GET request brings several users in JSON. The POST request requires to 1 request per user. Hence I need to:
Loop the same POST request several times depending on the number of users (already did this by using a while controller that checks the number of users from the JSON response).
For each POST request, I need the variables used in such request to be updated depending on the user's information inside the JSON response.
The approach I am attempting is to use BeanShell PreProcessor inside the POST request but I'm having troubles with it.
Suppose one variable is called ${name} in the request body of the POST. I am using a JSON Extractor PostProcessor (On the GET request) path: "Travelers[0].FirstName" that returns the first name of the first user but then I need the second user's name (Travelers1.FirstName) to be assigned to the same variable ${name} before the POST request is sent and so on with every single user.
I want to make a for loop like this:
for (int i = 0; i <= numberOfTravelers; i++) {
vars.put("Name", Travelers[i].FirstName)
}
The problem is that I do not know how to call a JSON path from a JSON response of another previous request. Is there a way to reference the jsonpath to the specific JSON response or a way to save the whole JSON response in a variable and then find the specific value inside that variable as a JSON path.
I have tried this with the JSON extractor but the problem is that if I use the path: Travelers[*].FirstName, it will actually get all the names on the JSON but the variable ${name} will only store ONE name, not all of them as an array that I can later access in my for loop with a normal variable ${name[i]}. That is why I need to access the JSON path from the BeanSheall.
Here a sample of the JSON response:
{
"Travelers":
[
{
"FirstName":"VICTOR",
"Surname":"ORREGO",
"Key":"1.1",
"PassengerRPH":1,
"TypeCode":"ADT"
},
{
"FirstName":"JULIO",
"Surname":"OZAETA",
"Key":"2.2",
"PassengerRPH":2,
"TypeCode":"ADT"
}
]
}
This is the PostProcesor JSON Extractor at the GET request that I'm using. it is currently assigning the first name it gets from the JSON response (Victor) to the variable ${Name}
I need that in the next iteration (of the POST Request) the variable ${Name} to return the next name in that path, which is Julio.
here is the solution..
Add a JSON Extractor to the get request .. use match no -1 to store all Firstnames as show below.
i'm extracting all Firstnames and storing it in JMeter variables with a single JSON extractor
2. To the same get request add a JSR223 Post processor and set the counter value to 1
vars.put("counter","1");
Add a while loop to the test plan and add the following condition to the while loop.
${__javaScript(parseInt(${counter})<=parseInt(vars.get("FirstName_matchNr")),)}
4.To the post request add a JSR223 Pre Processor and add the following code
vars.put("name",vars.get("FirstName_"+vars.get("counter")));
This will store FirstName_Matchno's value in name variable.
Add a JSR223 Post Processor to the POST Request and increment the counter.
int counter = Integer.parseInt(vars.get("counter")) +1;
vars.put("counter",Integer.toString(counter));
You can see in the results its substituting a different name on each iteration of loop
Let me know if it helps..
I have to perform load test using JMeter on POST REST API which send JSON data in the request body as follows
{
id="",
name="abc",
age="34"
}
I want to use UUID feature in JMeter to auto generate UUID for the value of "id" which should be unique for every record created in the backend.
The value for "id" should be unique for every Thread executed in that Thread Group in JMeter.
Just use __UUID function
{
id="${__UUID}",
name="abc",
age="34"
}
If you're looking to learn jmeter correctly, this book will help you.
I would like to read the exact value of a variable I use to pass through an HTTP Request. I first read in many values of variables using the CSV Data Set Config. For the username, it is in the form of an email address. So, I have a variable called "email" in the Data Set Config. In the actual HTTP Request, for "name", I call it "username". For the "Value" field for this same "username", I added a time() function to it like this so I would end up creating unique users in my tests:
${email}${__time()
When I view the "Request" in a View Results Tree, I can see my parameter is listed correctly:
username=email1%40email.com1390854377360
I do not care if this is correct in real world terms. I already know that is not a valid email. That is ok for now.
What I want to know is how can I log that email that I just created on the fly? I would like to not have to pull in the whole request every time also and then use some type of Regular Expression extractor. It just seems like there should be an easy way to do this.
I think there are 2 ways,
Beanshell Pre/Post processors : you can write custom code in which you can log all your variables in some custom log file
Simple data writer : you can configure it and check save url,save field names,save response data field checkboxes that will give you complete data but in that later postprocessing on result file is required to get all usernames (email in your case).
first approach is easier and allows you create your own logging format(easy to retrieve and use somewhere else).
second approach is somewhat tedious and requires post processing.