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",
"Action=https://alpha.test.com/psearch/searchProcessor.jsp",
"Method=GET",
"EncType=",
"RecContentType=text/html",
"Referer=https://alpha.test.com/index.cgi?osid=RSEupwwUN8gAAEQuCts",
"Snapshot=t7.inf",
"Mode=HTTP",
ITEMDATA,
"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,
LAST);

//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",

"URL=https://alpha.test.com/index.cgi?catalog_number={RandItemNum}&inE=1",
"Resource=0",
"RecContentType=text/html",
"Referer=https://alpha.test.com",
"Snapshot=t11.inf",
"Mode=HTTP",
LAST);

return 0;

1 Comments:

Post a Comment

<< Home