String Tokenizer in HPE LR using C
Hello,
In LR Vugen while developing SAP GUI scripts or to automate functionality one often encounters with challenges like extracting/filtering specific data.
For this we have a concept of Tokenizer where we develop a piece of code to fragment or break the long text into segments which is to be used in the script.
Examples shown here walks you through how to extract text/data from the output long text/message displayed on the "Status bar of SAP GUI screen".
Example I -
Output log
Action.c(171): The text of the status bar is "Registration 1100000007 Created Sucessfully"
Action.c(173): Registration 1100000007 Created Sucessfully
Action.c(175): The text of the status bar is "Registration 1100000007 Created Sucessfully"
Action.c(183): The value of parameter 1 in the status bar is "Registration 1100000007 Created Sucessfully"
Action.c(187): The value of parameter 2 in the status bar is ""
Action.c(189): The value of parameter 3 in the status bar is ""
Code - sapgui_status_bar_get_param("1","value1");
Example-I doesn't extract the registration no when a standard get is performed instead the entire string is displayed in value1 of parameter
============================================================================
Example - II
Unlike in Example-I it does not extract the registration no using the standard LR functionality
sapgui_status_bar_get_param("1","value2");
Status bar message - "Registration 1100015969 Created Sucessfully"
Output log
The value of parameter 1 in the status bar is "1100015969"
The only additional step one has to do is copy this value to another parameter so it can use for other functionalities in the script
Now , lets deal with how we can resolve extracting the desired string/value shown in Example - I
We explore a concept of Tokenizer or Tokenism to achieve this
LR which uses C language has strtok functionality which is quite helpful and its usage is demonstrated below :
////////////////////////////////////////////////////////////////////////////////////////////////////
extern char * strtok(char * string, const char * delimiters );
We then define all the variables necessary
char line[50] = ""; // char line[20];
int i; // incrementer
char reg_no[20];
char regno[10];
char val[4][30]; // output array of strings.
char separators[] = " "; //space is delimeter
char * token;
As a next step we fetch and copy the status bar message to the line array defined
sapgui_status_bar_get_param("1", "value1",LAST);
strncpy( line, lr_eval_string("{value1}"), 40 );
In the next step lets start with the tokens itself
token = (char *)strtok(line, separators); // Get the first token
i = 1;
if (!token)
{
lr_output_message ("No tokens found in string!");
return( -1 );
}
while (token != NULL )
{ // While valid tokens are returned
//lr_output_message ("%s", token ); one can un-comment if needed
strcpy( val[i], token ); // Get next token:
token = (char *)strtok(NULL, separators); // Get the next token
//token = strtok(NULL, separators); // Get the next token
i++; // increment
}
lr_output_message("Val #1 is: %s", val[1]); // Depending on which variable has the expected output
strncpy( reg_no, val[1], 10 ); //copy the value to reg_no
This also helps if you need to use the value in reg_no in the script for other functionality
Note - One can also use File methods to implement this.
char * filename1 = "C:\\Program Files\\HP\\LoadRunner\\scripts\\ZGATE\\registr_no.dat";
long file;
int d;
once the extraction is completed put this in your code
d= atoi(reg_no);
then ,write the contents to the file
if ((file = fopen(filename1, "w+" )) == NULL)
{
lr_output_message("Unable to create %s", filename1);
return -1;
}
fprintf(file,"registr_no\n%d\n",d);
fclose(file);
Thanks
Dilip Mamidela , SAP India BLR