1

I'm trying to generate my input through JavaScript using BSF PreProcessor. Currently I've only BSF PrepProcessor and a HTTP Request. My Request body goes like this.,

{
    "entity": {
        "id": "${EnitityName}",
        ----
     }
}

The randomly generated string should be the value for ID, i've parameterized how we normaly do it. This is not working. Can somebody tell me where i'm going wrong or any alternative approach.

1
  • FYI, JMeter is not inserting the result of my script into the variable ${EntityName} Commented Mar 2, 2017 at 7:37

2 Answers 2

1

try using __RandomString function: (no need of JavaScript & BSF PreProcessor if you want a random string)

{
    "entity": {
        "id": "${__RandomString(8,abcdefghiz,)}",
        ----
     }
}

From Docs:

Examples:

${__RandomString(5)} will return a random string of 5 characters which can be readable or not

${__RandomString(10,abcdefg)} will return a random string of 10 characters picked from abcdefg set, like cdbgdbeebd or adbfeggfad, …

${__RandomString(6,a12zeczclk, MYVAR)} will return a random string of 6 characters picked from a12zeczclk set and store the result in MYVAR, MYVAR will contain string like 2z22ak or z11kce, …

Sign up to request clarification or add additional context in comments.

Comments

0

Given ${EnitityName} variable is properly set in your BSF PreProcessor script, it should be substituted by the generated value given BSF PreProcessor success.

I would suggest checking jmeter.log file for any errors - it should give you an idea regarding what went wrong. You can also share your BSF PreProcessor code so others and myself could take a look and suggest the fixes.

Actually according to JMeter Best Practices it is recommended to use JSR223 Elements so you should switch to JSR223 PreProcessor and Groovy language, the example code which generates a random alphanumeric string of 10 characters and putting the value into the ${EntityName} JMeter Variable will look like:

vars.put("EnitityName",org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric(10))

References:

2 Comments

Thanks Dmitri.. The code u gave worked. Here is my java script which actually did not work function generateEntityName() { var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for( var i=0; i < 5; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } vars.put("EnitityName", generateEntityName());
I've also tried a code given by you for a similar question asked before. int min = Integer.parseInt(bsh.args[0]); // get first parameter int max = Integer.parseInt(bsh.args[1]); // get second parameter int random = min + (int) (Math.random() * ((max - min) + 1)); // calculate random number within parameters range vars.put("RANDOM_NUMBER", String.valueOf(random));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.