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

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

Related

Extracted Regex Value not getting passed into next POST request body in Jmeter

In my first request I am able to extract the value using Regular Expression Extractor which is clearly visible into the debug sampler. The value is extracted by setting the following options in Regular Expression Extractor:-
Name of Created Variable:- instanceUID
Regular Expression:- "InstanceUid":"(.*?)"
Template:-$1$
Match No:-1
Default Value:- (Blank)
The value that I want to pass in the next POST request is visible as:-
instanceUID_g1=2ab5dfb8-a217-4ff2-9025-523565b7b7ad
And the body for the next HTTP POST request is set like this:-
${"iInfo":{"InstanceUid":"${instanceUID_g1}","Registry":"${Registry}"}}
When this request seen in detail inside View Results Tree looks like:-
${"iInfo":{"InstanceUid":"${instanceUID_g1}","Registry":"AAX"}}
As seen the value of ${instanceUID_g1} did not get substituted in the POST body as was for variable ${Registry} which was taken from CSV config.
Being new to Jmeter can anyone suggest what did I miss?
Most probably your Regular Expression Extractor placement is not very correct, be informed about JMeter Scoping Rules concept
If you place Regular Expression Extractor as a child of the request - it will be applied to this request only
If you place Regular Expression Extractor at the same level as several requests - it will be applied to all of them
in the latter case it will be applied 1st to 1st sampler, then to Debug Sampler, then to 3rd sampler so on 2nd step it will be overwritten, most probably that's your problem
Also it appears that you're getting the data from JSON so it makes more sense to use JSON Extractor or JMESPath Extractor

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:

In jmeter : how to extract values from two different json response and use that extracted values as a parameter for new http request

In jmeter: I’m using two http requests in one thread group, so I’m getting two response from those http requests.
I need ‘member id’ from one response and some string(digest) from other response, in order to create third http request.
I have extracted 'member id' by using this regular expression: {"Id":(.+?)}
which return me correct member id.
Response from 2nd request is string(digest) : "G9V6Su9PESaobcInErdD7Y8OKNo="
I added one more regular expression to extract this string : "(.+?)"
I have added two debug sampler as I'm using two regular expression extractor.
then I pass extracted values 'member id' and string(digest) as a parameter to third http request.
When I run it I'm getting error, my 3rd http request failed.
It's failing to pass extracted 'member ID', but its correctly pass string(digest)
In first debug sampler: its showing correct extracted 'Member ID' but some different values for digest field.
In 2nd debug sampler: its showing correct extracted 'digest' field but some different value for 'Member ID'.
I’m doing it all under one thread group
I'm new to jmeter, I don’t know whether it is possible to extract field from two different response and use as parameter to create new http request.
if it possible how to do it please help me.
Thanks in advance.
I believe that it's due to including quotation marks (and who knows what else) into the "Id" regular expression, you need to surround it with quotes like:
{"Id":"(.+?)"}
Also the better way of working with JSON data is JSON Path Extractor which is available via JMeter Plugins project. See Using the XPath Extractor in JMeter guide (scroll down to "Parsing JSON") for more information on installation, usage, syntax, etc.

Want to check for assertion failure in Jmeter

I am a beginner in JMeter. I want to check for Assertion Failures in my script. I want to continue my transaction for a number of iterations after a single log in attempt, and I want to log out only if an error occurs. For that, I want to check if an error occurred in the script.
Is it possible by comparing assertions in JMeter?
If not, is there are any other way to find that?
Define first indicator(s) that will mark response as erratic (Response Code, keyword, etc.).
Try to use Response Assertion to handle state of your request (success/failure) depending on indicators above and then use IfController along with pre-defined JMeterThread.last_sample_ok jmeter's variable - whether or not the last sample was OK - true/false.
Schema will look like below e.g.:
ThreadGroup
LOGIN REQUEST
...
YOUR HTTP REQUEST HERE
Response Assertion
Response Field to Test: Response Code
Pattern Matching Rules: NOT Equals
Patterns to Test: 200
Regex Extractor
IfController
Condition: ${JMeterThread.last_sample_ok} // will be TRUE if Response Assertion above is TRUE (i.e. response code != 200)
LOGOUT REQUEST
...
In addition to the answer above I would recommend you:
Add error checking to your script - assertions that the responses are
valid for a given reques
Use Firebug to view network traffic when you need to debug your test
script
Use a regular expression extractor to retrieve a dynamic value from a
response and re-use it in a later request
To get the idea you can follow JMeter error checking video tutorial.
FYI: Assertion details provided.
Hope this helps.

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