How to pass json object in jmeter using csv file? - jmeter

This is the json object
{
"addLabourerToSpecificShift":false,
"isEarlySubmit":true,
"isExistinglabourer":false,
"task":"add",
"currentDate":"2018-06-08T07:08:21.296Z",
"allSections":[
],
"labourerBean":{
"gender":0,
"enrolmentDate":"2018-06-08T07:08:21.296Z",
"epfMemberId":"2",
"firstName":"Kedar",
"lastName":"jadhav",
"dob":"1990-11-30T18:30:00.000Z",
"phone1":"1236547896",
"panNumber":"1452368545"
},
"companyRetentionBonus":0,
"isRootAdmin":true,
"from":"manage_labourers"
}

The easiest way would be formatting your JSON payload to look like a single string i.e.
{"addLabourerToSpecificShift":false,"isEarlySubmit":true,"isExistinglabourer":false,"task":"add","currentDate":"2018-06-08T07:08:21.296Z","allSections":[],"labourerBean":{"gender":0,"enrolmentDate":"2018-06-08T07:08:21.296Z","epfMemberId":"2","firstName":"Kedar","lastName":"jadhav","dob":"1990-11-30T18:30:00.000Z","phone1":"1236547896","panNumber":"1452368545"},"companyRetentionBonus":0,"isRootAdmin":true,"from":"manage_labourers"}
Then you will be able either to use CSV Data Set Config with \n Delimiter like:
and refer the value as ${json} where required.
Even easier option would be using __StringFromFile() function directly in your HTTP Request sampler Body Data tab like:
${__StringFromFile(/path/to/your/csv/file,,,)}
__StringFromFile() function reads the next line from the specified file each time it's being called so this way you will be able to parameterise your request with minimal effort

Related

How to pick json playload from external file in jmeter?

How to pick json playload from external file in jmeter?
I Want to login with multiple users for that I need to add Json request. I need to pick json parameter/Json request from external file. Please let me know the process so that i can pick json parameter from external file.
Json request for example
{"username":"honey#est.com","password":"Phone#123","provider":"","access_token":""}
If you want to read a single line from a file - go for CSV Data Set Config or __StringFromFile() function
If you want to read entire file - go for __FileToString() function
If you have a folder with multiple JSON files and would like to iterate all of them - check out Directory Listing Config
It's also possible to read a file using JMeter's HTTP Request sampler
this way you will be able to use Post-Processors to extract partial data if needed.

JMeter Flexible file writer trying to save requestbody to a single cell in csv file

With Jmeter I am trying to save the request body using the Flexible file writer in csv file format. The request body is a json format something like this :
POST https://testurl
POST data :
{
"label" : test
}
The request body is getting saved to the file but not in a single cell. It is coming in multiple rows. How can I save the complete request body to one single cell.
There is no such a thing as "cell" in CSV files so your question doesn't make a lot of sense
If your request body is multiline you might want to remove these line breaks before saving the request body, it can be done using i.e. JSR223 PostProcessor and the following Groovy code:
def requestBody = prev.getSamplerData().replaceAll('[\n\r]', '')
vars.put('requestBody', requestBody)
You will need to declare this requestBody as a Sample Variable in the user.properties file:
sample_variables=requestBody
and once done you will be able to access the value in the Flexible File Writer as variable#0
[Filewriter csv][1]
Thanks for the response. When I try this I am seeing the requestdata like shown in the screenshot. And incase if there are multiple controllers the request body for all of them will be appended one after another in the same line.
[1]: https://i.stack.imgur.com/jb9Hm.jpg

Jmeter how to read file path within csv file

Is it possible to pass in file path or read from another file within csv? My test setup is something like that.
Jmeter Test
Http request -> body data -> "items": "${__property(${items})}",
CSV data config
Id,Name,Items
1,MyName,\input\items_json.txt
I want to include a file in csv items column and in jmeter test, it will read and post items json.
Got it working with this.
"items": ${__FileToString(${__property(${Items})},,)},
Not really, being a Configuration Element CSV Date Set Config is initialised before anything else so you will have to consider another approach, i.e.:
__CSVRead() function
__StringFromFile() function
__FileToString() function()
Functions are being evaluated exactly where they're called so this way you can use them for parameterization. See Apache JMeter Functions - An Introduction to learn more about JMeter Functions concept

I Want to add different values using csv config in my following code

In my below code i have to use multiple values in userEmail, userName, userMobile.
How can I do ti using csv config?
{"userDevicedId":"d0lfwViLwLQ:APA91bH4VSXS7VlthXhGQ4DPfb3otSUvwMnpmcK-mKyh7WN46xtDXWSYIOLB6Uw-nYr3zSBZOoiicpNuURQ2nR-a9vtwwSI8sehG-FbgYi4AaCdgurLG5C3Le4sYE0Lmp-piz_STLYnB","userEmail":"Tok#gmail.com","userMobileNo":"8989898995","userName":"Tik","userPasskey":"xmrVhb2MpmuzgLN941tGiLtdJnY3yvgMxlhXATEUa7SvsxclTiMLdJgPs7YTbNHF","userPassword":"Test123$$"}
Given your CSV file looks like:
someone#example.com,someone,1234567890
someoneelse#example.com,someoneelse,2345678901
etc.
Configure CSV Data Set config as follows:
Filename: full path to the CSV file
Variable Names: userEmail,userName,userMobile
Other config values - according to your scenario, see Using CSV DATA SET CONFIG guide for detailed explanation
Refer extracted values like ${userEmail}, ${userName}, etc. where required, i.e. if you want to send parametrised JSON it should look like:
{
"userDevicedId": "d0lfwViLwLQ:APA91bH4VSXS7VlthXhGQ4DPfb3otSUvwMnpmcK-mKyh7WN46xtDXWSYIOLB6Uw-nYr3zSBZOoiicpNuURQ2nR-a9vtwwSI8sehG-FbgYi4AaCdgurLG5C3Le4sYE0Lmp-piz_STLYnB",
"userEmail": "${userEmail}",
"userMobileNo": "${userMobile}",
"userName": "${userName}",
"userPasskey": "xmrVhb2MpmuzgLN941tGiLtdJnY3yvgMxlhXATEUa7SvsxclTiMLdJgPs7YTbNHF",
"userPassword": "Test123$$"
}
If you're using HTTP Request sampler, switch it to "Body Data" mode:
Remember that CSV Data Set config reads next line each iteration so make sure you provide enough loops on Thread Group level or via Loop Controller

JMeter using a variable as input for a response assertion pattern

I have a test plan in JMeter that has a Response Assertion where I'd like to use a variable that comes from a CSV Data Set Config. So my Pattern looks like:
${assert1}
Which corresponds (at least in my thoughts) to what comes from the file used in CSV Data Set Config, but it doesn't work. I have seen multiple suggestions to use a Regular Expression Extractor, but the examples I saw refer to something they're trying to capture from a page. In my case I am using an external csv file.
My question: how can I use a variable as input for a Response Assertion pattern ?
I have successfully used a CSV Data Set Config as a source of string that I use in a url parameter, and then use a Response Assertion to seek out that parameter in the response.
Variable name: P.
URL: /product/${P}.
The response assertion Parterns to Test: ${P}.
I wonder if it is not necessary to use the CSV data variable in the request in order for it to be available in the response assertion?
If my understanding is right,
here your problem:
You want to use the value from your CSV as part of URL and that too via variable.
Solution:
Configure your CSV Data Set Config like this:
Filename: url csv path
variable name : assert1
Delimiter : , (if your CSV comma separated)
leave remaining unchanged
That's it you can use the variable assert1 anywhere: ${assert1}.

Resources