JMeter check if status is 200 - jmeter

In my test plan I have 2 endpoints bid and win. And if bid endpoint return status 200 (it can also return 204, but I need only 200 so I can't use ${JMeterThread.last_sample_ok}) I need to run win endpoint.
I did:
create defined variable STATUS_OK
Create regular expressions extractor under bid request to get response code :
Add If controller and insert win request under that controller :
But if controller condition not working, Jmeter never run win request.
Any idea why it's not working? or maybe have I can debug it? I would be grateful for any help!!!
Updated including test plan structure:
bid requests - is CSV Data set config wit random jsons for each bid request (did like mentioned here)
thread - it's a thread with 200 users and 1 loop
bid - post request, for body I'm using one of json files ${__FileToString(/home/user/Downloads/jmeter/jsons/${__eval(${JSON_FILE})}.txt,,)}.
Also bid request include currency, bidid etc. it's Json
extractors, I'm using that data to generate correct win URL for
each bid.
if bid made - if controller discussed here
win - get request, where URL queries are different depends on bid response (using Json extractors). Url looks like:
win?auctionId=${AUCTIONID}&bidId=${BIDID}&impId=${IMPRESSIONID}&seatId=${SEAT}&price=${__javaScript((Math.random()* (4 - 1)+1).toFixed(4);)}&cur=${CUR}&adId=${ADID}

For If Controller You should use __groovy or __jexl3 function instead
Interpret Condition as Variable Expression? If this is selected, then the condition must be an expression that evaluates to "true" (case is ignored). For example, ${FOUND} or ${__jexl3(${VAR} > 100)}. Unlike the JavaScript case, the condition is only checked to see if it matches "true" (case is ignored).
Checking this and using __jexl3 or __groovy function in Condition is advised for performances
In your case use
${__groovy(vars.get("BID_STATUS") == vars.get("STATUS_OK") )}
Or
${__jexl3("${BID_STATUS}" == "${STATUS_OK}")}

You need to surround JMeter Variables references with quotation marks like:
"${BID_STATUS}" == "${STATUS_OK}"
Alternatively (better) you can get rid of this Regular Expression Extractor and switch If Controller's condition to use __groovy() function like:
${__groovy(prev.getResponseCode().equals(vars.get('STATUS_OK')),)}
More information: Apache Groovy - Why and How You Should Use It

Related

Looping in Jmeter

Looping test occurrence based on the data count retrieved from the JDBC request and also as input data for the HTTP request
I have test scenario where i need to use the DB output as the input criteria for the HTTP request. Based on the DB output count( from the first request) i need to loop the HTTP request and it data accordingly
I tried the logical Loop Count by passing the count variable from run time as ${TEST_ID_#}, still its not working.
I tried the logical Loop Count by passing the count variable from run time as ${TEST_ID_#}, still its not working.
Debug Sampler Output
You can extract the counter using Post Processor [either Regular Expression Extractor or JSON Extractor etc.]
Once you have extracted that count, now place a Loop controller as a parent of HTTP request.
For example. I am using User Defined Variable for loop Count:
Any reason for using ${TEST_ID_#} variable? If your Debug Sampler screenshot is full and correct you should be using ${KEY_ID_#} instead.
Also it might be a better idea to use ForEach Controller instead of the Loop Controller, the relevant configuration would be something like:
References:
How to Use ForEach Controller in JMeter
Using Regular Expressions in JMeter

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

How to Handle Dynamic Requests in JMeter - Which may/may not occur with each run

In Web Application :
There is Single Page having Different Sections/Partitions
Each section Retrieves the Data with the Help of Filter Query.
If there is no matching Result, Section will Send below Request only :
Request 1: domain/search/jobs/csuser__search2_1413357426.1559
If the Query returns any matching Result, Section will Send below Two Requests:
Request 1: domain/search/jobs/csuser__search2_1413357426.1559
Request 2: domain/search/jobs/csuser__search2_1413357426.1559/results_preview
How can I manage Request 2, which may or may not occur with each run.
Currently I am manually Recording entire Network calls, Removing unnecessary ones & running it for 'N' Loop Count. How can I make sure while test is runing if any section has matching results Request 2 Should also be taken care which I might not have benn recorded on my first execution.
You can use combination of Beanshell PostProcessor and If Controller to work it around as follows:
Add a Beanshell PostProcessor as a child of the Query request
Put the following code into the PostProcessor's "Script" area:
int length = prev.getSubResults().length;
if (length > 1) {
String path = prev.getSubResults()[length - 1].getURL().getPath();
if (path.contains("results_preview")) {
vars.put("resultPresent", "true");
}
} else {
vars.put("resultPresent", "false");
}
Add an If Controller after the request
Depending on your scenario put on of the following conditions to If Controller's "Condition" input
${resultPresent}==true - children will be executed if the query returns results
${resultPresent}==false - children will be executed if the query doesn't return anything matching
Beanshell code does the following:
check how many requests were executed
if there were more than 1 requests, path of the last request is extracted
if path contains "results_preview" variable resultPresent is being set with the value of "true"
if there was only 1 request then resultPresent variable is false
References:
How to use BeanShell: JMeter's favorite built-in component
JMeter “if controller” with parameters
SampleResult class JavaDoc
As Dimitri said. The processor should run after (post) request 1. The if controller will then act on the results of the processor. Section two is only run when the condition is not "false".

How to use JMeter to create a resource with POST, extract Location, and then subsequently update it with PUT

I would like to profile a REST API using JMeter. I would like to write the test plan such that each user thread performs the following actions:
creates a new resource using HTTP POST
If HTTP 201 Created is received, then extract the new resource URL from the Location header of the HTTP response.
Subsequently update the resource using HTTP PUT
Loop in 3 and measure the response time
It's unclear to me how to use JMeter's conditional logic to break up the tests into these discrete parts. I would appreciate any insight anyone can provide on how to implement this.
You need to use If Controller to express this logic.
You can use Regular Expression Extractor to extract Response code (In field to check, check it and extract response code in a Variable)
Use the previously extracted variable in the If Controller condition

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