How to retrieve number from Jason response - jmeter

I have the request where I'm passing page number.
https:// †*********/projects?page=1
Response will be project details with page number.
{
"Firstpage" : "page=1";
"Lastpage" : "page=10"
}
1000 records will be printed in first page. I need to repeat the request untill it's reached page 10.
I tried capturing with following regular expression
"Firstpage" : "(.*?)"
This will capture page=1, but I need to loop till it reached page 10.

Extract the value for the last page and store it into Lastpage JMeter Variable using Regular Expression Extractor configured like:
Name of created variable: Lastpage
Regular Expression: Lastpage"[\s]+:[\s]+"page=(\d+)
Template: $1$
Add Loop Controller to your Test Plan and put the following __jexl3() expression into "Loop Count" field:
${__jexl3(${Lastpage} - 1,)}
That's it, you should be able to refer the next page as ${__jexl3(${__jm__Loop Controller__idx} + 2,)} where required

Related

How to loop a jmeter request?

I have a request https://***********/projects?page=1
which gives the response
{
"QueryInfo": {
"QryNa": "Q_PTE1",
"resourc": "https://******************QueryResultsAsXML.v2/q/?auth=",
"CurrentPage": "page=1",
"FirstPage": "page=1",
"LastPage": "page=6",
"Rows": "1000",
"TotalRows": "6000"
}
}
I have to verify the LastPage, if it's more than 1 I have to run the same above request for all Pages numbers from 1 to 6. I have captured the page number in regular expression. Can you tell how to loop for this scenario?
Add an If Controller with condition ${__jexl3("${LastPage}" > 1)} where LastPage is the reference name for your regular expression extractor.
Add a loop controller and change the loop count to ${__intSum(${LastPage},-1)}
Add a Counter under the loop controller with below configurations
Start: 2
Increment: 1
Maximum: ${LastPage}
Reference Name: pageCounter
Make sure to check both Track counter independently for each user & Reset counter on each Thread Group iteration
Copy & paste your HTTP request under the loop controller and change page parameter to page=${pageCounter}
Add Regular Expression Extractor as a child of the request which returns the above response and configure it like:
Name of created variable: anything meaningful, i.e. lastPage
Regular expression: "LastPage"\s?:\s?"page=(\d+)",
Template: $1$
Add If Controller after the request and use the following condition:
${__groovy((vars.get('lastPage') as int) > 1,)}
Loop Controller as a child of the If Controller and use the following expression as the "Loop Count":
${__groovy((vars.get('lastPage') as int) -1,)}
Add HTTP Request sampler as a child of the Loop Controller and amend the request body to look like:
{
"QueryInfo": {
"QryNa": "Q_PTE1",
"resourc": "https://******************QueryResultsAsXML.v2/q/?auth=",
"CurrentPage": "page=${__intSum(${__jm__Loop Controller__idx},2,)}",
"FirstPage": "page=1",
"LastPage": "page=6",
"Rows": "1000",
"TotalRows": "6000"
}
}

Count the number of occurences of a string in response data

I want to search a string from a response in jmeter and count the number of occurences based on which i want to use a if controller to run the next requests. I am stuck with the code for counting the occurences
You can do it in at least 2 ways:
Using Regular Expression Extractor:
Add Regular Expression Extractor as a child of the request.
Configure it as follows:
Reference Name: anything meaningful, i.e. count
Regular Expression: string you want to count, i.e. JMeter
Template: $1$
Match No: -1
The number of matches will be stored in ${count_matchNr} JMeter Variable
Using Beanshell PostProcessor
Add Beanshell PostProcessor as a child of the request
Put the following code into the PostProcessor's "Script" area
import org.apache.commons.lang.StringUtils;
String response = new String(data);
int count = StringUtils.countMatches(response, "JMeter");
log.info("Found " + count + " \"JMeter\" words at the " + prev.getUrlAsString() + " URL");
vars.put("count", String.valueOf(count));
You'll be able to refer the matches count as ${count} JMeter Variable
References:
JMeter Regular Expressions
How to Use BeanShell: JMeter's Favorite Built-in Component

JMeter - Using response data as variable

The response I get back from a post is just a plain text guid seen here I'd like to pull this guid and use it in a variable for my following Get statement:
post response data
And I've tried a bunch of configurations in my regular expression extractor.
But it just ends up pulling Null when what I want is that guid.
Null
I'm new to jmeter - so thank you in advance for the help.
If you need the whole response use the following Regular Expression Extractor configuration:
Reference Name: response
Regular Expression: (?s)(^.*)
Template: $1$
As per How to Extract Data From Files With JMeter guide:
() = grouping
(?s) = single line modifier
^ = line start
. = wild-card character
* = repetition
So it will return the whole response.

How do I take the response from one JMeter rest sampler and use it as input to another?

I have a JMeter test that has a rest sampler that outputs a value as such in the response data:
{"Var1":"xxxxx-xxxxxxxx-xxxxxxxxxx"}
I need to take that out put (Var1) and use it as input for the next rest sampler in the following test step. I have a line in a BeanShell pre-processor that says:
String clearText = "somestring1" + "_" + var1 + ":" + "somestring2";
where var1 is derived from a Regular Expression Extractor from the preceding RestSampler (that is how I got the out put {"Var1":"xxxxx-xxxxxxxx-xxxxxxxxxx"} ).
Problem: I get a void for var1 in the output of the second rest sampler.
What am i doing wrong? how can i get the value from the output of the first rest sampler and use it as input in the second rest sampler??
Thanks.
ironmantis7x
Are you sure that your Regular Expression Extractor is correctly obtaining Var1? If you're testing RESTful API it's better to use JSON Path Extractor available via JMeter Plugins (you will need Extras with Libs Set).
Configure JSON Path Extractor as follows:
Reference Name: Var1 or whatever you like
JSON Path: $.Var1 this one assumes your response.
So you would be able to provide the variable value as ${Var1} or ${__V(Var1)} to 2nd request.
See Using the XPath Extractor in JMeter (Scroll down to "Parsing JSON") for more details on how to properly install the extension and build up JSON Path queries.
Hope this helps.
If your API returns response in JSON format then use below solution :
#1 To use this parameter within same thread group:-
a] Right click on Thread Name --> click on "Add" --> click on "Post Processors" --> select "Json Extractor"
b] On 'Json Extractor" window, select below values :
Variable Names == test //give any variable name(user-defined)
JSON Path expressions == $.[0].name //this will pick value of "name" param from API's Json response
c] Now to use ${test} to pass this value to other API as input. By using ${test} you can use it anywhere.
#2 To use this ${test} parameter in other thread group :-
a] Right click on Thread Name --> click on "Add" --> click on "Post Processors" --> select "Json Extractor"
b] On 'Json Extractor" window, select below values :
Variable Names == test //give any variable name(user-defined)
JSON Path expressions == $.[0].name //this will pick value of "name" param from API's Json response
c] Right click on First Thread Name(where variable value is populated) --> click on "Add" --> click on "Assertions" --> select "BeanShell Assertions"
d] Under "script" section paste below code :
${__setProperty(test, ${test})};
e] Now you can access this first API's response value using ${__property(test)}
variable

JMeter: Parse JSON and count

I am using JMeter to test a web application. The application returns JSON that looks like the following:
{"type":"8","id":"2093638401"}
{"type":"9","id":"20843301"}
{"type":"14","id":"20564501"}
I need to get a count based on type.
I have tried adding foreach controller with a regular expression extractor, but Im not sure I have done it correctly:
Apply to: Main sample only
Response field to check: Body
Reference name: match_type
Regular Expression: "type":"(\d)"
Template: $1$
Match no.: -1
Im new to JMeter so Im not sure if Im doing any of this correctly.
Thanks
If you want to operate on both type and id in single sampler, I think simple regex and ForEach controller won't be sufficient. You will have to write two regex extractor followed by while controller with BSF processor (javascript or beanshell) to extract both the values and export them to jmeter variable. Something of following type
- First Request
- Regex extractor for type
- Regex extractor for id
- BSF processor (to initialize the loopcount=0 and the total_matches of matches that you found)
- while controller (loopcount < total_matches)
- BSF processor
- export/set current_type = type_$loopcount
- export/set current_id = id_$loopcount
- increment loopcount
- USE current_type and current_id in whatever sampler you like
== Update ==
This http://goo.gl/w3u1r tutorial depicts exactly how to go about it.

Resources