Sample LoadRunner scripts, demonstrating LOOPs

I have found numerous occasions where I need to use a LOOP within a LoadRunner script to repeatedly perform an action. There are several ways that LOOPs can be used to repeat actions for either a specified number of times or until a particular occurrence is observed.

 

This script includes an example of FOR, WHILE and DO LOOPs.

Click here to download the script.
LoopExample.zip

 

 

Convert a number to text

Another Mark Sibley function.

This converts a number into text. It relies on the number already being saved as a character string.

It’s currently set to create a title case word but could be amended to create an all upper or lower case word easily enough.

The words it creates are going to be mostly nonsense, however because it could have occasionally created a real word I removed all of the vowels to reduce the likelihood of creating any offensive words.

Copy and paste the following into a LoadRunner script to see it in action.

int ConvertNumberToText(char * sInput)
{
int i;
int c;
char n;
char sText[50];
char sCharacter[10];
char * sUpper[10];
char * sLower[10];

sUpper[0] = “B”; sUpper[1] = “C”; sUpper[2] = “D”; sUpper[3] = “F”; sUpper[4] = “G”;
sUpper[5] = “H”; sUpper[6] = “J”; sUpper[7] = “K”; sUpper[8] = “L”; sUpper[9] = “M”;

sLower[0] = “b”; sLower[1] = “c”; sLower[2] = “d”; sLower[3] = “f”; sLower[4] = “g”;
sLower[5] = “h”; sLower[6] = “j”; sLower[7] = “k”; sLower[8] = “l”; sLower[9] = “m”;

for (i = 0; i < strlen(sInput); i++) {

sscanf(sInput+i,”%c”,&n);
sprintf(sCharacter,”%c”,n);
c = atoi(sCharacter);

// This ensures that the first character is an upper case letter and that the rest are lower case.
// If you want a word that is either all upper or lower case then amend the appropriate [sUpper] or [sLower]
if (i == 0) {strcpy(sText,sUpper[c]);}
else  {strcat(sText,sLower[c]);}
}

lr_save_string(sText,”sName”);
lr_output_message(lr_eval_string(“Output:[{sName}]”));
}

Action()
{
int p = 459332;
char string[50];

itoa(p,string,10);

lr_save_string(“50328″,”sNumber”);

ConvertNumberToText(string);
ConvertNumberToText(“2168”);
ConvertNumberToText(lr_eval_string(“{sNumber}”));

return 0;
}

 

Reverse a text string in LoadRunner

Another Mark Sibley C/LoadRunner function.

This function takes a string of text and reverses it, turning [Peter Piper] into [repiP reteP]. I know it will have limited use but should you need it then here it is.

Copy and paste the following into a LoadRunner script to see it work:

//*******************************************************
int Reverse (char* Param, char* PName)
{
char n, Character[2000], Varname[2000];
char FullString[2000];
int i = strlen(Param);

strcpy(FullString,””);
strcpy(&n,””);

while (i >= 0)
{
sscanf(Param+i, “%c”, &n);
sprintf (Character,”%c”,n);

strcat(FullString, Character);

i–;
}

lr_save_string(FullString,PName);
}
//*******************************************************

Action()
{
lr_save_string(“ABC123456″,”Param”);
Reverse(lr_eval_string(“{Param}”),”NewParam”);
lr_output_message(lr_eval_string(“Changed [{Param}] to [{NewParam}]”));

lr_save_string(“Peter Piper”,”Param”);
Reverse(lr_eval_string(“{Param}”),”NewParam”);
lr_output_message(lr_eval_string(“Changed [{Param}] to [{NewParam}]”));

lr_save_string(“Fiddlesticks”,”Param”);
Reverse(lr_eval_string(“{Param}”),”NewParam”);
lr_output_message(lr_eval_string(“Changed [{Param}] to [{NewParam}]”));

return 0;
}