Advanced Techniques


We are going to use test script language (TSL) in load runner sometimes might require to write C or Java to make our business scenario work. By default, we will use C language.
Note: We have only one data type in LR that is “string”
Note: We don’t require to declare LR variable.
C Data Types:

  • Int,
  •  char,
  •  double,
  •  float,
  •  flag,
  •  long,
  •  short,
  •  Boolean etc


How to convert an integer value to LR variable?

Solution: “itoa”  //It converts integer to string
Lr_eval_string //It reads the value from a variable
Lr_output_message //It displays the message
Example:
Int x=0;
Char abc[100];
Itoa(x,abc,10); //Converting int to string
Lr_save_string(abc,”final”); //Assigning value to lr variable
Lr_output_message(“%s”, lr_eval_string(“{final}”));


How to compare two LR variables?

Solution:
lr_save_string(lr_paramarr_random("c_source"),"Ran_Source");
lr_save_string(lr_paramarr_random("c_desti"),"Ran_Dest");
if(strcmp(lr_eval_string("{Ran_Source}"),lr_eval_string("{Ran_Dest}"))==0)
{
lr_output_message("SOURCE & DESTINATIONARE SAME, CANT BOOK
TICKET");
lr_abort();
}
else
{
lr_output_message("SOURCE & DESTINATION ARE NOT SAME, CAN
BOOK TICKET");
}


Working for a banking application. Capture the account holder balance and add 10 rs to balance and substitute in the next request.

Solution:
y = atoi(lr_eval_string("{c_sid}"));
z=x+y;
lr_output_message("%d",z);
itoa(z,final, 10);
lr_output_message(final);
lr_save_string(final,"final_balance");

How to use string tokenizer functionality?
Or 
How to split a string into multiple strings?
Solution:
char* abc;
char xyz[100];
--------------------RESPONSE BODY--------------------
strcpy(xyz,lr_eval_string("{c_fno}"));
abc=(char*)strtok(xyz,";");
lr_output_message(abc);
lr_save_string(abc,"final");


How to split a string into multiple strings?
Solution:
char path[100];
char separators[] = ";";
char* token;
---------------------RESPONSE BODY---------------------
strcpy(path,lr_eval_string("{c_fno}"));
token = (char *)strtok(path, separators); // Get the first token
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 );
token = (char *)strtok(NULL, separators); // Get the next token
}

How to use atoi and itoa functions?

int cnt,x;
char* temp;
char abc[10];
--------------------------RESPONSE BODY-----------------------
cnt=atoi(lr_eval_string("{c_desti_count}"));
x=1+rand()%cnt;
lr_output_message("%d",x);
itoa(x,abc,10);
lr_save_string(abc,"F_dest");
temp=lr_eval_string(lr_eval_string("{c_desti_{F_dest}}"));
lr_save_string(temp,"final");


Working for a banking application. Capture the account holder balance and add 10.25 rs to balance and substitute in the next request.

float x=10.75,y,z;
char final[100];
------------------------RESPONSE BODY--------------------
y = atof(lr_eval_string("{c_sid}"));
lr_output_message("%.2f",y);
z=x+y;
lr_output_message("%.2f",z);
sprintf(final,"%.2f",z);
lr_output_message(final);
lr_save_string(final,"final_balance");


How to download a pdf/doc/xls from an application?



unsigned long paramlen;
char* newparam;
web_save_timestamp_param(“tstamp”,LAST);
file=fopen(lr_eval_string(“D:\\anand_{puser_id}_{p_iteration}_{tstamp}.pdf”),”wb”);
lr_eval_string_ext(“{vinay}”,strlen(“{vinay}”),&newparam,&paramlen,0,0,-1);
fwrite(newparam,paramlen,1,file);

fclose(file);

No comments:

Post a Comment