Getting Number of Occurrences of String in JMeter - regex

I am analyzing a website that returns text (JSON array), which I'm using HTTP Request element for. What I'm trying to do is check the number of times a string appears in the response, for example a field called "itemname". So I have added a Regular Expression Extractor, put ItemNameVar as the Reference Name, ^itemname$ as the Regular Expression, $1$ for the Template, -1 for Match No, and "NOT FOUND" for Default Value. I've also added an If Controller, which says "${ItemNameVar_matchNr}" == "1", because I expect it to occur only one time. However, it never fails if I set it to a different number. What am I doing wrong here? Thank you.

It looks like to be an issue with your regular expression.
I would suggest using Beanshell Post Processor instead of Regular Expression Extractor as JSON structures aren't very handy to parse with regexes.
Reference Beanshell code will look as follows:
import org.apache.commons.lang3.StringUtils;
int matches = StringUtils.countMatches(new String(data), "itemname");
vars.put("ItemNameVar_matchNr", String.valueOf(matches));
Explanation:
First line - import necessary helper class
Second line - data is a short-hand for byte-array representation of Sampler response. Method countMatches counts number of occurrences of itemname criteria in string representation of response data. Result is saved as matches integer variable
Third line - value of matches is saved as JMeter Variable called ItemNameVar_matchNr
See How to use BeanShell: JMeter's favorite built-in component guide for more detailed explanation of Beanshell scripting in JMeter and small Beanshell cookbook containing JMeter API usage examples.

The approach you are following is absolutely correct.
Please check your Jmeter script. Make sure the next request you are trying to execute after
IF Controller is branch or within Controller
Jmeter IF Controller
Hope this will help.

Related

JMeter Regular extraction looking to correlate a value between two dynamic variables

im in a bit of a pickle
im trying to extract a value where the value before it and after it are very redundant.
[sample of the response with the desired value highlighted][1]
now since the value before it and after it are dynamic that narrows down the regex values i can rely on.
and validating every idea i have gives back a wider string.
how can i extract this value?
[1]: https://i.stack.imgur.com/kYUXf.png
JSON is not a regular language therefore using Regular Expressions for extracting data from it is not the best idea.
Consider using i.e. JSON Extractor which allows using arbitrary JsonPath queries allowing fetching data from JSON in flexible, readable and more powerful way.
If you post the code in text form and not as an image I could come up with a proper JsonPath expression.

JMeter '&' Character not Encoded as '&' in the URL

When request extracts a URL variable from XML document through XPath Extractor and Regular Expression Extractor it decodes the encoded characters in the extracted URL.
For example, the URL in the XML file:
<url>www.testing.com/test/request?parameter1=test1&parameter2=test2</url>
The & between the two URL parameters become & which affect my testing final results.
Is there any way to prevent special characters decoding in JMeter?
Normally JMeter should not perform encoding and decoding of variables during extraction, the variable should be sent as is so most probably something is wrong with your test logic. You can double check the variable value using Debug Sampler and View Results Tree listener combination. See How to Debug your Apache JMeter Script article for more information on JMeter tests troubleshooting.
As a workaround you can explicitly tell JMeter to encode the variable using __escapeHtml() function like:
${__escapeHtml(${your_variable_here})}

Regular Expression Extractor Parameterization

I'm just getting started with JMeter and have run into a problem. I'm using the Regular Expression Extractor to scrape and parameterize a string that I named CouponID, as follows:
"coupon_id":”(.+?)” (from the following json response "coupon_id":"320747")
But when I plug the variable (${CouponID}) in the value field of a parameter in the subsequent http request, its request URL ends up looking like this and the test fails:
http://[...]/coupon.json?id=${CouponID} (instead of [...]/coupon.json?id=320747)
When I include a valid default value, the URL is constructed correctly and the test passes.
Please advise.
Try using this instead.
(?<=\"coupon_id":")(.*?)(?=\")
Preview:
http://imgur.com/6F5Lk0X

jmeter extract multiple regular expression

I'm looking to extract 2 expressions from the following response:
"FirstValue":"1234","Someotherfield":"****","Someotherfield":"****",(Some other more fields),"SecondValue":"6789"
Now explained deeply: there is my first value - which is followed by many other values, and eventually my second value. Note: the number of fields between is defined and determined, but I don't use the following solution:How to extract multiple values with a regular expression in Jmeter because I think it will be too long regular expression (about 20 back slashes).
I've come up with the following 2 solutions:
Reference name: Parameters
1."FirstValue":"(.+?)"(.+?)"SecondValue":"(.+?)"
2."FirstValue":"(.+?)"*.*"SecondValue":"(.+?)"
Which work fine. But, I want to make it more efficient, since in the response I get also the value between my requested values (e.g. Parameters_g0="FirstValue":"1234","Someotherfield":"****","Someotherfield":"****",(Some other more fields),"SecondValue":"6789").
So question is, is there a more efficient way to use? If no, which one is preferred between what I raised in this post?
Thank you

How can I use details from a JSON response from JMeter in a JSR223 PreProcessor?

I'm using JMeter 2.7 to load test a web application. I have a HTTP Request Sampler that returns a JSON document that has two values I want to extract from it. To do that, I have two RegEx PostProcessors assigned to the Sampler with the following configuration:
Regular Expression Extractor
Apply to: Main Sample Only
Response Field to Check: Body
Reference Name: val_1
Regular Expression: "val_1": "(\S+)"
Template: $1$
Match No.: 1
The configuration for the second is the same, just substituting val_2 for val_1. These seem like they should work.
Now, I also have a JSR223 PreProcessor script on a subsequent HTTP request that I want to use to transform the values I'm grabbing with the regex. However, the output of vars.get('val_1') and vars.get('val_2') are both null values. Tinkering with user-defined variables and "Apply to" settings haven't yielded any useful results.
Can anyone explain what I'm doing incorrectly? How can I use data retrieved through a RegEx extractor in my JSR223 script? Thanks in advance.
Edit - 9/26/2012
It was requested that I explain the structure a bit more, so here's an outline.
Thread Group
Once Only Controller
HTTP Sampler
Regular Expression Extractor (for val_1)
Regular Expression Extractor (for val_2)
Some Additional Simple Controllers with HTTP Samplers Here
Throughput Controller (80%)
HTTP Sampler
JSR223 PreProcessor (This is the PreProcessor in question)
Regular Expression Extractor (to grab an unrelated value from the response)
Some Additional Controllers with HTTP Samplers Here
Hopefully this is more helpful.
Is your regexp "val_1": "(\S+)"?
can you add a debug sampler just after Http sampler to check that the 2 vars are defined.
To do that add a tree result listener and see what debug sampler shows.