User Beanshell processor to write code so that data driven testing performed rather than using csv - jmeter

I want to test a Get Request with list of values. I dont want to use CSV ,
so i started using Beanshell Preprocessor and has those values in Array. Then used for loop to use those values and send to Get Request in HTTP Request. Every this was Successful except sending values to Get request. It is reading all values and sending the last read value to Get request.
Question : I want my request to run for each value when code reads the data one by one.
var TtValuedetails;
int i;
n=22;
String[] ttvalue = {"34324324224","fdadsfadsf","dfdsfdsfds","dafadsfa",
"45435435","dfadsfads"
};
for(int i=0;i<n;i++)
{
if(i==0)
{
TtValuedetails=ttvalue[i];
if(ttvalue[0]=="34324324224")
{
vars.put("TtValuedetails",TtValuedetails);
log.info(TtValuedetails);
log.info("first value is executed" );
Org.Apache.......startRequest();
}
}
} ;

We cannot help you without seeing the full code and knowing what you're trying to achieve but one thing is obvious: you should not be using Beanshell, since JMeter 3.1 it's recommended to use Groovy for scripting.
Current problem with your code is:
ttvalue array contains 6 elements
n=22
the line TtValuedetails=ttvalue[i]; will cause failure on 7th iteration of the loop due to IndexOutOfBoundsException
If you want to send a request for each value of the ttvalue array the easiest is converting it into separate JMeter Variables like:
ttvalue_1=34324324224
ttvalue_2=fdadsfadsf
etc.
and using ForEach Controller for iterating the values.
Example Groovy code for storing the values into JMeter Variables:
String[] ttvalue = ["34324324224", "fdadsfadsf", "dfdsfdsfds", "dafadsfa",
"45435435", "dfadsfads"];
for (int i = 1; i <= ttvalue.size(); i++) {
vars.put("ttvalue_" + i, ttvalue[i - 1]);
}

Related

Jmeter- how to pass a fixed number of values from an array at a time until iterating through all values to a http request

In my get htttp request, I get an array of ids ex: list=[1, 2, 3.....1000]
And then for my next http request, I want to pass all the values in the list 10 at a time, so it will be 100 requests total and each time, it takes 10 values from the list array. I'm using a loop controller to call the http request 100 times. but I dont know how to retrieve 10 values at a time, and then go to the next 10, next 10 until all values are used.
How should I do that?
Add a regular expression extractor to your get request as a child.
Add the following properties shown in the screenshot to extract List
Add a Beanshell sampler/ JSR223 sampler and add the following code.
The code below creates a series of variables and store series of 10 values in a variable starting from Final_0 to Final_99
import java.util.Arrays
String complete_List=vars.get("List");
String[] complete_List_Array = complete_List.split(",");
int i;
for(i=1;i<=complete_List_Array.length;i++)
{vars.put("List_"+i,complete_List_Array[i-1]);}
int j;
int loopcount=complete_List_Array.length/10;
vars.put("loopcount",Integer.toString(loopcount));
for(j=0;j
{
StringBuilder sb = new StringBuilder();
sb.append("[");
for(i=j*10+1;i<=(j+1)*10;i++)
{
sb.append(vars.get("List_" + i));
if (i < (j+1)*10)
sb.append(",");
}
sb.append("]");
vars.put("Final_"+j,sb.toString());
}
vars.put("counter","0");
Add a loop counter and mention loop count as ${loopcount} as shown below
You can add HTTP Request as a child of loop counter and to pass series of 1o value at a time use ${__V(Final_${counter})}
Add a beanshell post processor to the http request to that will increment the counter
add the following code to the beanshell sampler
int counter = Integer.parseInt(vars.get("counter")) +1;
vars.put("counter",Integer.toString(counter));
You can see in the results its passing series of 10 values at a time and loop will run for 100 times
For more info on beanshell please follow the link
Please let me know if it helps.........
Considering you have the list array available at your disposal by using any extractor.
Extract the sublist from the array and put them in properties. Then, fetch the properties where ever you want it.
In the below I am fetching the sublist from the array and putting it in jmeter properties.
Here, I am fetching the values from the properties. This is just for demonstration and you dont need this. After putting list in properties just fetch in HTTP sampler as shown in the last image.
Now, to fetch it in HTTP sampler you can use loop and counter and fetch the properties using groovy. Loop for iteration and counter for increment the variable mylist_x.
Hope this helps.

Jmeter - How to handle duplicate values captured in RegExpression from the same response

How to ensure/handle duplicate values are not getting captured while using regular expression?
In my scenario i need to capture multiple offers from my response. But cant use the same offers again and again within the transaction
There are multiple ways to do this. If you are using the default Regular Expression Extractor, the problem is it creates the variables (eg: offer_1, offer_2 etc), ready to go with the execution. It would've been easier if it returned a ArrayList of some sort where we can remove the duplicates. What I am about to suggest is to add those variables to a list in a JSR223(groovy) sampler/post processor then convert them back to usual jmeter variables to use in the usual jmeter script flow.
Snippet:
I created a sample script as per your description which would return multiple offers with some duplicates. Following is the state of jmeter variables before post processing.
offer_1=RUSSIA
offer_1_g=1
offer_1_g0=offer="RUSSIA"
offer_1_g1=RUSSIA
offer_2=UK
offer_2_g=1
offer_2_g0=offer="UK"
offer_2_g1=UK
offer_3=ICELAND
offer_3_g=1
offer_3_g0=offer="ICELAND"
offer_3_g1=ICELAND
offer_4=USA
offer_4_g=1
offer_4_g0=offer="USA"
offer_4_g1=USA
offer_5=UK
offer_5_g=1
offer_5_g0=offer="UK"
offer_5_g1=UK
offer_6=USA
offer_6_g=1
offer_6_g0=offer="USA"
offer_6_g1=USA
offer_7=USA
offer_7_g=1
offer_7_g0=offer="USA"
offer_7_g1=USA
offer_matchNr=7
As you can see above there are duplicates in the variables. Put the following Groovy code in a JSR223 post processor with groovy as language selected.
// Count of offers extracted by Regular Expression Extractor
def count = Integer.parseInt(vars.get("offer_matchNr"))
// An empty list which will store the offers
def offer_list = []
for (int i = 1; i <= count; i++){
def offer = vars.get("offer_" + i)
offer_list.add(offer)
}
// Removes the duplicates in the list
offer_list.unique()
// Following one liner adds new variables but with only unique offers in similar format as jmeter variable.
offer_list.eachWithIndex{ it, index -> vars.put("unique_offer_${index+1}", "${it}")}
After post processing:
unique_offer_1=RUSSIA
unique_offer_2=UK
unique_offer_3=ICELAND
unique_offer_4=USA

How to get Maximum value from correlated IDs in Jmeter?

I have scenario where I need to capture dynamic Order-value IDs from response and among those values, I need to select maximum value and pass it to the next request.
How can we achieve this with the help of Jmeter tool?
Assuming you have the following JMeter Variables from the PostProcessor:
foo_1=1
foo_2=5
foo_3=10
foo_matchNr=3
You can get the maximum value as follows:
Add JSR223 PostProcessor as a child of the request, make sure it goes after the PostProcessor which returns your Order-ID values
Put the following code into "Script" area:
List values = new ArrayList()
for (int i=1; i <= (vars.get('foo_matchNr') as int); i++) {
values.add((vars.get('foo_' + i) as int))
}
vars.put('foo_max', Collections.max(values) as String)
Assuming everything goes well you should be able to access the maximum value as ${foo_max} where required.
See Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter tests

How to replace the characters of a variable with some random numbers in Jmeter

I am trying to pick up a variable from Debug Sampler and replace the characters of the variables with some Randomly generated number. I have added a BeanShell Sampler for writing the custom code. Below is the piece of code:
String myvariable = vars.get("Corr_ContextN");
chars1 = new ArrayList();
chars2 = new ArrayList();
for(int i =0; i<myvariable.length(); i++) {
chars1.add(myvariable.charAt(i)); }
String value = chars1.toString();
Random randomnumber = new Random();
for (int idx=1; idx < 15; ++idx) {
chars2.add(randomnumber.nextInt(100)); }
String Newvalue1 = chars2.toString();
vars.put("NewVariable", Newvalue1 );
By the above way I get a New variable in the Debug Sampler (NewVariable) with a list of random numbers. But I want to replace the existing variable "Corr_ContextN" with this NewVariable created. In other words the existing variable should be replaced by some dynamically generated number/variable.
Please help me out.
Just change the last line to be:
vars.put("Corr_ContextN", Newvalue1 );
P.S. Consider using JSR223 Sampler instead of Beanshell. You can even reuse the same code which will perform much better being compiled by Groovy engine.
P.P.S. Be informed that much easier would be using __Random function which is capable of storing randomly generated numbers into JMeter Variables as well as returning them in place where the function is called. Check out How to Use JMeter Functions post series to learn about functions in JMeter.

In jMeter, how to update a parameter value on each occurrence?

I'm facing a situation where I need to build a request string dynamically before sending it in an HTTP sampler. I'm choosing a random number between 15 and 50, and then for that many times, I append an XML tag with a parameter. So if my random number is 22, the this appended string (I call it ricString) will contain the same xml tag 22 times! And all I want is for it to use 22 different parameter values from the CSV file. But it doesn't do that. It takes the same value 22 times, and then uses the next value in the next iteration. Here is what I have written in my beanshell pre-processor.
counter = ${__Random(15,50)};
i = 0;
String ricString;
while(i<counter)
{
i++;
ricString = ricString + "<req:RCS>${__StringFromFile(...\RIC_3_01_Flag.csv)}</req:RCS>";
}
I have tried using both __StringFromFile as well as __CSVRead(filename, next) functions but no luck. It just does not update the value when inside the while-loop. Anyone know what I'm doing wrong?
Use a CSV DataSet that you nest into your loop (this is very important).
Then just use that variable that your CSV DataSet defines in your XML.

Resources