Consider we
have a situation where we want to generate a random string of say length 5 (let’s
say name of an employee) on the fly during our execution. Now one easy way
would be to enter all possible permutations and combinations in our parameter file
which would be 265 which would be roughly around 12 million. Well
not exactly an “EASY” way. So another method is using lr_param_sprintf()
function.
Let’s take
an example: I want to randomize a name for length 4 (like AGBG, HTGF, GAHS,
etc.) which I want to use in my script later on. So, I will add the below code
at the beginning of my script to generate a random name.
Now
lr_param_sprintf() function is just like standard C function sprintf(), only
difference being that unlike the latter (which writes the output to a string
buffer) the former’s output is written to a LoadRunner parameter.
Syntax: int lr_param_sprintf(const char *paramName,
const char *format[,args….]);
This
function returns a 0 on success, so if you get any non-zero value you know your
function has failed.
Prerequisite:
You do need to prepare somethings before you can use this function.
If you are
using alphabets to create random string, then you should have a parameter with all
26 alphabets* stored in it and the same logic can be applied to anything and
everything you want to randomize. Refer below screenshot for parameter file settings.
![]() |
Fig 1. Parameter File settings |
lr_param_sprintf (“p_RandomName”, “%s%s%s%s”,
lr_eval_string(“{p_RandomLetter}”),
lr_eval_string(“{p_RandomLetter}”),
lr_eval_string(“{p_RandomLetter}”),
lr_eval_string(“{p_RandomLetter}”));
- “p_RandomName” is the name of the parameter where we want to store this random name. You can name it anything you like.
- “%s%s%s%s” is used since we are working with alphabets here and in C %s is used to denote a string character.
- “p_RandomLetter” is the name of the parameter where we have all our alphabets stored (A,B,C,etc..). It is the parameter we created as a prerequisite.
- “lr_eval_string()” this function is used to evaluate the value of the parameter (p_RandomLetter) and convert it into a string. Now one thing you must keep in mind is that we cannot use parameter values directly as they are, even if the values are string itself. We need to convert them to string using lr_eval_sting() function
Have a nice
day. And please share and like this post if it was even slightest bit of
helpful to you. ☺
Comments
Post a Comment