For another function, please refer Functions and Variables post
The RandomString function returns a random String of length using characters in chars to use.
Parameters
1. Context
As you know, while working with JMeter, we’re working with the requests. And each request, they should contain some parameters. The parameter, it might be required or not, but sometimes, we don’t care about the value, it can be anything, just a string is enough.
Let’s talk about a specific case, a registration request in Test Plan needs some information like First Name, Last Name, Email, Password, Phone Number, Address, etc. In that case, you can see, we don’t prefer to input fixing value into First Name, Last Name, Phone Number, Address all time like this:
First Name: Tom
Last Name: Tran
Phone Number: 0987654321
Address: 123 ABC Street, District 1.
All these data can be populated randomly. It makes the request, the test more realistic. Let see how __RandomString help us.
2. Using
To generate a random string, you should specify these two things:
- Define how many characters do you want to have in that string (aka string length), including special character.
- Which characters do you want to use to generate the string?
2.1 Basic Random String
2.1.1 Requirement 1: I would like to random a String with length is 10, and using these characters A, B, C, D, E, F
Solution: Click button in the icon toolbar to open Functional Helper dialog
– Select __RandomString in the “Choose a function” dropdown list
– Input the length into “Random string length”: 10
– Input the characters which using for random into “Chars to use for random string generation”: ABCDEF
Remember: input the characters sit close together without any space or any other special characters
– The last field Name of variable is just an optional field, we can leave it blank.
– Then click Generate button at the bottom left corner of the dialog to generate the needed function.
– Copy and paste the generated function into where you want to use.

Now we have the function ${__RandomString(10,ABCDEF,)}
. Apply this and run, below is the result:
You can see, we have a string with 10 characters, and all of them are made from these characters: A, B, C, D, E, F.
2.1.2 Requirement 2: I would like to random a String with length is 10 and using all alphabet character, then store the generated string into a variable called MY_STRING.
Solution: We’ll do as the requirement 1, but the different thing is the chars to use, we’ll input fully alphabet characters into that field.
Now we have the function:
${__RandomString(10,QWERTYUIOPASDFGHJKLZXCVBNM,MY_STRING)}
Apply this string and see what we have:
We have a string of 10 characters every time the test ran. After that, the generated string will be stored into variable MY_STRING, so when we call this variable by ${MY_STRING}
, it will return the generated string above.
—
NOTE: But, did you realize that we input the uppercase letter, it will random the uppercase letter only? Then in case you want to random both uppercase and lowercase also, you must input both of them into the “Chars to use” field. The function now looks like:
${__RandomString(10,qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM,MY_STRING)}
2.2 Random Email
Could you apply the rule above to generate an email? I bet, at least one of you, will have a thinking: how to generate the string which contains the @ character, and domain name also.
Don’t follow that way, it’s very easy. Let see an example below:
${__RandomString(10,qwertyuiopasdfghjklzxcvbnm,)}@gmail.com
Now we have a random email, it combines the random string and a fixed value @gmail.com
If you don’t want to specify the domain name, we can random it also:
${__RandomString(10,qwertyuiopasdfghjklzxcvbnm,)}@${__RandomString(3,qwertyuiopasdfghjklzxcvbnm,)}.${__RandomString(2,qwertyuiopasdfghjklzxcvbnm,)}
But I don’t prefer this way.
And if you want to call that email for later use, just prepare the function:
${__RandomString(10,qwertyuiopasdfghjklzxcvbnm,MY_EMAIL)}@gmail.com
${__RandomString(10,qwertyuiopasdfghjklzxcvbnm,MY_EMAIL)}@gmail.com
Then call it:
${MY_EMAIL}@gmail.com
2.3 Random Phone Number
A phone number is just a number. So, in case you want to random a phone number, please input the number into “Chars to use” field. We have a function like below:
${__RandomString(10,1234567890,)}
Sometimes, your phone number must start with a specific number, for example, 09xxxxxxxx. Then how to achieve it? Very easy, we just specify that fixed number, before random the rest number behind.
09${__RandomString(8,1234567890,)}
Apply this function and run few times, let see the result:
Now we have the phone number exactly like we expected.
2.4 Random Special Characters
Using the same way, we can have a random string with all sepecial characters. All we need is: input the list of characters into “Chars to use” field. But there is a special character is real special characters with the function, that’s comma “,”. So if you want to use this character for randomizing, please escapes it: \,
.
${__RandomString(15,`-=~!@#$%^&*()_+[]\;'\,./{}|:"<>?,)}
2.5 Random String with Random Length
For the above example, all of them have fixed length. You input 10 into “String length” field, then you always get a random string with the length of 10. To make it more efficient, you might want to have a string with random length also. For example, the string length should be ranged from 5 to 10 characters. How to do it?
Answer: Very easy, just input another function __Random into the “string length” field, that function will help to return a number in a range, then __RandomString will use that number to generate the string.
${__RandomString(${__Random(5,10,)},qwertyuiopasdfghjklzxcvbnm,)}
Let see how it works, I’ll run the test 5 times:
Each time the function was called, it will help to generate a random string with the length is 5 to 10.
With the help of this post, hope you all can save the time to your work, and also make the test more realistic. If you have any concern or something else need to discuss, please feel free to leave the comment below.
For another function, please refer Functions and Variables post.
Nice explanation.
Hi Sir,
I want to get the random values from list box for multi iterations, Kindly share the steps.
Thanks in advance
Laxman
LikeLike
Hi, I have to create multiple JSON requests within 1 HTTP request…
The external key, email and phone number should be unique in every JSON request.
Eg:
[{ “externalKey”: “1”, “customerDetails”: { “address”: { “address”: “111 Bourke St”, “country”: “AU”, “postcode”: “3044”, “state”: “Victoria”, “unitOrBuilding”: “1” }, “emailAddress”:”loadtest1@gmail.com, “phoneNumberInternational”: “+61360000001”
},
{ “externalKey”: “2”, “customerDetails”: { “address”: { “address”: “111 Bourke St”, “country”: “AU”, “postcode”: “3044”, “state”: “Victoria”, “unitOrBuilding”: “1” }, “emailAddress”:”loadtest2@gmail.com, “phoneNumberInternational”: “+61360000002”
}]
Currently, I am preparing the data outside JMeter and then copy/pasting the it in the HTTP request… Can you please suggest a way to create the data automatically in JMeter.
I need to trigger a HTTP request every 20 min for 8 hrs with 200 bulk JSON requests in each (unique key, email,phone).
LikeLike
can i user this MY_STRING variable in regular exprssion if i need to extract value for respective, if yes how.
LikeLike
Testing
LikeLike
оооо ты хорошо выглядишь в этой сети вау
LikeLike