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"
}
}
Related
I have a scenario where in I need to wait for the response text .I need to send the same request till i get the required response text. I included my http samples in while loop with a counter. Now I am not able get the correct while condition.
Tried with below conditions.
${__javaScript(("${recordTypeLabel1}"!="asdf" && ${counter} < 5),)}
${__jexl3("${recordTypeLabel1}" != "asdf",)}
Both are failing. How to handle this?
Pleasae help.
Threadgroup
Once only controller Login
loop controller
HTTP req
HTTP req
While loop {
Counter
HTTP request
HTTP Request
JSON extractor
}
HTTP req
Once only Controller Logout
The correct syntax for the __jexl3() function would be:
${__jexl3("${recordTypeLabel1}" != "asdf" && ${__jm__While Controller__idx} < 5,)}
Don't use __javascript() function as it is some form of a performance anti-pattern, stick to __jexl3() or __groovy() functions if you need to script some extra logic
Also you don't need to introduce a Counter, since JMeter 5.0 you have a special pre-defined variable called ${__jm__While Controller__idx} which holds zero-based iteration number of the While Controller. (If you change the While Controller's label to something else - make sure to amend the variable accordingly)
Exit when loop number exceeds the threshold
Exit when variable value becomes expected:
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
I am getting a text in the response data "success". i am using while controller with a http request, counter, regx. regx is capturing the text, counter is to increment the count. and i want the while controller to run until i get say 5 times success in the response data
while controller ${__jexl3("${variable}" ="Successful" && ${counter} = 5)}
http request
regx
counter
it became an infinite loop
the equality operator in JEXL is "==" (you see section "Operators" in documentation: http://commons.apache.org/proper/commons-jexl/reference/syntax.html), besides the while loop should go until the counter variable is less equal to 5. So the condition should be set like this:
while controller ${__jexl3("${variable}" == "Successful" && ${counter} <= 5)}
i hope this helps
I have added a regular expression with reference name variable to capture the word "successful "from response data and match no as -1 .
And in while controller
Condition = ${__javaScript("${variable_matchNr}" =="${counter}",)}
And added http request (with same regx)and pause
It worked.
Thanks .
I have caught up in a situation, where in i need to verify the response of the Previous Sampler for one of the value and if the Value for that is [], then i need to trigger the below request or else then switch to another Sampler.
Flow:
Check Response of Sampler for One of the attribute
IF(attribute value==[])
Execute the Sampler under IF Conditions.
ELSE
New Sampler
Sample Response:
{"id":8,"merchant_id":"39","title":"Shirts-XtraLarge","subtitle":null,"price":110,"description":null,"images":"image_thumbs":[[]],"options":[],"options_available":[],"custom_options":[]}
I need to check if the attribute custom_options is empty or not! If Empty do some actions and if not empty do some other action !
Need if condition to simulate this!
Help is useful!
A nice to have feature in JMeter would be Else statement, but until then you will have to use 2 If Controllers
If Controller allows the user to control whether the test elements below it (its children) are run or not.
Assuming you hold your attribute value using regex/json/css/other post processor extractor add two condition, first is positive and under it the Sampler:
${__groovy("${attributeValue}" == "[]")}
Second is negative and under it add the New Sampler
${__groovy("${attributeValue}" != "[]")}
__groovy is encourage to use over default Javascript
Checking this and using __jexl3 or __groovy function in Condition is advised for performances
Go for Switch Controller
Add JSR223 PostProcessor as a child of the request which returns your JSON
Put the following code into "Script" area:
def size = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..custom_options')[0].size()
if (size == 0) {
vars.put('size', 'empty')
} else {
vars.put('size', 'notempty')
}
Add Switch Controller to your Test Plan and use ${size} as "Switch Value"
Add Simple Controller as a child of the Switch Controller and give empty name to it. Put request(s) related to empty "custom_options" under that empty Simple Controller
Add another Simple Controller as a child of the Switch Controller and give notempty name to it. Put request(s) related to not empty "custom_options" under that notempty Simple Controller.
More information: Selection Statements in JMeter Made Easy
I am a beginner with Jmeter tool.
I use JMeter to hit a URL and obtained a response message in JSON format.
I want to parse the response to extract every array element and use it to append to the tail of another URL. Can you please tell me how to do this?
example: { "reply": { "code": "111", "status": "SUCCESS", "customer": [ "222-a", "b-333", "44-4", "s-555", "666", "777", "88-8" ] } }
You can use a Regular Expression Extractor (as child of your URL sampler) to first extract the array of values:
Reference name: ary
Regular Expression: \[([^\]]+)\]
Template: $1$
Match No: 1
then use another Regular Expression Extractor to extract the values:
Apply To JMeter variable: ary
Reference name: vals
Regular Expression: "([^"]+)"
Template: $1$
Match No: -1
The match no -1 creates variables vals_1 .. vals_7, which you can then use in a ForEach Controller to assign to a JMeter variable:
Input variable prefix: vals
Output variable name: id
[v] Add '_' before number?
now you can use the JMeter variable ${id} in a nested URL sampler to pass the customer id in a URL.