The LoadRunner Blog

Tuesday, October 03, 2006

Using ORD=ALL and getting a Random Value from Array

TOOL : LoadRunner 8.1

This code is used for getting a bunch of values returned by the server and randomly selecting one of them to use in the next weburl step. Imagine searching for a number of items on ebay and then clicking one of the results... Paste the code in the Action () step.

//declare variables
int IterNum, ItemNumCount, RandNum;
char* temp;
char RandNumStr[20];
char namestr[20];

web_reg_save_param ("ItemNumArray", "LB=/catalog/product/index.cgi?catalog_number=", "RB=&", "ORD=All", LAST);
web_submit_data("Send Search Term and Get Results",
"Name=org_id", "Value=2003521", ENDITEM,
"Name=custpartgrp", "Value=WEB", ENDITEM,
"Name=cntry", "Value=us", ENDITEM,
"Name=Nu", "Value=RollupKey", ENDITEM,
"Name=spage", "Value=header", ENDITEM,
"Name=Np", "Value=2", ENDITEM,
"Name=Ntt", "Value={SearchParam}", ENDITEM,
"Name=Ntk", "Value=All", ENDITEM,

//Display array count
lr_output_message ("Number of Items in Array : %s",lr_eval_string("{ItemNumArray_count}"));
ItemNumCount = atoi(lr_eval_string("{ItemNumArray_count}"));

//Parse thru array and display all the results
for (IterNum=1; IterNum<=ItemNumCount; IterNum++) {
sprintf(namestr, "{ItemNumArray_%i}",IterNum);
lr_save_string(lr_eval_string(namestr), "TempItemNum");
lr_output_message("Saved Item Number: %s", lr_eval_string("{TempItemNum}"));

//Generate a Random Number
RandNum = 1 + rand() % ItemNumCount;

//SAve Random Number to String
itoa (RandNum,RandNumStr,20);
lr_output_message("Random Number Generated is :%s", lr_eval_string(RandNumStr));

//Saves RandNumStr into random_value
lr_save_string(RandNumStr, "random_value");

temp = lr_eval_string(lr_eval_string("{ItemNumArray_{random_value}}"));
lr_save_string(temp, "RandItemNum");

//Use RandNumStr variable in a weburl step
web_url("Click on Random Product Page",


return 0;


Post a Comment

<< Home