How to parse response of sample1 to create new sample in JMeter - 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

Related

how to save whole response message in 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

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.

JMeter reponse headers values before redirection on status code 302

I am performing load testing on an API using JMeter. For that, I call an oauth link, which returns a code in the headers which I use for further testing. But the link redirects to another link and I am unable to capture the value of the response headers when a response with status code 302 is returned. How can I do that.
If your situation is like this one:
You can still extract the dynamic value from the latter sample result by modifying Regular Expression Extractor scope
As per documentation:
Apply to:
This is for use with samplers that can generate sub-samples, e.g. HTTP Sampler with embedded resources, Mail Reader or samples generated by the Transaction Controller.
Main sample only - only applies to the main sample
Sub-samples only - only applies to the sub-samples
Main sample and sub-samples - applies to both.
JMeter Variable - assertion is to be applied to the contents of the named variable
By default Regular Expression Extractor is looking into Main sample only, in the above example it is HTTP Request, if the data you're looking for is stored in one of the sub-samples it is enough to change Regular Expression Extractor's scope to look into sub-results as well:
You have 2 options:
Do not allow HTTP request to redirect. Simply uncheck "Follow Redirects" checkbox in the HTTP Sampler:
That way you can process this request normally. The drawback is of course that you need to add a second request which will take you to a link to which you are normally redirected automatically.
Most post-processors allow you to extract value from either main sample, or sub-samples, or both. So follow redirect as before, but change Post-Processor to extract value from sub-sample. For example Regular Expression Extractor:

Jmeter performance testing :

I have a requested in jmeter that request created new one request, so I need that new one request url in a variable because new one request having a user I'd.
You can handle this situation in at least 2 ways:
Extract the dynamic part using Regular Expression Extractor:
Add Regular Expression Extractor as a child of the 1st request
Configure it as follows:
Field to check: URL
Reference Name: anything meaningful, i.e. id
Regular Expression:userId=(.*)
Template: $1$
Assuming everything goes well you should be able to refer extracted value as ${id} where required (manually add it to the end of each and every request)
You can semi-automate the process using Using the HTTP URL Re-writing Modifier, just add it as a child of the first request and put userId into "Session Argument Name" input field, JMeter should be smart enough to automatically add these "userId" postfixes to subsequent requests.

Manipulating the request body of HTTP thread based on the data extracted from the previous HTTP response

I want to manipulate the request body of HTTP thread based on the data extracted (using 'Regular Expression Extractor') from the previous HTTP response.
Here is the scenario:-
I have extracted the statusFlag and statusId from 'HTTP request 1' as:
Ref name: status
Reg. Exp: "statusFlag":"(\w+)","statusId":"(\w+)"
So, first I want to check that the value of statusFlag is 'New' or not.
If it is New then I have to proceed and feed statusId in next HTTP request or else display statusFlag mismatch.
Need help. Got stuck badly.
I believe Response Assertion is what you're looking for. Add it after the Regular Expression Extractor and configure it as follows:
Apply to: JMeter Variable -> statusFlag (or your reference name)
Pattern Matching Rules: Equals
Add New as a "Pattern to Test"
The assertion will check whether "statusFlag" is "New" and if not - it will fail the sampler and report the expected and the actual value.
Optionally you can add If Controller after the Response Assertion, use ${JMeterThread.last_sample_ok} as a condition and place 2nd request as a child of the If Controller - it will be executed only if "statusFlag" is new.
See How to Use JMeter Assertions in Three Easy Steps guide for more information on conditionally setting pass or fail criteria to requests in your JMeter test.
That's how your Jmeter project should look like.
Regular Expression Extractor stores extracted value in ct variable that can be accessed in If Controller as "${ct}" == "yourvalue" and, if true, can be also sent as a part of Request 2 body using the same ${ct} reference.
Jmeter project structure

Resources