Logic in JMeter Script to handle padding(space) - jmeter-5.0

Need to pass 34 length value for a field in JMeter.
length of padding = 34-(len(itemcode)+1)
need to have space for padding as per above calculation.
total 34 length for this field= ItemCode+"N"+X ,
the "N" length = 34-len(ItemCode)-1
Item code need to fetch from previous response.

This can be achieved by JSR223 post-processor with below code.
log.info('${prod2name}');
var s= '${prod2name}';
var totallength=34;
var vallength=s.length;
var spacelength=totallength-parseInt(vallength)-1;
var finall=s;
for(i=1;i<=spacelength;i++){
finall=finall+" ";
}
var finall=finall+"X";
log.info(finall.length);
vars.put("z",finall);

Related

Use same random variable in all threads

I've got test plan :
Thread groups ( users 3, loop 2)
Random Variable
HTTP Request
I want variable to be changed only per loop, so under each iteration all three threads should send same value.
So I want something like this :
request where random var = X
request where random var = X
request where random var = X
request where random var = Y
request where random var = Y
request where random var = Y
I tried a lot of workounds but can't find proper solution.
P.S. I don't want to read variables from file. I need to generate them
No matter whatever you "want" the best option would be pre-generating random values somewhere in setUp Thread Group and writing it to the file and then using CSV Data Set Config in the "main" Thread Group to read the values.
However if this is still not something you "want" here is yet another "workaround", hopefully it's "proper" enough for you:
Add JSR223 PreProcessor as a child of the request which you "want" to parameterize with the random variable
Put the following code into "Script" area:
if (props.get('foo_' + vars.getIteration()) != null {
props.put('foo_' + vars.getIteration(), org.apache.commons.lang3.RandomUtils.nextInt(0, 100))
}
Refer the "generated" random value using the following __groovy() function where required:
${__groovy(props.get('foo_' + vars.getIteration()),)}
Demo:

Random number between to values in google-sheets script not honoring max/min values

Just started learning app script/java and i want to get a random number between to values defined in 2 rows in a google sheet (see picture for clarification and debug info) I have the min/max value in two arrays for this purpose. max[x] and min[x]
So when i run this function from the script and outputs the values to my sheet it don't honor the max/min, (var pris = randomnumber(max,min)) but when i run the function from a cell with the same inputs it works as it should (=randomnumber(A1,A2)
The values in max/min are correct in the script as far as i can see in Logger and debug.
max, min have been declared as arrays earlier in the script and contains all the correct numbers, also pris.
So can anyone figure out what is wrong here? This is making me nuts!!!
And i do need the script to write the numbers, not using the function from the cells.
Within a loop
m = max[i];
l = min[i];
pris=randomnumber(m,l);
price.push([pris]);
i++;
Within a loop
function randomnumber(ma, mi) {
// If i uncomment those two lines it works from the scrip, even if the value parameters are the same.
// var mi = 80;
// var ma = 120;
var randomNumber = Math.floor((Math.random() * (ma - mi)) + mi);
return randomNumber;
}

Jmeter how to get value of Httprequest GET at runtime

I have done lot of cases where value is extracted from response of first request and passed to the subsequent request using Regular expression/CSS extractor etc but now i want to extract a value at GET and pass it to same GET (When i click on my page a random code is generated which has to be passed to the same page, this code will be unique for every click). How i can create as a variable and store value of code and pass ?
how to extract GET value from request in jmeter at runtime and pass it as variable. i want to pass this value as variable in same request
See below screenshot
My C# code for encrytion/decrytion is as follows -
string handlerPath = HttpUtility.UrlEncode(appPath + "/ReportFileUpload.ashx");
string cipherText = string.Empty;
string passPhrase = "!MMFileUploader123"; // can be any string
string initVector = "2038459710286532"; // must be 16 bytes
// Before encrypting data, we will append plain text to a random
// salt value, which will be between 4 and 8 bytes long (implicitly
// used defaults).
RijndaelEnhanced cipher = new RijndaelEnhanced(passPhrase, initVector); //, 4, 16, 256, "SHA1", DateTime.Today.ToBinary().ToString());
cipherText = cipher.Encrypt("?rn=" + repNum + "&uid=" + user.UserId + "&op=" + originPath + "&hp=" + handlerPath + "&p=" + pass + "&domain=" + domain + "&org=" + org + "&entitytype=" + entityType);
cipherText = HttpUtility.UrlEncode(cipherText);
//Decrypt
//decipher the code get the parameters and assign them to inParams
string passPhrase = "!MMFileUploader123";
string initVector = "2038459710286532"; // must be 16 bytes
RijndaelEnhanced cipher = new RijndaelEnhanced(passPhrase, initVector);
string cipherText = HttpUtility.ParseQueryString(
ApplicationDeployment.CurrentDeployment.ActivationUri.Query)["code"];
inParams = cipher.Decrypt(cipherText);
You need to have a pre-generated random code stored in CSV (CSV Data Set Config) and pass it to the request.
Ask your developer about the logic to generate the random code, it should be similar to some md5 encryption.
Use JSR223 Pre processor to generate random value before your request and pass that value to your request.

How to call Jmeter Counter Reference Name variable incrementing one

In my testplan, I'm using Counter config element and have configured start = 1, increment = 1, maximum = 4 and the ReferenceName = loopCount.
And in a sampler, I have used Json Extractor as well where I want to set the
Match No: ${loopCount} + 1
since in the Json file I'm extracting always the first match is null.
Unfortunately I think I can't give it like
${loopCount} + 1.
Is there any work around for this.. please help.
You can add a BeanShell Sampler after the Counter config element with the below code in the code area:
int Counter = Integer.parseInt(vars.get("loopCount"));
vars.put("Increment", String.valueOf(Counter+1));
And now you can use the variable ${Increment} as the Match No.
OR
You can use ${__intSum(${loopCount},1)} which will add 1 to the value of loopCount variable directly.

how to use variable in jmeter like counter

I'm using loop controller inside the jmeter script, and I'm not able to fetch previous variable value in pre-processor beanshell.
Example:
var temp = 1; log.info("before : "+temp.toString()); temp++; prev.put("t",temp.toString());
Thanks in advance
To save values b/w iterations:
Following is one of the ways to store and retrieve the values b/w iterations:
log.info("temp prev value " + vars.get("temp")); // first iteration returns null
vars.put("temp","something"); // store or override the value, so it will be available in next iterations.
To know iteration number:
If your need is to know the iteration number, then use Counter:
In beanshell preprocessor, access using reference name (counter) as shown below:

Resources