Jmeter - Accessing Regex Array Variables In Beanshell - jmeter

I have problems picking up variables set by the Regular Expression
Extractor in a Beanshell.
I have an HTTP Request sampler which returns a list of 50 numbers in random form (4, 2, 1, 3....50, 45) which I have extracted via regEx.
Now I want to get each and every numbers in a variable so I used again regEx with expression (.+?)(,) on JMeter variable from step# 1 above.
I have problem here at this step when I am using BeanShell to access these values
Wasn't very sure I used below:
long var1 = Integer.parseInt(vars.get("Number_i"));
print("Value of var1: " +var1);
Practically I want to do this:
for (i=0; i<50; i++) {
if (var1==1) {
do this
}
}
I am not adept at Jmeter, so please bear with me.

Given you extract variables using Regular Expression Extractor and you have > 1 match you already have multiple variables, you can check them using Debug Sampler and View Results Tree listener combination
So you can access variables in JMeter like:
${number_1}
${number_2}
and in Beanshell test elements using vars shorthand which stands for JMeterVariables class instance like:
vars.get("number_1");
vars.get("number_2");
Example code which will iterate all the matches and "do something" when current variable value is "1"
int matches = Integer.parseInt(vars.get("number_matchNr"));
for (int i=1; i<=matches; i++) {
if (vars.get("number_" + i).equals("1")) {
log.info("Variable: number_" + i + " is 1");
// do something
}
}
See JMeter API - JavaDoc on all JMeter classes and How to Use BeanShell: JMeter's Favorite Built-in Component for more information on how to get started with Beanshell in JMeter

Related

Jmeter: Comparison of multiple result for result response with JDBC variables

In j meter: In a bean-shell assertion or any other I want to match the content of response which I have fetched using Jason extractor suppose:
Result[1]=A, Result[2]=b, Result[3]=c
and so on Versus variables I have fetched using JDBC pre-processor which has saved as:
Result_1=, Result_2=B, Result_3=c.
I am able to match 1 variable at a time but not all at one time. so need help with bean-shell code to compare all result at once.
Add JSR223 Assertion somewhere to your test plan (normally as a child of a Sampler you would like to fail)
Put the following code into "Script" area
for (int i = 1; i <= Integer.parseInt(vars.get('ResultFromJSON_matchNr')); i++) {
if (!vars.get('ResultFromJSON' + i).equals(vars.get('ResultFromJDBC_' + i))) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Variables mismatch')
}
}
Make sure you have groovy selected as the "Language" and have Cache compiled script if available box ticked
The above script assumes ResultFromJSON and ResultFromJDBC JMeter Variables reference names, amend them according to your real definitions.
More information: Scripting JMeter Assertions in Groovy - A Tutorial

Looping through JSON response + in JMETER

I am using Jmeter for performance testing and stuck at following point:
I am getting a JSON response from Webapi as follows:
PersonInfoList:
Person
[0]
{
id: 1
name: Steve
}
[1]
Person
{
id: 2
name: Mark
}
I need to get the ids based on the count of this JSON array and create a comma separated string as ("Expected value" = 1,2)
I know how to read a particular element using JSON Post processor or Regex processor but am unable to loop through the array and create a string as explained so that I can use this value in my next sampler request.
Please help me out with this: I am using Jmeter 3.0 and if this could be achieved without using external third party libs that would be great. Sorry for the JSON syntax above
Actually similar functionality comes with JSON Path PostProcessor which appeared in JMeter 3.0. In order to get all the values in a single variable configure JSON Path PostProcessor as follows:
Variable Names: anything meaningful, i.e. id
JSON Path Expressions: $..id or whatever you use to extract the ids
Match Numbers: -1
Compute concatenation var (suffix _ALL): check
As a result you'll get id_ALL variable which will contain all JSON Path expression matches (comma-separated)
More "universal" answer which will be applicable for any other extractor types and in fact will allow to concatenate any arbitrary JMeter Variables is using scripting (besides if you need this "expected value and parentheses)
In order to concatenate all variables which names start with "id" into a single string add Beanshell PostProcessor somewhere after JSON Path PostProcessor and put the following code into "Script" area
StringBuilder result = new StringBuilder();
result.append("(\"Expected value\" = ");
Iterator iterator = vars.getIterator();
while (iterator.hasNext()) {
Map.Entry e = (Map.Entry) iterator.next();
if (e.getKey().matches("id_(\\d+)")) {
result.append(e.getValue());
result.append(",");
}
}
result.append(")");
vars.put("expected_value", result.toString());
Above code will store the resulting string into ${expected value} JMeter Variable. See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information regarding bypassing JMeter limitations using scripting and using JMeter and Java API from Beanshell test elements.
Demo:

How to get print the value of a variable which is extracted by regural expression extractor in JMeter

I am trying to track the value by printing it to the log but getting void as an answer.enter image description here
The easiest way to see the variable names along with values is using Debug Sampler
However if you need to print all the extracted values to JMeter log for some reason you need to slightly change your script to look like:
log.info("Detected " + vars.get("urls_matchNr") + " URLs");
for (int i=1; i<= Integer.parseInt(vars.get("urls_matchNr")); i++) {
log.info("URL # " + i + ": " + vars.get("urls_" + i));
}
vars stands for JMeterVariables class instance so this way you get read/write access to all Jmeter Variables in scope.
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on Beanshell scripting in JMeter
There are 2 problems in your script.
1) You are extracting Match No: -1, which is not right (Check your regular expression extractor). You can either chose 0 for random match or any positive number for the respective match.
2) In BeanShell assertion, you are trying to retrieve the value as
logs.info("the" +urls);- which is not the right way to do.
To get the value of a variable in BeanShell we have to use "vars.get" method.
So change your assertion to logs.info("the" +vars.get("urls")); and try once.

Need to extract dynamic values from a string response in Jmeter

I need to extract the dynamic value "BSS1,DS1,HYS1,MS1,PTS1,QS1,USG1,YS1,RT10086,RT10081,RT10084,RT10082,OT10076,RT10083,UT10081,RT10085,"
from the string response "ACCOUNT_DETAIL_ACCOUNT_PRODUCT_SERVICES_EDIT_UPDATE_NameSpace.grid.setSelectedKeys(["BSS1","DS1","HYS1","MS1","PTS1","QS1","USG1","YS1","RT10086","RT10081","RT10084","RT10082","OT10076","RT10083","UT10081","RT10085"]);"
I have tried using the regular expression extractor :
Regular Expression :Keys\(\[\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\",\"(.+?)\"]\)
template : $1$$2$$3$$4$$5$$6$$7$$8$$9$$10$$11$$12$$13$$14$$15$$16$
But the above regular expression works only if there are 16 values in the response. If the response contains less number of values, for example, "ACCOUNT_DETAIL_ACCOUNT_PRODUCT_SERVICES_EDIT_UPDATE_NameSpace.grid.setSelectedKeys(["BSS1","DS1"]);"
then the above regular expression doesn't work.
How can I extract the values in the response if the total count is unknown?
Also the double quotes in the response need to be omitted.
Is there any post processor using which dynamic values can be extracted?
Any help is greatly appreciated.
I believe it will be easier with some scripting.
Add Beanshell PostProcessor as a child of the request which returns aforementioned response
Put the following code into the PostProcessor's "Script" area:
String response = new String(data);
String rawKeys = response.substring(response.indexOf("[") + 1, response.indexOf("]")); // get the data inside square brackets
String keysWithoutQuotes = rawKeys.replaceAll("\"", ""); // remove quotes
String[] keyData = keysWithoutQuotes.split("\\,"); // get array of keys
for (int i = 0; i < keyData.length; i++) { // store array of keys into JMeter variables like
vars.put("Keys_" + (i +1), keyData[i]); // Keys_1=BSS1, Keys_2=DS1, etc.
}
vars.put("Keys_matchNr", String.valueOf(keyData.length)); // set Keys_matchNr variable
Where:
data is byte array containing parent sampler's response data
vars is a shorthand to JMeterVariables class which provides read/write access to JMeter Variables.
As a result you'll have variables like:
Keys_1=BSS1
Keys_2=DS1
..
Keys_matchNr=X
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for additional information on Beanshell scripting in JMeter and some more examples

jmeter json path bean post processor

I have following entries that got extracted from Response using JSONPath extractor
entries = ["e-1553","e-1552","c-1052","e-1551","c-1050",
"e-1550","c-1049","e-1549","c-1051","e-1548",
"c-1048","e-1547","c-1047","e-1546","c-1045",
"e-1545","e-1544","c-1046","e-1543","e-1542",
"c-1026","e-1541","e-1540","e-1539","e-1538",
"c-1025","e-1537","e-1536","c-1024","f-1535",
"f-1534"]
I want to Iterate only over those entries that start with "e-" e.g. "e-1553,e-1552" etc. in my ForEach Controller and exclude other entries such as "c-1052, c-1050" etc.
So that I can use http://somesite.com/e-1553 etc.
How do I do that?
Given you have "entries" variable which holds that JSONArray you can get all the entries which start from "e-" as follows:
Add a Beanshell PostProcessor after the JSONPath Extractor
Put the following code into the PostProcessor's "Script" area:
JSONArray array = JSONArray.fromObject(vars.get("entries"));
int counter = 0;
for (int i=0;i < array.size();i++) {
String s = array.get(i).toString();
if (s.startsWith("e-"))
{
counter++;
vars.put("entry_" + counter, s);
}
}
It will produce variables like:
entry_1=e-1553
entry_10=e-1544
entry_11=e-1543
entry_12=e-1542
entry_13=e-1541
etc.
Then add ForEach Controller and configure it as follows:
Input variable prefix: entry
Start index for loop: 0
Output variable name: current_entry
Then in HTTP Request use ${current_entry} in the Path.
See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting in JMeter.

Resources