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

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

Related

JMETER: Need to send all entries in a CSV file to HTTP request body in 'one' request

I'm trying to solve a test data issue in Jmeter. Can anyone of you have a look at below problem statement and advise here please ?
Requirement: Need to send all entries in a CSV file to HTTP request body in 'one' request to the end point.
Example CSV File:
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d"
HTTP Request Body:
["${data}"}]
When the data is substituted, I should be able to get below output.
[
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d"
]
Problem Statement: When I use CSV data set config. file, I'm unable to concatenate all entries into one single request body. My understanding is, CSV data set config. the file is not the right option here.
Did some search in StackOverflow and followed a method to achieve above using JSR223 PreProcessor' and the link is, How to send multiple json body using jmeter?.
Followed the above link and tried added below custom code provided.
def builder = new StringBuilder()
new File('/path/to/plans.csv').readLines().each { line ->
builder.append(new File(line).text).append(System.getProperty('line.separator'))
}
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', builder.toString(), '')
sampler.setPostBodyRaw(true)
Upon running, I get below error message,
Caused by: java.io.FileNotFoundException,
"adsfas123wsf00000wqefqwe52145t10000",
"fdfrgvergq120947r0000dwsfqwaef237sadf",
"wfrqwef7865034r78tkahsefjh6985r7asfdaf",
"qefqwe52145t10000adsfas123wsf00000w",
"wsfqwaef237sadffdfrgvergq120947r0000d" (The filename, directory name, or volume label syntax is incorrect)
If the file is not found, then how come the entries are read and displayed in the log viewer?
Also, how do I link the output of custom code to the request body? Or is it taken care of by the custom code itself?
You have an error in your code, change this line:
builder.append(new File(line).text).append(System.getProperty('line.separator'))
to this one:
builder.append(line).append(System.getProperty('line.separator'))
If you want to send the full contents of the file you don't even need to go for scripting, you can use __FileToString() function right in the "Body data" tab of the HTTP Request sampler:
${__FileToString(/path/to/plans.csv,,)}
And last but not the least if you need to generate JSON from the plain text it might be a better idea to go for JsonBuilder class, see Apache Groovy - Why and How You Should Use It and Apache Groovy - Parsing and producing JSON
Two steps:
Add a User Parameter Pre-processor before the HTTP request:
Name: DATA
User_1: ${__FileToString(/path/to/plans.csv,,)}
Add the following to request body:
${DATA}

How to replace blank " " cell to null without quotes in jmeter while reading data from csv in HTTP Request body? data

I have data in csv and somewhere cell value is blank and when I am using this blank value in my HTTP request body data then it is replacing like "", but I want to replace as null without quotes. Please help here.
PFA snapshots
1
2
3
You can do it dynamically using i.e. JSR223 PreProcessor and the following Groovy code:
def body = sampler.getArguments().getArgument(0).getValue().replaceAll('\"\"','null')
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', body,'')
sampler.setPostBodyRaw(true)
Add JSR223 PreProcessor as a child of the HTTP Request and it will replace blank values with nulls.
Where sampler stands for HTTPSamplerProxy class instance, it provides access to request body, url, headers, cookies, etc.
The other simple solution to resolve this problem is to use "" in the test data itself.
Check the below screenshots :-

How to read random data for each request in JMeter

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.

How can I dynamically post Request body (xml) and validate the response (xml)?

Is there a way to send the XML request dynamically and validate the XML response?
My scenario is:
I will have a CSV dataset config and inside the csv file I will have two column, the first one is for the inputXMLFilePath and the second column is the expectedXMLResposneFilePath.
So I need to have a JSR233 PreProcessor under HTTP request sampler, read the input file path convert it to the post body, and also has another JSR233 sampler for load the expected response from the expectedXMLResponseFilePath and compare it with the previous XML response. I have a snippet for JSON which is working fine. but for XML how can I do it?
You can use __FileToString() function for both use cases:
To send the XML request body, like ${__FileToString(${inputXMLFilePath},,)} (where ${inputXMLFilePath} is the variable from the CSV Data Set Config)
To validate the response using Response Assertion configured like:
Field to Test: Text Response
Pattern Matching Rules: Equals
Patterns to test: ${__FileToString(${expectedXMLResponseFilePath},,)}
You can use JMeter Functions literally at the any place of your Test Plan so their flexibility is higher than for other test elements. Also JMeter Functions are being compiled into native Java code therefore their execution speed will be higher and footprint will be less comparing to Groovy scripting.
Check out Apache JMeter Functions - An Introduction article to learn more about JMeter Functions concept.

How to pass json object in jmeter using csv file?

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

Resources