Regular Expression to read csv file on post processor - jmeter

i have one requirement, it might be simple but not getting any clue. Actually in my jmeter script i am getting one http response as csv file. The same file i need to read into my post processor script which is groovy script. Any one has any idea how to do this, struggling from past couple of days but not getting any clue.

If I correctly got your use case, you have:
HTTP Sampler which is hitting some CSV file
Some Post Processor in which you want to read data from this CSV file
If you look into View Results Tree listener into your request, which retrieves CSV file you should see this CSV file contents.
JMeter provides powerful BeanShell Post Processor which is capable of interacting with JMeter Context. It's as simple as next line:
vars.put("myCSV",prev.getResponseDataAsString());
Another option is to store response data as a new CSV file as follows:
FileOutputStream out = new FileOutputStream("my.csv");
out.write(prev.getResponseData());
out.flush();
out.close();
As the result of above you'll store your response as file "my.csv" in the folder, from where you launched JMeter (usually /bin folder of your JMeter installation).
After that you will be able to use "my.csv" file in CSV Data Set Config to iterate through the variables as usual.

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: 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 write HTML response in a file using JMeter

Can any one help me how to save HTML response (including screen images) from View Results Tree listener using JMeter?
I can store the results in csv but my main objective is to store the screen images that are displayed in view results tree
The screenshot name should be stored under step name (eg: TC002 Account Menu)
You can add JSR223PostProcessor and save response body to the file.
For example like this:
File file = new File(pathToYourFile);
FileWriter fstream= new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(prev.getResponseDataAsString());
out.close();
fstream.close();
If you want each answer to be saved in a different file, you will need to add code to create the files and add them a new uniq name.
UPD
One way to save each response in your own file is to generate the name depending on the value of the counter like this:
(Using JMeter Functions)
def filename = "${__counter(FALSE,)}" + "response.html";
File file = new File("C://JmeterResultFolder//"+filename);
or this:
(Using Counter Sampler)
def filename = "${counter}" + "response.html";
File file = new File("C://JmeterResultFolder//"+filename);
and in the end you will get file for each request
You can user .csv file to store the response data. Please refer below screen
Basic link for Listeners
You can configure JMeter to store response data if it is needed for any reason, add the next lines to user.properties file:
jmeter.save.saveservice.output_format=xml
jmeter.save.saveservice.response_data=true
jmeter.save.saveservice.response_data.on_error=true
and restart JMeter to pick the properties up. Next time you run your script response data will be inlined into .jtl results file and you will be able to see it with View Results Tree listener.
More information:
Configuring JMeter
Apache JMeter Properties Customization Guide
Results File Configuration
Be aware that storing response data causes massive disk IO overhead so use it wisely (i.e. for tests development and/or debugging only) as it might ruin your test given more or less high load.

HTTP Request path needs to be parameterized with UUID and a random URL

URL format is
www.something.com/something/api/{uuid}?code=gomundw0gjq2kbvc3g63whfr9usap5rurjaq5vui5vm6xbt9hhqa8hbcpto4yatwpn26v42t9
I need to make several calls to this GET Url and check performance of the feature.
uuid here is not just the UUID , but is specific to my application
UUID= accountid+identifierid+uuid, which will look like 1234+1234+123e4567e89b12d3a456426655440000
How should I arrange my Jmeter Test Plan.
My Plan would be:
write UUID generated to a file
Using any preprocessor call the file from file and write program to append to a different value in the csv which is account/identifierid.
Please help on this, can some one explain how I should logically arrange my test plan and code to get data to parameterize my path in HTTP Request sampler.
Thanks in Advance
You need to get accountid+identifierid information stored into CSV file. This is something you need to do on your own.
You can use CSV Data Set Config in order to get the information from that file while your test is running, example configuration would be:
UUID can be generated using __UUID() function
So your HTTP Request sampler configuration would be something like:

Reading a XML file input in jmeter

We have few load runner scripts that take XML file as input and reads these xml files for data.
For example:
while (!feof(file_ptr))
{
/* Check for file I/O errors */
if (ferror(file_ptr))
{
lr_output_message("Error reading file %s", filename);
break;
}
}
Please let me know if we can handle these file handling operations (other than CSV file) in jmeter
You can use __FileToString function to read an arbitrary file into a JMeter Variable which can later be used anywhere in the current thread group.
Refer to How to Use JMeter Functions post series for extra information on this and other various helpful JMeter functions.
You can use BeanShell Sampler for reading the XML file.
You can write a code in BeanShell sampler to read the xml file which you can write in JAVA.
And you can store the xml contents in Object type you want i.e Hashtable, ArrayList etc...

Resources