Monday, November 19, 2012

Loadrunner Check for Substring in Response

The following describes one method for checking a response for a specific substring.  The method is:

  • Use web_reg_find to search the response to the next request for the specified substring, saving the count in a parameter
  • Make the request
  • Check the count parameter for the number of occurrences of the substring

The following script illustrates the method, searching for the substrings "Google" and "Microsoft" in an http request to http://www.google.com, printing out the results.

Script


Action()
{
char *response;

// save response
        web_reg_save_param("responseParam", 
       "LB=", 
       "RB=", 
       LAST ); 

        // check response for substrings "Google" and "Microsoft"
web_reg_find("Text=Microsoft", "SaveCount=microsoft_count", LAST );
web_reg_find("Text=Google", "SaveCount=google_count", LAST );

// send request
web_url("test", 
"URL=http://www.google.com", 
"Resource=1", 
"RecContentType=text/plain", 
"Referer=http://www.google.com/", 
"Snapshot=t12.inf", 
LAST);

// Save respone to string
//response = lr_eval_string(lr_eval_string("{responseParam}"));
//lr_message("response = %s", response);

// check whether substring "Google" was found
if (strcmp(lr_eval_string("{google_count}"), "0") > 0) {
lr_output_message("Found %s occurrences of substring \"Google\" in response", lr_eval_string("{google_count}"));
} else {
lr_output_message("Did not find substring \"Google\" in response");
}

// check whether substring "Microsoft" was found
if (strcmp(lr_eval_string("{microsoft_count}"), "0") > 0) {
lr_output_message("Found %s occurrences of \"Microsoft\" in response", lr_eval_string("{search_count}"));
} else {
lr_output_message("Did not find substring \"Microsoft\" in response");
}

return 0;
}


Console Output


Starting iteration 1.
Starting action Action.
Action.c(7): Registering web_reg_save_param was successful   [MsgId: MMSG-26390]
Action.c(13): Registering web_reg_find was successful   [MsgId: MMSG-26390]
Action.c(14): Registering web_reg_find was successful   [MsgId: MMSG-26390]
Action.c(17): Registered web_reg_find successful for "Text=Microsoft"   [MsgId: MMSG-26362]
Action.c(17): Registered web_reg_find successful for "Text=Google" (count=7)   [MsgId: MMSG-26364]
Action.c(17): web_url("test") was successful, 5034 body bytes, 736 header bytes   [MsgId: MMSG-26386]
Action.c(31): Found 7 occurrences of substring "Google" in response
Action.c(40): Did not find substring "Microsoft" in response
Ending action Action.
Ending iteration 1.

No comments:

Post a Comment