How to make jmeter while controller work - jmeter

I'm having trouble getting the while controller to work in jmeter.
I've a feeling that I read that it doesn't re-evalute user defined variables, so I am trying to use properties instead.
I start off by using a BSF assertion to set a property called keepLooping
${__setProperty(keepLooping, true)};
This seems to work as it enters the While controller with a condition of
${__property(keepLooping)}
But I cannot for the life of me get it to change that property to something else. I want it to change the property depending on the resulting text of an http request.
So I am using a Regular Expression Extractor to set a variable, which I can see is getting set. Then I am trying to use a BSF assertion to set the keepLooping property on the basis of the variable that I have set. I am using javascript as follows:
log.info("IM IN HERE");
log.info("props is "+props);
//log.info("props keep looping is "+props["keepLooping"]);
if (${surveyRequired} == false){
log.info("IM IN HERE 1A and props is "+props);
${__setProperty(keepLooping, true)};
log.info("IM IN HERE 1B");
}
else {
log.info("IM IN HERE 2A");
${__setProperty(keepLooping, false)};
log.info("IM IN HERE 2B");
}
I can't figure out how to set the property with javascript - I've tried several things. Can anyone help? Many thanks!
Also can anyone recommend a good resource that negotiates what seem to be the many 'quirks' of jmeter? Many thanks!

"I've a feeling that I read that it doesn't re-evalute user defined variables" -- I use JMeter 2.9 and it really does. I use user defined variable in order to count number of loops. It looks like: ${__javaScript(${MY_USER_DEFINED_VARIABLE}>0)}. The only one annoying thing is that I have to get value of variable, increment it, cast to string (toString() in Groovy), and then put new value into MY_USER_DEFINED_VARIABLE (by using vars.putObject("MY_USER_DEFINED_VARIABLE",localBSFVariable))

Using vars.put or props.put will help, as explained in detailed in detail in this jmeter thread.

Related

postman - locking random value, same value for every test in same collection

Is there a way to 'lock' random value in postman? I would like to generate one random value and use same value for every test in same collection.
this code generate different random value for every test:
pm.environment.set("random", _.random(0,9999999));
You could you use this in the pre-request:
let randomNum = pm.variables.replaceIn("{{$randomInt}}")
pm.variables.set("randomNum", randomNum)
Then use {{randomNum}} in the requests.
Solution to my problem was setting global variable in 'pre-request script' only for first request of the collection, and leaving 'pre-request script' empty for every next request:
pm.globals.set("randomNum", _.random(0,9999999));
But thanks for your comment anyway #Danny Dainton. I should use 'request' instead of 'test' in my question. What probably misleaded your thinking.

How to set and get environment variables in Jmeter to test API's

I need to set and get variables in Jmeter for API automation.
I am using the groovy script for same.
I have achieved same using code as below:
import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("PC_CREATED_PROMO_CODE", "shubhamvalue");
log.info("will it work? ="+JMeterUtils.getProperty("PC_CREATED_PROMO_CODE"))
Now the problem is I am not able to see the value in any contanier where I can set my hardcode values like token, baseURL, Headers. it should be similar we do in SOAP-UI or postman tests.
Please let me know if I can see these setProperty values in file/section/container in Jmeter.
Or suggest me any other workaround which is more feasible for same.
Any workaround will be helpful and appreciated in advance.
If you need to get and set variables I would recommend using vars shorthand
As per documentation
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads, and need to be referenced using the __P or __property function.
So I would suggest setting variables as: vars.put('foo', 'bar') and accessing them as ${foo} where required as my expectation is that you will be getting different PC_CREATED_PROMO_CODE for each thread (virtual user)
Also be aware that it is also recommended to avoid scripting where possible so consider going for JSON Extractor instead.
To view properties in file/section you can use 2 functions __property or__P while the second will return 1 as default. in your case
${__property(PC_CREATED_PROMO_CODE)}
${__P(PC_CREATED_PROMO_CODE)}
For example you can change next sampler name to Post2 ${__property(PC_CREATED_PROMO_CODE)}
BTW you can set property use props instead
props.setProperty("PC_CREATED_PROMO_CODE", "shubhamvalue" )
Example of use in HTTP Header Manager, adding cotentType from property:
1) In JMeter GUI mode, under WorkBench, create Property Display by
WorkBench > Add > Non-Test Elements > Property Display.
Then select JMeter Properties checkbox to view all the exist properties
props.put("shubhamKey", "shubhamValue")
When you execute this code the property will set in a property file and you can see it in below location:
WorkBench > Add > Non-Test Elements > Property Display.
2) Now if you are want to use User Defined Variables in your scripts you can call value like below:
vars.get("shubhamUserKey")
Still looking to set the value from code in User Defined Variables

Can we control Jmeter's components through Beanshell? I want to disable all assertions through one flag. how can I do it?

Can we control Jmeter's components through Beanshell? I want to disable all assertions through one flag. How can I do it?
If any other solution than beanShell then let me know.
The easiest way is running your JMeter test using Taurus tool as a wrapper, it naturally supports JMeter tests, moreover it provides some nice extensions.
Particular in your case you can use Modifications for Existing Scripts functionality which allows enabling or disabling Test Elements
---
scenarios:
modification_example:
script: /your/jmeter/testplan.jmx
modifications:
disable: # Names of the tree elements to disable
- Response Assertion
- Duration Assertion
I see two ways in addition to previous answers:
1) Wrap assertions into If controllers, then - yes, set a flag var & check at the If block.
2) Run JMeter programmatically through JMeter API - here you'd have programmatic access to each and every element in the TestPlan.
Although that way is documented quite poorly while the API model is far from being clear itself.
UPD: some clues for the way of doing the latter
1) Here's the main reference: http://jmeter.apache.org/api/index.html
2) Instantiate engine and load properties:
StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.loadJMeterProperties("/path/to/jmeter.properties");
3) Instantiate SaveService and load your plan (yes, save service is what resposible for that)
SaveService.loadProperties();
File yourplan = new File("/path/to/yourplan.jmx");
HashTree planTree = SaveService.loadTree(yourplan);
4) Here's the point where you can access & work your plan elements, going through the HashTree, retrieving test elements in sub-hashtrees (for elements, see the reference mentioned in p.1) & changing them and/or the test structure (cast to TestElement must be good enough for enabling/disabling).
5) As you got done with it, the rest is straightforward:
jmeter.configure(planTree);
jmeter.run();
That should be pretty much it.
From my knowledge, you can not disable all Assertions in the Test Plan by using BeanShell
The work around is as follows:
Create a variable as processAssertions in User Defined Variable config element.
Keep All Assertions inside If Controllers.
Add condition as ${processAssertions}==true, so Assertions will be evaluated ONLY when you set the processAssertions value to true. Set any value other than true, to make JMeter to ignore Assertions.
Using Bean Shell Assertion:
Pre-condition: create processAssertions (Set to TRUE) in User Defined Variables
import org.apache.jmeter.assertions.ResponseAssertion;
log.info("hello");
try{
ResponseAssertion obj = new ResponseAssertion();
if(${processAssertions}==TRUE) { // value accessed from UDV
log.info("inside if");
obj.Enabled=false;
SampleResult.setSuccessful(true); // set sample result to PASS, set to false to mark it failure.
}
} catch(Exception e) {
}
If Controller - with assertions:
If Controller - without assertions:
References:
https://www.blazemeter.com/blog/how-use-jmeter-assertions-3-easy-steps
http://jmeter.apache.org/usermanual/component_reference.html#assertions

Trying to generate JMeter Test Plan (jmx) With JMeter API : Not able to save CSVDataSet element

I am creating a JMeter jmx file dynamically by using JMeter APIs. I am able to add a ThreadGroup within a TestPlan and a JavaSampler within the ThreadGroup. But when I add a CSVDataSet element within the Java Sampler, it does not get saved properly.
The following code is used to create a new CSVDataSet element
CSVDataSet csvDataSet = new CSVDataSet();
csvDataSet.setName("CSV Data Set");
csvDataSet.setComment("Sample CSV Data Set");
csvDataSet.setDelimiter(",");
csvDataSet.setFileEncoding("");
csvDataSet.setFilename("d:\\jmeter\\data.csv"); // variable
csvDataSet.setQuotedData(true);
csvDataSet.setRecycle(true);
csvDataSet.setShareMode(shareMode.all);
csvDataSet.setStopThread(false);
csvDataSet.setVariableNames("firstname, lastname, email"); // variable
csvDataSet.setEnabled(true);
When this is saved using SaveService.saveTree, the final jmx does not contain all the values which were set.
<org.apache.jorphan.collections.HashTree>
<CSVDataSet testname="CSV Data Set Config" enabled="true">
<stringProp name="TestPlan.comments">Sample CSV Data Set Config</stringProp>
</CSVDataSet>
<org.apache.jorphan.collections.HashTree/>
As seen above, only the test name, enabled, and comments are added. The rest of the variables are completely ignored.
Is there something that needs to be set in order to get all the values as expected?
or is this a bug in JMeter? I am using version 2.11
The basic code is as per section 4.3 from following link
http://blazemeter.com/blog/5-ways-launch-jmeter-test-without-using-jmeter-gui
To that I add the code shown above. The way it is added is,
testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("httpSampler", httpSampler);
testPlanTree.add("csvDataSet", csvDataSet);
SaveService
.saveTree(testPlanTree, new FileOutputStream("d:\\test.jmx"));
output of CSVDataSet block is as shown above.
After looking into the JMeter source code, it seems all the properties are set using the setProperty function rather than the individual setter functions. So putting the following code does the job of creating the CSVDataSet element properly.
csvDataSet.setProperty("delimiter", ",");
csvDataSet.setProperty("fileEncoding", "");
csvDataSet.setProperty("filename", "d:\\data.csv");
csvDataSet.setProperty("quotedData", true);
csvDataSet.setProperty("recycle", true);
csvDataSet.setProperty("shareMode", "shareMode.all");
csvDataSet.setProperty("stopThread", false);
csvDataSet.setProperty("variableNames", "var1, var2, var3");
Not sure why setters are not used in the code, but this seems to be the way to go for now
It is clearly not a bug in JMeter otherwise CSV Data Set could not be saved.
It is probably an issue in the way you build the HashTree, but unless you show the full code, you cannot get help.
By the way, as I said in a previous answer, what you are trying to do to build different tests based on input parameter is not good idea IMHO, the approach will be very fragile towards upcoming versions of JMeter.
JMeter provides ways to do it that you should follow.

Jmeter while controller doesn't seem to evaluate variables as numbers

I am writing a jmeter script that keeps loading data until a table reaches a specified size. I have a while loop, in which I have one HTTP Sampler to loads the data, then another HTTP Sampler with an XPath Post-processor to check the table size (they call two different APIs). The reference variable of the XPath Post Processor is currentSize and I have a user defined variable maxSize, but using ${currentSize} < ${maxSize} as the condition for the while loop creates an infinite loop.
Thinking maybe the problem is that the output of XPath is a string, I've tried doing various things in beanshell to coerce it to a number, but I'm a beanshell noob, so I haven't been successful with that either. Can anyone guide me about how to get jmeter to recognize a variable as a number? (Preferably a decimal, but if I have to round to an int, I can live with that.)
Thanks!
I think using __javascript(parseInt()) should suffice for you to check the condition.
e.g.
${__javaScript(parseInt(${time_elapsed_string}) < parseInt(${duration}))}
Assuming that you have following variables:
currentSize
maxSize
continue
where continue is set via User Defined Variables and has the value of true
You can use following Beanshell code to check if current size is equal or greater than maximum size:
import java.math.BigDecimal;
String currentSize = vars.get("currentSize");
String maxSize = vars.get("maxSize");
BigDecimal currentSizeNumber = new BigDecimal(currentSize);
BigDecimal maxSizeNumber = new BigDecimal(maxSize);
if (currentSizeNumber.compareTo(maxSizeNumber) > -1){
vars.put("continue", "false");
}
Make sure that following criteria are met:
Your While Controller has ${continue} as a condition
Beanshell Sampler, Pre / Post Processor or Assertion with the code above is added as a child of the While Controller
See How to use BeanShell guide for more details and kind of Beanshell cookbook.
Everything should work this way.
Hope this helps.

Resources