JMeter - compare assersion between two variables - jmeter

I have two variables (create_date) took from SQL queries, and I want to validate the results.
Using Regular Expression I fetch both created date and I want to make sure that the dates is not equal.
I have created the following BeanShell assersion to compare both variables:
enter image description here
however, I'm getting the following error:
enter image description here

You're mixing 2 approaches:
Referencing variables like ${CreateDateBefore} in scripts (which you should not be using at all)
And using vars shorthand for JMeterVariables class instance
Apart from this you're using double to store some form of a timestamp
So you need to update your code like:
String var1 = vars.get("CreateDateBefore");
String var2 = vars.get("CreateDateAfter");
Also be aware that since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating to JSR223 Assertion
And last but not the least, you can compare 2 JMeter Variables using "normal" Response Assertion configured like:

Related

How to pass long string value variable in a Doc file using beanshell post processor in Jmeter

The following is the code, I have added in my Beanshell Post Processor:
import org.apache.jmeter.services.FileServer;
String path=FileServer.getFileServer().getBaseDir();
String var1= vars.get("Others");
String var2= vars.get("Recommendations");
f = new FileOutputStream("C://apache-jmeter-5.4.1/apache-jmeter-
5.4.1/bin/Output_IndustryType.csv",true);
p = new PrintStream(f);
this.interpreter.setOut(p);
p.println(var1+ "," +var2);
f.close();
But the problem is that the value obtained from these two variables, is a long text, so in the CSV file it is coming in each column as a single text.
And value of Var1, is not coming in the CSV file, and when checked using Degub Sampler it is extracted properly.
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so it worth considering migrating and it might be the case it will resolve your issue
If you plan to run your test with > 1 thread (virtual user) the approach is not valid at all as you will be getting random failures and unexpected behaviour due to race conditions when 2+ threads will be concurrently writing into the same file so it makes more sense to consider declaring these Others and Recommendations guys as Sample Variables and store them into the CSV file using Flexible File Writer plugin

How to generate Random number in Jmeter with prefix and store it into variable

I am using JMeter's Function Helper Dialogue and i found below syntax -
Syntax - ${__Random(12,99,customer-id)}
Result - random values b/w 12 to 99 getting generated, which will get stored in variable "customer-id"
Now Problem is I have to generate value with prefix 'test' (say-test12) and store it in variable 'customer-id'
How to do that ?
I don't see why would you need this because JMeter Variables can be normally concatenated with plain text so if you need test12 just use test${customer-id} statement where required.
However if you really need to generate some static text followed by a random number you could go for __groovy() function configured like:
${__groovy('test' + org.apache.commons.lang3.RandomUtils.nextInt(12\,99),customer-id)}
Demo:
More information:
RandomUtils JavaDoc
Apache Groovy - Why and How You Should Use It

Jmeter. __base64Encode function doesn't encode in JSR223 Sampler

Every time I need to achieve something in Jmeter, I ask a question in SO...
To make a story short: my goal is to read Base64 encoded values from csv file line by line and save them into separate variables. According to this page (in order to decode them), there are functions ${__base64Encode} and ${__base64Decode}. I installed Custom Jmeter Functions Plugin and restarted the Jmeter. Another referral to this answer, there is an advisory to use JSR223 PostProcessor / Sampler, verbatim:
it is recommended to use Groovy for any form of scripting to consider
migrating to JSR223 PostProcessor
so I decided to give it a try. Below is my script to read csv files:
def csvFileLocation = new File("C://JohnDoe//MyWork//sensitive_data.csv")
def lines = csvFileLocation.readLines()
lines.eachWithIndex {line, idx ->
vars.put("Id_value_" + idx, line)
}
${__base64Decode(${Id_value_0}, first_variable)}
${__base64Decode(${Id_value_1}, second_variable)}
${__base64Decode(${Id_value_2}, third_variable)}
Then I saw something interesting:
2018-01-01 12:30:60,767 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in
JSR223 script JSR223 Sampler, message: javax.script.ScriptException:
org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed: Script10.groovy: 8: unexpected token: ! # line 8,
column 1. !ßïj[žÿ
I simply decided to test it on https://www.base64encode.org/ with a string this is the test of base64 encoding
However, Jmeter _base64Encode function produces completely different result:
vars.put("jmeter_variable", "this is the test of base64 encoding");
${__base64Encode(${jmeter_variable}, my_variable)}
log.info(${my_variable});
OUTPUT:
JHtqbWV0ZXJfdmFyaWFibGV9
My question: what type of encoding algorithm Jmeter uses? Should I be able to save already encoded values into the csv file and retrieve them to the original value with the help of __base64Decode function? Thanks for the help...
You need to replace these lines:
${__base64Decode(${Id_value_0}, first_variable)}
${__base64Decode(${Id_value_1}, second_variable)}
${__base64Decode(${Id_value_2}, third_variable)}
With these ones:
vars.put('first_variable', vars.get('Id_value_0').decodeBase64() as String())
vars.put('second_variable', vars.get('Id_value_2').decodeBase64() as String())
vars.put('third_variable', vars.get('Id_value_2').decodeBase64() as String())
vars is a shorthand for JMeterVariables class instance, it provides read/write access to JMeter Variables in current test element's scope.
As per JSR223 Sampler documentation you should avoid inlining JMeter Functions and/or variables into Groovy scripts as they may resolve into something which can cause your script compilation failure (like in your case) or unexpected behaviour. Moreover it does not align with Groovy's compilation caching feature therefore performance of your Groovy script even if everything will be fine from syntax perspective will be a big question mark.
So the most "natural" way from Groovy perspective will be using String.decodeBase64() function like demonstrated above. See The Groovy Templates Cheat Sheet for JMeter article to learn what else could be done with Groovy and how.

Jmeter : random number with beanshell

I want to add 2 kind of parameters to a post request.
From an http request I extract 2 list of variables:
Order_id = input type="hidden" name="(drag.+?)" value="(\d+?)"
Weight_id = class="draggableviews-weight form-select" id=".+?" name="(drag.+?)"
In the Post Request that follows, I need to repost all this variables. I use a BeanShell PreProcessor for this. The first list is no problem, since this get the same value. The second list should get a new random value between -50 and 50. I also want a different random value for each iteration.
How should I do this ?
If I use Random Variabele Config Element I get the same random int for each variabele. I want a different one for each iteration.
enter image description here
I would recommend using ThreadLocalRandom.nextInt() method like
sampler.addArgument(name2, String.valueOf(java.util.concurrent.ThreadLocalRandom.current().nextInt(-50, 51)));
Don't inline JMeter Functions into Beanshell scripts, either pass them via "Parameters" section and refer via Parameters or args shorthands or use appropriate code-based substitutions.
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on Beanshell scripting in JMeter tests.
To retrieve multiple values from single Regular Expression Extractor, we use Templates as follows:
then refer groups as follows:
In the image, you can see that link is Reference Name and there is only match found with the regular expression.
number of matches is : link_matchNr
first group value : link_1_g1
second group value : link_1_g2
Note: The regular expression I tried on is google.com, you can also simulate the same as follows:
Use Random function as follows:
value2 = ${__Random(-50,50)};
log.info("valuee2 " + value2);
Use Random Variable:
then, refer Output Variable in Beanshell Preprocessor as follows:
value2 = vars.get("randomInt");

Is it possible to include jmeter variables in values obtained from CSV?

I have a csv file which contains a column named "query". One of the entires I have for query is /user/${id}/list/${list}.
What I would like to do is let jMeter overwrite the ${list} and ${id} variables in the query when it is passed to a HTTP Sampler with variable values already in use from previous steps in my test plan.
For example:
In test plan, create ${id} = 5 and ${list} = 10.
In test plan, open csv file that contains query string.
In test plan, perform use a HTTP Sampler. Path in query should be the query value passed from csv file.
3a. Jmeter should take query passed to sampler and replace ${id} and ${list} with the values stored to those variables within test plan (5 and 10).
Right now when I try this, the HTTP response comes back showing the request was made to /user/${id}/list/${list}, not /user/5/list/10.
Does anyone know how to force the substitution through jMeter?
Is it even possible?
I was able to figure this one out after a bit of head scratching.
JMeter allows you to overload variables (place references to variables within a variable) by using the __eval function.
To get around the issue, I left the csv file as is, with references to variables set. When I wanted to reference the query from the csv file and overload the variable placeholders with actual values I used ${__eval(${query})} - where query = the
Try to use __eval function instead:
/user/${__eval(${id})}/list/${__eval(${list})}
__eval function seems to be just your case.

Resources