How to capture dynamics values which will be generated from loop in JMeter - jmeter

I have one scenario to test in which one transaction needs to be iterated for several times and then submit the request. For each iteration, I will get one detID (e.g: for 20 iterations - 20 unique detID). The problem is while submitting the request all the detID are been passing in the request parameters (example: if loop ran for 10 iterations then 10 detID are passing inside the request). I have put regular expression extractor to the transaction which is in loop which will capture all the mathces but it is capturing only the last one. (e.g: if loop ran for 10 iteration, regex capturing the 10th iteration value).
Please help me.I want to include these detID inside the submit request.

Add a Counter before the request with the regex.
Add a BeanShell PostProcessor as a child of the request after the regex with the below code:
String ID = vars.get("ID");// ID is the reference name of your regex
String Counter = vars.get("Counter");// Counter is the reference name of your regex
vars.put("ID_"+ Counter, ID);
You will have 20 different variables each one holds a different ID value and you can use them as ${ID_1} for the first ID and ${ID_2} for the second and so on.

Related

Can a request parameter extracted from response of previous request have different values when run multiple times - Jmeter

Scenario:
We are extracting a value from 1st request and passing it as a parameter for the 2nd request.
The 2nd request is in Loop controller and it is run multiple times, But every time the 2nd request runs , It should take different value. Is there any way to do this.
Eg: Below is the example screenshots for the same. data is the variable which is passed to the second request .When second request is hit multiple times , It should extract different values.
In the Regular Expression Extractor set "Match No" to -1 to you will extract all the matches into:
data_1=1
data_2=2
etc.
In the Loop Controller set the "Loop Count" to ${data_matchNr}. This way the controller will iterate as many times as there are matches in the Regular Expression Extractor
Instead of ${data} use ${__V(data_${__intSum(${__jm__Loop Controller__idx},1,)},)}
More information: Here’s What to Do to Combine Multiple JMeter Variables

Jmeter - My response has multiple values of same id. I want to extract and pass these values incrementally to the next request

My response to a save action is somewhat like this-
"diagnosisId":45686,"confidence":0.0, --other text--
"diagnosisId":45966,"confidence":0.0,--other text-- etc. Say there are 27 diagnosis Ids.
Now i want to send request for the 1st Diagnosis Id in loop till the last id (multiple records/rows to save depending upon the diagnosis Ids).
Request is something like this -
"diagnosisId":45686,"confidence":0," etc.
I have extracted the Diagnosis Id using Regular Expression extractor and matched the first value -
"diagnosisId":(.+?),
How do I pass the values incrementally now?
Given you provide "Match No:" as -1 in the Regular Expression Extractor:
You will get the following JMeter Variables as the result (viewable via Debug Sampler):
At this stage you can add ForEach Controller and configure it like:
Input variable prefix: diagnosisId
Output variable name: anything meaningful, i.e. id
That's it, you can reference each consecutive ID as ${id} in the request(s) which will be the children of the ForEach Controller:
Another example: Using Regular Expressions in JMeter

Compare extracted variable across threads in JMeter

My goal is to POST to some URL from every thread in my thread group, which will create an asset somewhere. If all goes right, the first request will create the asset, then subsequent requests will see that the asset is already created (or in the process of being created), and will reuse that same asset.
The test plan:
Create N threads
HTTP Request - POST to some URL
Regular Expression Extractor - extract part of the response (the assetId generated by the POST request)
Verify that every thread extracted the same string from the response
My question:
What I don't have a clue how to do is the last step - verify that the value extracted from each thread is the same. How can this be done in JMeter?
To achieve your requirement, we need to share the value among all the threads.
Properties:
We can use properties to share a value. Lets assume a prop 'shared' is created with default value as blank "". Add the below code in the beanshell assertion. If it is blank, then a thread will add the value extracted from the RegEx. All other threads will just compare the value and if it does not match, it will fail it.
if(props.get("shared")==""){
props.put("shared") = "extracted";
}else{
if(!props.get("shared").equals("extracted")){
Failure = true;
}
}
Bsh.shared:
We can use the bsh.shared shared namespace to share the value among the threads and compare if the all the threads have the same value.
1.setup threadgroup will contain beanshell code like this to create a hashset.
import java.util.*;
if (bsh.shared.hashSet == void){
bsh.shared.hashSet=new HashSet();
}
bsh.shared.hashSet.clear();
2.The regular thread group will contain the code for extracting the value. Once the value is extracted, add it to the hashset which stores only the unique values. Any duplicate values are simply ignored.
bsh.shared.hashSet.add("value extracted");
3.teardown threadgroup will group will check the hashset for the size. If the size is more than 1, then it failed.
log.info(String.valueOf(bsh.shared.hashSet.size()));
I guess you can use Response Assertion.
The test plan:
Create N threads
HTTP Request - POST to some URL
Verify that every request has the same string in the response with Response Assertion
When you place this assertion on the Test plan level it applies to all the threads.

JMeter: Can't copy CSV variable into another variable

I am reading a token from a .csv file into variable CSV_ACCESS_TOKEN. I have 3 request under one ThreadGroup. I want a scenario when logged in user loads a page thrice (or N time). So 1 thread is looping N time. After reading token once, I dont want to read next token in loop but want to loop through URL three (or N) time with same token.
Right now I am reading data from CSV, and using "BeanShell Sampler" inside "Once only Controller". In the sample I am using line like: vars.put("ACCESS_TOKEN",vars.get("CSV_ACCESS_TOKEN"). But that BeanShell sampler is recorded in my Summary Result. I don't want that.
I tried using "User Defined Variable" controller and try to assign the value ${__evalVar(CSV_ACCESS_TOKEN)} but it return empty value for ${ACCESS_TOKEN}. When I use ${CSV_ACCESS_TOKEN}, it shows me the values. If I use some other variable instead of CSV_ACCESS_TOKEN in UDV controller, it assigns the value from other variable and I see values for ${ACCESS_TOKEN}.
Why CSV variable is not assigning the values in regular variable.
Thanks
Vinay
If you have 3 requests, I suggest you put a Beanshell preprocessor on the first request, which copies the CSV_ACCESS_TOKEN to ACCESS_TOKEN.
Each of your samples can the use ACCESS_TOKEN, so CSV is accessed once per cycle of 3.
Each time the preprocessor run (ie before each 1st request), CSV_ACCESS_TOKEN will get updated from the dataset.
If it is the same request, which you do not wish to duplicate, you can look into use of test fragments and modules, so you can run the same sample from a variety of controllers. First from a simple controller with the preprocessor attached, and then from a loop controller to perform 2 more requests.
I think the code you have used already to manipulate the CSV values will continue to work in this scenario.

Modify a Property on JMeter

I have created a Test Plan that has two thread groups. Each thread group has a SOAP/XML-RPC Request Sampler. Thread Group A also has a Regular Expression Extractor that contains:
Reference Name : ABC
Regular Expression :<response>([A-Z 0-9]+)</response>
Template: $1$
Moreover, Thread Group A has a BeanShellAssertion with
Name: Extract value
Script: ${__setProperty(ABC, ${ABC})};
What I want to do is modify the ABC variable and then pass it on the SOAP Sampler of the second Thread Group.
So, if ABC equals 1000 (response tag holds an int) I want to get that value divided it by two and then pass it on the second sampler like :
<abcValue>${__P(modifiedABC)}</abcValue>
Any ideas?
EDIT:
I have tried preProcessors(on the second thread group) and postProccessors (on the first thread group) but whatever I tried gave me back errors like:
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``String value = Integer.toString(Integer.parseInt(72295) /2); props.put("modifiedABC", v . . . '' : Typed variable declaration : Error in method invocation: Static method parseInt( int ) not found in class'java.lang.Integer'
If your response contains anything other than A-Z, ' ', or 0-9, the regex extractor will fail. It may be better to set the match group to (.+), so it collects whatever is in the response and use a separate regex assertion to check the contents are what you expect. That way you will get a sample fail when the results are bad, rather than a subsequent fail, when your next sample is badly formed through bad input.
In the Thread Group A assertion, you need some "s..
${__setProperty("ABC", "${ABC}")}
This sets a property called ABC to the value of variable called ABC, which is what I think you intend.
Easy way to divide your value is with __javaScript() function..
${__javaScript(${ABC}/2)}
You can use this anywhere in jmeter and it will substitute the value you require. Make sure you have retrieved the property at the start of Thread Group B, as the variable (ABC) is in different scope.
Thread Group 1
Please use 'Beanshell Post Processor' for your request. Add this post processor under the sampler where you extract ABC.
Below link will you give you an idea.
http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_PostProcessor
Note that by default everything is String in Jmeter. So you might want to convert it to an Integer before dividing it by 2. You have to add something like this in the Beanshell Post processor.
modifiedABC = Integer.toString(Integer.parseInt(props.get("ABC"))/2);
props.put("modifiedABC",modifiedABC);
Thread Group 2
Now you want to access the modifiedABC in the second thread group.
Simply access it using
${__P(modifiedABC)}

Resources