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

Related

Simple Data Writer: Configure parameters Vs user.properties file

As I run my tests in Non-GUI mode, I am using Simple data writer with .CSV file option to write the results. I need to see the response data only when a request fails. I open the result file after the test is complete using view results tree, synthesis report and etc. but the response data says "Non-TEXT response data, cannot record: ()".
I am trying to understand:
When to use CSV and when to .jtl option? What is the major difference? When I used CSV, I am not able to see the response data. Do I need to add the below lines in the user-properties file:
jmeter.save.saveservice.response_data=true
jmeter.save.saveservice.response_data.on_error=true
jmeter.save.saveservice.output_format=xml
OR/AND configure the parameters in the GUI (in Simple data Writer) by choosing 'Save response data' and 'Save as XML' and unselect 'save field names (CSV)'?
JMeter cannot save non-text responses into XML files, you will get Non-TEXT response data, cannot record: () message when SampleResult.getDataType() function returns anything but text
You can work it around by adding a JSR223 Listener and using the following Groovy code there:
if (!prev.isSuccessful()) {
new File('response.txt').bytes = prev.getResponseData()
}

how to run multiple urls in jmeter and it should display on one screen

i am new to jmeter. i would like to run multiple urls at one shot and display the results on one screen. finding hard to config urls through csv file and in jmeter.
my sample url:
http://10.56.34.67:7065/services/sample/2070
http://10.56.34.67:7065/services/sample1/2070
http://10.56.34.67:7065/services/sample2/2070
like this i have more thn 100 url to test it.
could you please tell me the format to store urls in csv file and how to config the csv file in jmeter?
It has simple as
Click Ctrl+0,Ctrl+1 which adds Thread Group and HTTP Request in side
In HTTP Request add ${path} to Path field
In Thread Group choose Loop Count Forever
Add CSV Data Set Config by right click on Thread Group -> Add -> Config Element
CSV Data Set Config parameters :
a. Put the fileanme in Fileanme field
b. Enter in Variable Names path
c. Choose Recycle as False
d. Choose Stop Thread as True
Click Ctrl+R (run)
It will go through all URLs and submit them sequentially
To view results you can add View Results Tree (Click Ctrl+9) and you will see all your requests/responses.
It seems your data here is your URLs.
So instead of using multiple samplers for each URL, you can go for CSV Data config and store all your URLs there and name the column as URL.
you can refer to this in your single http sampler as ${URL}.
Your CSV should look like this
In Server name put ${URL} and in the Thread Group check the forever check box
You don't need CSV for this use case, the easiest way would be going for __StringFromFile() function.
In the HTTP Request sampler put the __StringFromFile() function into "Path" input field like:
The textual function representation is ${__StringFromFile(urls.txt)}, you will need to replace urls.txt with full or relative path to the file where your URLs are listed
That's it, each time the request is called JMeter will read the next line from the file and substitute request path with the string from file:
See Apache JMeter Functions - An Introduction article to get familiarized with JMeter Functions concept

How to hit parameterized url in Apache Jmeter?

This is my test URL " http://appr.seconddemo.org/hitssurveys/survey?uid=113&offerid=311&subid=subvalues&offr_id={Email} "
I want to hit the url 1500 times per second , and want to change the "{Email}" with real value in each iteration.
How it's possible please give me a step by step guide.
Considering that you need to pass the emails from the external file, the most efficient way that we are using in software testing companies is to pass the variable from .csv or .txt files using 'CSV Data Set Config' element of the JMeter.
Please find the steps that you need to follow:
Add a 'CSV Data Set Config' element from 'Config Element' by right-clicking the thread group
Set Filename field with complete path to your .csv or .txt file that contains your emails
Set Variable field as 'Email' [This variable name should be same as you set in your url request]
Ignore first line to False
Set other fields as per your requirement
Now add HTTP Sampler in your Thread Group and set the Protocol, Server Name, Method & Path as instructed in the screenshot:
Create .csv or .txt file and add all emails separated by new line:
Hope this answer is useful.
You can use the CSV Data Set Config. Put all your email credentials in the CSV file and make sure you have put this CSV file in your JMeter /bin directory.
Add a CSV dataset config in your test plan. Your CSV dataset config should be like:
Now in your thread group, define the number of threads you want to execute and then in your sampler put the path as follows:
http://appr.seconddemo.org/hitssurveys/survey?uid=113&offerid=311&subid=subvalues&offr_id=${Email}
Depending on where do your emails live there are following options:
If they are in a text file, each email on a new line you can use __StringFromFile() function like:
If they are in a database you can use JDBC PreProcessor to fetch the data from the database table column and put your request under ForEach Controller
If you need to provide just some random characters you can use __RandomString() function.
More information: JMeter Parameterization - The Complete Guide

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();

Regular Expression to read csv file on post processor

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.

Resources