Programmatically Create A Unique Parameter Name

Lets say you need to grab a list of items or numbers from a web page, such as GUID’s. These are not dynamic numbers, but will be used in a script as parameters. Here is how I captured the data I wanted and created my own DAT file:

  1. Figure out the format you want the DAT file to be in. For me, this was a GUID, another GUID, and a user ID. I created a file and put the top line in with the headers GUID1, GUID2, LOGIN and saved it to my C: drive in the root folder. I already knew the login so I had already set this up a a parameter called pLogin. The other two items I had to capture on the fly. For each user the list was different. Each user had between two and 60 items in their list.
  2. Figure out what you need to capture using the web_reg_save_param. I assume you already know how to do this. You will use the ORD=ALL to capture all of the items in the list that match your capture criteria, so make sure you have your boundaries set up so it will catch them all.
  3. Create a loop that gets the value of each of the things gathered by the ORD=ALL and spit them out to a file.

here is the code:

----------------------------------
 
// Declarations
 
int i, ord;
int *reviewer_log;
char logdata[256];
char buid[64];
char asid[64];
char *outbufftemp;
char *outbufftemp2;
 
//Steps that captures the two GUID's
 
web_reg_save_param("pBUIDNumber",
"LB=leftstring{",
"RB=}rightbracketstring",
"Ord=All",
"NotFound=ERROR",
LAST);
 
web_reg_save_param("pAsidNum",
"LB=leftstring{",
"RB=}rightbracketstring",
"Ord=ALL",
"NotFound=ERROR",
LAST);
 
//Page that displays the information I want
 
web_submit_data("Login.asp",
"Action=http://servername/Login.asp",
"Method=POST",
"RecContentType=text/html",
"Referer=http://servername/",
"Snapshot=t2.inf",
"Mode=HTTP",
ITEMDATA,
"Name=Command", "Value=Login", ENDITEM,
"Name=email", "Value={pLogin}", ENDITEM,
"Name=password", "Value=password", ENDITEM,
"Name=TargetURL", "Value=Login.asp", ENDITEM,
LAST);
 
//Count how many things you grabbed and put a message in the log
 
ord=atoi(lr_eval_string("{pBUIDNumber_count}"));
lr_output_message("Number of thing captured was %d", ord);
 
// Set up a loop that will start at the first thing captured, spit out the value
// to the log, and continue counting until you reach the value of ord.
 
for (i=1; i<=ord; i++){
    sprintf(buid, "{pBUIDNumber_%d}", i);
    outbufftemp = lr_eval_string(buid);
    sprintf(asid, "{pAssessmentNum_%d}", i);
    outbufftemp2 = lr_eval_string(asid);
    reviewer_log = (int *) fopen("C:\\reviewer_log.txt", "a");
    sprintf(logdata,"%s, %s, %s", outbufftemp, outbufftemp2, lr_eval_string("{pLogin}"));
    fwrite(logdata, sizeof(logdata), 1, reviewer_log);
    fclose(reviewer_log);
}
 
//Run the script so that you iterated in seqential order the amount of times
// needed to go through all the logins.
 
//End Code
 
-----------------------------------------

It has been pointed out that the fwrite function won’t remove the trailing spaces and it may be easier to use the fprintf function. See this code:

//--------------------Code----------------------
 
char logdata[80];
long stream;
extern int memset (char *, int, int);
 
char * my_username = "lenny";
char * my_password = "squiggy";
 
if((stream = fopen("c:\\successful_namepairs.txt","a"))== NULL)
{
    lr_error_message ("Opening of file failed");
    return(-1);
}
else
{
    lr_log_message ("Login successful for %s", my_password);
    sprintf(logdata,"%s,%s",my_username,my_password);
    fprintf(stream,"%s,%s\n",my_username,my_password);
    fclose(stream);
}
return 0;
 
//---------------end code------------------------

Paste that into the action file and you should have a new file created in your C:\ folder.

The line should be “lenny, squiggy” without the trailing spaces.

What's Next?

Did you enjoy this article? Help spread the word by sharing:

Join the Northway Navigator Club today and get access to restricted content including our best tips and tricks. Membership is free! You will also receive free email updates by registering.

Engage in the conversation and leave a comment:

Scott Moore

About Scott Moore (153 articles)

With over 20 years of IT experience with various platforms and technologies, Scott has tested some of the largest applications and infrastructures in the world. He is a Certified Instructor and Certified Product Consultant in HP’s LoadRunner and Performance Center products. He currently holds HP certifications for ASE, ASC, and CI. A thought leader in the APM space, he speaks regularly at IT conferences and events