how to save whole response message in jmeter - jmeter

I am receiving below response in jmeter-
[{"Status":"Failed","ErrorDesc":"Duplicate Transaction Id","Amount":"23","CorporateID":"aaa","StatusCode":"ERR0DUP","TransactionReferenceNumber":"1111"}]
I need to save this whole response message.
I tried by using listner,and using csv file as well but only b able to save response like - OK,true
Please help me to save whole response as it is.

You could use the Save the Response to a File Sampler with following configuration
Ensure "Don't add number to prefix" check box is not checked
Set the Minimum Length of sequence number (e.g 6)
You can try with following options for unique file names
Check the Add timestamp
Use ${__threadNum} and/or ${__threadGroupName} fields with file name
response-${__threadGroupName}-{__threadNum}.json

If you want to save response into a variable just use Boundary Extractor with empty left and right boundaries or Regular Expression Extractor and (?s)(^.*) regular expression, see The Boundary Extractor vs. the Regular Expression Extractor in JMeter article for more details to learn more about differences of these two guys.
Example setup:
in the above setup the whole response will be saved into a JMeter Variable and you will be able to refer it as ${response} where required
If you want to save response into a file - go for Save Responses to a file listener, add it as a child of the request which returns the response and configure it like:
the above configuration will store the whole response of the parent sampler into response.txt file in "bin" folder of your JMeter installation

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 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

Automate sending a request and saving the response

There is a URL which I want to hit and save the response. The URL id needs to be incremented each time and save the response. For example -
First Get Request - http://google.com/getdata/?Id=1
First Response - one
Second Request - http://google.com/getdata/?Id=2
Second Response - two
and so on...
I want to hit the request with increment the id each time and save the response
I have tried using fiddler but unable to figure how to increment the id and save the response.
P.S. - I have to make around 6,00,000 hits
Since the 'Postman' tag is mentioned, I can help you regarding how to implement this in Postman.
Postman has a nice feature of using 'variables'.
You can use environment variables or globals.
Read more about these on their docs:
https://www.getpostman.com/docs/v6/postman/environments_and_globals/variables
You can use a global variable such as 'counter' and set it to 1 / whatever starting point you want.
Then you can modify your request like so :
http://google.com/getdata/?Id={{iteration}}
Now, in the Tests script of the request you can write the following script
let i = parseInt(pm.globals.get('iteration')) + 1;
pm.globals.set('iteration', i);
Also, to access the response you can use the following command in Test script:
console.log(pm.response); // Use pm.response as per your needs
Save the request in a collection.
Now load the Postman's Runner and select the collection.
Now you can put an iteration count of 6,00,000 and hit run!
Remember, heavy iterations will cause performance degradation.
In JMeter you need to click , Ctrl+0 and Ctrl+1 to create , Thread Group and HTTP Request
In Thread Group put the number of hits you need in Number of Threads (users)
In HTTP Request Put in Server Name or IP www.google.com and in Path /getdata/?Id=${__threadNum}
__threadNum will create increasing number from thread 1 to number of hits.
For small number of hits or debugging you can add View Results Tree to view request/response by clicking Ctrl+9 in Test Plan/Thread Group level.
To save the response use Post Processor, especially by adding Regular Expression Extractor below HTTP Request by clicking Ctrl+2.
Allows the user to extract values from a server response using a Perl-type regular expression. As a post-processor, this element will execute after each Sample request in its scope, applying the regular expression, extracting the requested values, generate the template string, and store the result into the given variable name.
Import to notice that for load testing you need to work with non GUI mode, which means call jmeter using command line as jmeter -n -t myTest.jmx
you will use Command-line mode (called Non-GUI mode) to run it for the Load Test.
Don't run load test using GUI mode !
For saving all responses to a one file see save response data or if you want to save file per thread/user you can add Save Responses to a file
Fiddler:
Open script editor (Control + r ) then add the following code inside OnBeforeResponse
static function OnBeforeResponse(oSession: Session) {
if(oSession.oRequest["X-SAVE-ME"] != "")
{
oSession.SaveResponseBody("C:\\tempfiddler\\" + oSession.SuggestedFilename);
}
}
Go to the "Composer" tab and include the header X-SAVE-ME with any value, in the URL, replace your ID with # (just like this: http://google.com/getdata/?Id=#) fiddler will now ask for the starting and ending value of ID before executing;
Please find the snapshot below for your scenario.
Scenario_Testplan
First, go to user properties and put "sample_variables = ID, Response_File_Name" or whatever the name you choose for the variables. Restart jmeter.
Create the below plan:-
CSV data set config to have incremental values and response file name
HTTP request will use ${ID}
Save response to a file will use ${Response_File_Name}
Hope this will help.
I would do this by command line, using a while loop with a curl to the URL, storing the body result on the standard output to a file. It would look something like this:
for i in {1..600000}; do curl "http://google.com/getdata/?id=$i" > body-result-id-$i; done
I couldn't test the line above because I don't have any access to a console right now, but I think it should work.
In Burp you can do this using the Intruder tool. First, capture a sample request in Burp. If you're unsure how to do this, please consult the getting started documentation.
Then right-click the request and selected "Send to Intruder".
In the Positions tab within Intruder, first click "Clear" then select the section you want to vary, and click "Add"
In the Payloads tab select the Payload type as "Numbers" and configure the range.
Click "Start attack"
For more information, consult the documentation.
One Another solution is that you can use Counter in jmeter. That you can find from below path
Thread Group > configElement > Counter.
Configure the Counter as per your need.
Give the Reference Name i for counter.
Use the variable in your request
For more information.

How to Re-use data generated by one Response to other request?

In my application while executing the first request one unique key is generated which key is required for Next all the request. let me how to automate such scenario in Jmeter.
The process should look as follows:
Add Post Processor to the first request
Configure it to extract the required value and store it into a JMeter Variable
Use JMeter Variable from step 2 in your next request.
Depending on response data type you have the following choices:
Regular Expression Extractor - for text
CSS/JQuery Extractor - for HTML
XPath Extractor - for XML and XHTML
JSON Path Extractor - for JSON
It is also possible to extract data from files i.e. if response is in PDF format, but it's a little bit tricky
Example configuration to store the whole response:
Reference Name: any suitable variable name, i.e. response
Regular Expression: (?s)(^.*)
Template: $1$
You can refer the extracted value as ${response} where required. You can also amend the regular expression to extract response part instead of the whole response. JMeter uses Perl5-compatible regular expressions, see Regular Expressions User Manual Chapter for details
You can use regular expression extractor to extract the key from the response of your first request and use the extracted key for subsequent requests. To achieve this:
Right click on the first request and add post processor: Regular Expression Extractor.
Create your regular expression and provide values in other required fields. Please refer to JMeter component reference http://jmeter.apache.org/usermanual/component_reference.html#Regular_Expression_Extractor
Extracted value will be saved in the variable given as reference name
Use this variable in subsequent requests
Here is an example test plan with results.

How to parse response of sample1 to create new sample in JMeter

I use JMeter to do performance test of web server. My test case is as following:
step1: send file update request to server.
step2: server will return some files URL as html response
step3: client need to create new request with the URL returned in step2,thus need to parse
the response of step2.
I am new to JMeter, and do not know how to implement it. I basically learned JMeter about the pre-processor and post-processor, but still no clue about how to do.
Ok let's start before the first step :
Right click -> Add -> Threads (Users) -> Thread Group
Now the actual first step (If you use REST) :
Add -> Sampler -> Http Request
You have at the bottom part Send Files With the Request. You can add file attachment if that is what you asked.
Extracting response from the server :
Let's assume your response is this :
<Response>
<name>StackOverflow.com</name>
<url>http://stackoverflow.com/questions/11186423/how-to-parse-response-of-sample1-to-create-new-sample-in-jmeter</url>
</Response>
Here is what you do :
Right click on The http request you previously added (in step 1) -> Post Processors -> Xpath Extractor
Reference Name is the name of the variable in which you want to store the value. Let's name it url. And Xpath query is Response/url or //Response/url if you get more response tags. If you want the first one //Response[1]/url and so on..
Repeat step 1 (copy/paste sampler and remove the Xpath Extractor you don't need it anymore), and change the Server Name or IP to ${url} which is the value previously returned.
And Voila there you go. Let me know if you got more specific questions. Jmeter is fun.
Per Grace comment :
Wants to extract https://192.168.100.46/updserver/download?action=signature_download&token=
Out of response data :
<responseData class="java.lang.String"><html>
<body>
ERROR=0
MSG=N/A
FILELIST=1555;1340778737370;1526545487;
VERSION=1.002
URL=https://192.168.100.46/updserver/download?action=signature_download&token=
INTERVAL=0
</body>
</html>
</responseData>
This should be pretty simple. Add a post processor -> Regular Expression Extractor and put the following :
Reference Name : url
Regular Expression : (http[\S]+)
Template : $1$
Match No. (0 for Random): 1
So now you have url variable that you can use further in your test as ${url}. Let me know if that works for you. I tested with dummy sampler and it works for me.
This is how I extract some value from url and pass it further as variable so next requests will contain it.
Here are some nice screenshot and wider description about making test in JMeter http://jmeter.apache.org/usermanual/build-web-test-plan.html
Add the Thread Group and HTTP Requests
When this HTTP Requests response with some data (in this example in URL) you want to extract it and us it after
So lets go:
Go to your first HTTP Request after which one you receive response with variable:
Add -> Post Processor -> Regular Expression Extractor
In this window set:
Response Field To Check: URL
Reference Name: MY-CUSTOM-VARIABLE-NAME
define name of variable whatever you like
Regular Expression: permanent.part.of.url.com/([a-zA-Z0-9]*)
so expression ([a-zA-Z0-9]*) is responsible for getting all
occurrences of alphabetic and numeric chars after meeting permanent url at start
Template: $1$
only one expression is extracted in our case and it need to be read
Match No. (0 for Random): 1
in this case there is only one match, but if more occur you can
choose which one use
Now put extracted value into next HTTP Request
Path: some.other.url.com/${MY-CUSTOM-VARIABLE-NAME}
remember that you read JMeter variables with this pattern ${}, so
use ${MY-CUSTOM-VARIABLE-NAME} whenever you need this value
Run your test and check what did you get in url of your request with MY-CUSTOM-VARIABLE-NAME
Experiment with regexp to get desired output.
Here is blogpost about this stuff:
http://kenning.co.nz/development/extracting-variables-using-regular-expressions-in-jmeter/
And always useful JMeter documentation:
http://jmeter.apache.org/usermanual/component_reference.html#Regular_Expression_Extractor

Resources