Using strstr in LoadRunner

Brought to you by Mark Sibley.

The following is an example of how you can use the standard C function [strstr] within a LoadRunner script as a means of searching through long strings of text for the occurrence of any matching string. It has two examples that demonstrate how to use the output to do one of two things depending on whatever it is you want it to do.

A sample script can be downloaded here.
STRSTR.zip

Action()
{

// This creates an integer I decided to call [occurrence] and gives it a value of zero
int occurrence = 0;

// This creates a long string of text and then stores it in a parameter I’ve called {sLongString}
// In a real script you wouldn’t need this as you’d be searching through text that you’d captured.
lr_save_string(“peter piper picked a peck of pickled peppers”,”sLongString”);

//**************************************************
// Example 1:
// This is searching for the occurrence of [Poland] anywhere within the parameter {sLongString}
occurrence = strstr(lr_eval_string(“{sLongString}”),”Poland”);
if(occurrence != 0)
{
lr_output_message(“1) The string was found”);
}
else
{
lr_output_message(“1) The string was NOT found”);
}
//**************************************************

//**************************************************
// Example 2:
// This is searching for the occurrence of [peck] anywhere within the parameter {sLongString}
occurrence = strstr(lr_eval_string(“{sLongString}”),”peck”);
if(occurrence != 0)
{
lr_output_message(“2) The string was found”);
}
else
{
lr_output_message(“2) The string was NOT found”);
}
//**************************************************

return 0;
}

Author: Richard Bishop

http://about.me/richard_bishop