Back to Blog
Back to Blog
Vugen: Old School Random Selections With srand
Posted on Aug, 2012 by Admin
At the beginning of your script where you declare variables, seed time() with the srand() function:
char buffer[80];
int TotalNum, RandNum, i;
srand(time(NULL));
Here is how you might use this. Let’s say you need to use a random selection from a web page. This might be a different check box or drop down selection and you want it to be random each time:
//First grab the total count of items to choose from using ORD=ALL
web_reg_save_param("pCapture", "LB=something",
"RB=something else", "Ord=All", LAST);
web_submit_data("BLINGBLING",
"Action=https://heyyaserver/blingbling.asp",
"Method=POST",
"RecContentType=text/html",
"Referer=https://heyyaserver/default.asp",
"Snapshot=t35.inf",
"Mode=HTML",
ITEMDATA,
"Name=txtAction", "Value=Submit", ENDITEM,
"Name=homeydontplaydat", s1, ENDITEM, EXTRARES,
"Url=../images/snoopdogg.jpg", ENDITEM,
LAST);
// First get the total count of what was captured
TotalNum = atoi(lr_eval_string("{pCapture_count}"));
// Now get a random number between 1 and the total count
RandNum = rand() % TotalNum + 1;
sprintf(buffer, "{pCapture_%d}", RandNum);
// Now save the new selection as a true parameter in Vugen that can
// be used in this instance.
lr_save_string(lr_eval_string(buffer), "pNewAssigned");
// What is the value of it? Send a message to the output log
lr_output_message("The assigned value is: %s",
lr_eval_string("{pNewAssigned}"));
In more recent versions of LoadRunner, there are easier ways to do this with newer functions. However, I think it is still important to know how to do it other ways. Can you think of other uses for this? If so, let me know in the comments section below.