Save body request in JMeter - jmeter

I am using CSV file which contains variables for my XML requests. I know how to see the request sent, but I would like to save each request in a separate file.
Is that possible?

The easiest way seems to be using Flexible File Writer, you can save requestData into a file.
If you need more flexibility - for HTTP Request samplers you can do it with Beanshell Listener like:
Add Beanshell Listener to your Test Plan
Put the following code into the Listener's "Script" area
import org.apache.commons.io.FileUtils;
String data = ctx.getCurrentSampler().getArguments().getArgument(0).getValue();
int threadNum = ctx.getThreadNum();
int loop = ctx.getVariables().getIteration();
FileUtils.writeStringToFile(new File("request-" + threadNum + "-" + loop + ".txt"),data);
You'll get files like request-x-y.txt generated in JMeter's "bin" folder where:
x - JMeter Thread Number
y - Current Loop Iteration (Thread Group level)
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using Beanshell in JMeter tests.

you should save the results as XML and turn on only the "Save Sampler Data (XML)"

Add > Listers> View Result Tree
In result tree click on config button and check the type of data thats has to be stored and provide the link to csv file.
Check the snapshot for more info.
Hope this helps.

Related

How to create a Java program in Beanshell PostProcessor in Jmeter to merge all the responses?

I have to send JSON requests based on the CSV test data. Suppose there are 90 records - which are basically the request bodies. I put the Thread in a loop to keep sending the request until the last one in the CSV.
Every time I get the response, I need to append them into a single CSV file. Now, since Jmeter Listener does not consolidate all the responses into CSV (I do not want it in xml), I want to know if I can write a Java snippet in BeanShell, capture all responses and write them to a CSV file.
You can use JSR223 Sampler with File.append adding text with , to append to CSV file
This will append to the end of the file.
File file = new File("out.txt")
file.append("hello\n")
If you want the "program"
Add JSR223 Listener to your Test Plan (since JMeter 3.1 it is recommended to use JSR223 Test Elements for scripting)
Put the following code into "Script" area
new File('myfile.csv') << prev.getResponseDataAsString() << System.getProperty('line.separator')
where prev stands for previous SampleResult class instance. Check out Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on JMeter API shorthands available for the JSR223 Test Elements.
A better option would be saving a response into a JMeter Variable using i.e. Regular Expression Extractor and writing it to a file via sample_variables property

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.

How to save values from HTTP response with JMeter?

on a JMeter test plan, I have too many HTTP requests. One of these creates a new session every time when clicking the create button.
How do I store that session_id in a CSV file for further operation?
Given you have already extracted this session_id using the relevant JMeter PostProcessor you can save its value into a file using JSR223 PostProcessor and the code like:
new File('/path/to/your/file.csv') << vars.get('session_id') << System.getProperty('line.separator')
Make sure you select groovy in the "Language" dropdown and tick Cache compiled script if available box.
If ${session_id} variable exists - JMeter will store its value(s) in the file provided.
There are a few ways to do it. The most useful is RegExp post processor.
It could be found here as it is shown in the following image.
Place it under Request that returns needed data in response.
The RegExp catches groups and stores them under different variable names, based on Name of Create Variable. The values could be searched in different areas of Response, as it is demonstrated in the image, we can search in headers, redirected pages, main bodies and so on. The Stored variable could be re-used in other HTTP Requests or processors (Post and Pre) through ${VariableName} (e.g. ${JSESSION_ID})
Reference name
RegExp itself
Capturing group
Match number
A default value to set if RegExp didn't work
DEBUG:
If a value is not found, the DEBUG in cooperation with Tree Results Viewer can help. Here they are :
The general script structure might look like :
Add BeanShell PostProcessor. Copy, paste the below code (with your modifications for path and var).
var = vars.get("your_variable_name");
FileWriter fstream = new FileWriter("/your/desired/path/results.csv", true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(var);
out.write(System.getProperty("line.separator"));
out.close();
fstream.close();

how can i pass dynamic values to url in jmeter

I have to give dynamic values to url which takes number of users and their age , which can be selected though web page. but I want to give it in Jmeter using BeanShell PostProcessor.
Help me in this,since I'm new to Jmeter.
This is the path:
/destinations/packages?airports%5B%5D=LGW&units%5B%5D=000577%3ADESTINATION&when=29-05-2016&until=&flexibility=true&flexibleDays=3&noOfAdults=2&noOfSeniors=0&noOfChildren=1&childrenAge=3&duration=7114&first=0&searchType=search&searchRequestType=ins&sp=true&multiSelect=true
from what I've got looks like you can use CSV Data Set Config.
Create .txt file with the data you want to feed your test with;
Place the above .txt file to the folder where your .jmx file lies;
In your Test plan: Under your request sampler - place CSV Data set Config;
Then if you need to use your dynamic values within one threadgroup => refer these data as ${quanity}, ${age} in you url.
If you need to pass these values across the threadgroups => add BeanShell Assertion
Then (in the other Tread group) refer those as ${__property(_quantity)},${__property(_age)}.
Hope, it helps.
First of all you need Beanshell PreProcessor, not Beanshell PostProcessor.
All this parameters are basically name/value pairs which can be defined via HTTPSamplerBase class. HTTPSamplerBase class instance is available to Beanshell PreProcessor as sampler pre-defined variable so if you add the following code into the Beanshell PreProcessor "Script" area
sampler.addEncodedArgument("airports[]","LGW");
sampler.addEncodedArgument("units[]","000577:DESTINATION");
sampler.addEncodedArgument("when","29-05-2016");
sampler.addEncodedArgument("until","");
sampler.addEncodedArgument("flexibility", "true");
sampler.addEncodedArgument("flexibleDays","3");
sampler.addEncodedArgument("noOfAdults","2");
//etc
Your HTTP request will be populated with the values, you set via Beanshell.
JMeter Variables can be accessed via vars shorthand which stands for JMeterVariables class instance.
String airport = vars.get("airport");
sampler.addEncodedArgument("airports[]", airport);
//etc
See How to Use BeanShell: JMeter's Favorite Built-in Component article for comprehensive information on how to use Beanshell Test Elements in Jmeter.
Remember that it is recommended to avoid scripting where possible so if there is another way of implementing your task - go for it.

jmeter : how to save parameter from response to external file and use it to enter another page?

How can i Save parameter or any word from the response of the page to external file (txt ot csv)ang get this parameter from the file to enter another page ?
In fact I want to be able to change the parameter dynamically via file.
Thanks.
To save the whole response thing into a file:
Add a Beanshell PostProcessor as a child of the sampler, whose data you need to save.
Add the following code to the PostProcessor's "Script" area
import org.apache.commons.io.FileUtils;
File file = new File("/path/to/your/file.txt");
FileUtils.writeByteArrayToFile(file, data);
To read file contents in JMeter:
Just use __FileToString function wherever file's content is required as:
${__FileToString(/path/to/your/file.txt,,)}
For more information on Beanshell bit refer to How to use BeanShell: JMeter's favorite built-in component guide.
For high loads it will be better to replace Beanshell PostProcessor with JSR223 PostProcessor and choose "groovy" as the language (you'll need groovy-all.jar somewhere in JMeter's classpath)

Resources