How to upload any local or network files with Load runner scripting:
There are several options that do the same work:
1. Running test locally :
Record script where you upload a local file. I suggest placing file in “C:\temp” before recording.
Run the test using same local machines. Test will pass since script and upload file both located in the same machine during recording and test execution.
Keep in mind to replace single backslash with double backslashes in the script. Here is the sample script:
web_submit_data("AddAttachment.aspx",
"Action=https://learningshared.com/AddAttachment.aspx",
"Method=POST",
"EncType=multipart/form-data",
"RecContentType=text/html",
"Snapshot=t7.inf",
"Mode=HTML",
ITEMDATA,
"Name=__VIEWSTATE", "Value={ViewState_Value_1}", ENDITEM,
"Name=__EVENTVALIDATION", "Value={EVENTVALIDATION_1}", ENDITEM,
"Name=FileUpload1", "Value=C:\\Temp\\my_upload_file.pdf", "File=yes", ENDITEM,
"Name=Button1", "Value=Upload", ENDITEM,
LAST);
2. Running test with any network load generator: This method is similar to the first method. Therefore, record the script the same way as above.
Create a network share location with read & write access. Place files those need to be uploaded.
Replace local file location with network file location. Make sure to replace single backslash with double backslashes in the script. Here is the sample script:
web_submit_data("AddAttachment.aspx",
"Action=https://learningshared.com/AddAttachment.aspx",
"Method=POST",
"EncType=multipart/form-data",
"RecContentType=text/html",
"Snapshot=t7.inf",
"Mode=HTML",
ITEMDATA,
"Name=__VIEWSTATE", "Value={ViewState_Value_1}", ENDITEM,
"Name=__EVENTVALIDATION", "Value={EVENTVALIDATION_1}", ENDITEM,
"Name=FileUpload1", "Value=\\\\shared_network_location\\my_upload_file.pdf", "File=yes", ENDITEM,
"Name=Button1", "Value=Upload", ENDITEM,
LAST);
3.Attach upload file with scripts:
This method is similar to the first method. Record the script with a local file.
Add all the files (to be uploaded) to scripts by clicking “Add Files to Scripts…” menu from “Files -> Add Files to Scripts…”
Now clear file location and just leave the file name. These scripts should run from any LG's in the network or can be run from Performance center ALM too.
web_submit_data("AddAttachment.aspx",
"Action=https://learningshared.com/AddAttachment.aspx",
"Method=POST",
"EncType=multipart/form-data",
"RecContentType=text/html",
"Snapshot=t7.inf",
"Mode=HTML",
ITEMDATA,
"Name=__VIEWSTATE", "Value={ViewState_Value_1}", ENDITEM,
"Name=__EVENTVALIDATION", "Value={EVENTVALIDATION_1}", ENDITEM,
"Name=FileUpload1", "Value=my_upload_file.pdf", "File=yes", ENDITEM,
"Name=Button1", "Value=Upload", ENDITEM,
LAST);
===========================================
Problem:
How to handle the File Download Scenario in LoadRunner?
OR
How to capture the File Download Time in LoadRunner?
Explanation:
VuGen does not capture client-side activity like browsing a location in a pop-up, select a file from the local system etc. Such limitations restrict a performance tester to simulate the file download scenario in LoadRunner.
Although he can easily record the file download step while script recording through VuGen. But when he replays the script then VuGen does not calculate the correct response time to download the file.
Solution:
It is true that you can not download the file in the actual format and save in the local machine during the script replay or test execution. Instead of that, LoadRunner allows to get the file in the raw data form. Thus you can capture 2 things:
Size of the File (The amount of data coming in the response): It signifies that the file contents are transferring from the server to the client.
Time to download the File: It helps to find out the time to virtually download a file.
Write the below code in your script to create the File Download Scenario:
//Firstly defined below variables at the top in Action.c
long Download_Size=0;
float Download_Time=0;
//Write below code after web_submit() - where you click to browse the location and download the file
lr_start_transaction("File_Download_Scenario");
Download_Size= web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
Download_Time= web_get_int_property(HTTP_INFO_DOWNLOAD_TIME);
lr_output_message("Downloaded File Size is: %.2ld Bytes", Download_Size);
lr_output_message("File Download size is: %.2ld KB", Download_Size /1024);
lr_output_message("File Download Time is:%.2f Seconds",Download_Time / 1000);
lr_end_transaction ("File_Download_Scenario", LR_AUTO);
Example:
Action()
{
long Download_Size=0;
float Download_Time=0;
........
........
........
........
web_submit(){
.......
.......
}
lr_start_transaction("File_Download_Scenario");
Download_Size= web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
Download_Time= web_get_int_property(HTTP_INFO_DOWNLOAD_TIME);
lr_output_message("Downloaded File Size is: %.2ld Bytes", Download_Size);
lr_output_message("File Download size is: %.2ld KB", Download_Size /1024);
lr_output_message("File Download Time is:%.2f Seconds",Download_Time / 1000);
lr_end_transaction ("File_Download_Scenario", LR_AUTO);
}
===================================
Write Data to an external file (in local machine)
Business Scenario: For each unique login, application was generating a Unique ID. If there is >1000 unique login, and there is required to save all Unique ID in any file format to local machine.
It will be helpful– if your requirement is to capture all the unique IDs in any file format (notepad.txt etc..)to your local machine (like- C:/users/desktop/file.txt).
long FileVarriable;
char FileLocation[1024] = “C:\\Users\\Desktop\\PT_Test.txt”;
Action()
{
FileVarriable = fopen (FileLocation,”w+”);
fprintf (FileVarriable, “%s \n”, lr_eval_string(“{PT_Param}”));
fclose (FileVarriable);
return 0;
}
Note: -
FileVarriable– It is a variable which open file for reading/writing etc..
FileLocation– This variable keeps the file location.
fprintf function- sends formatted output to a stream.
{PT_Param}– This is parameter which is keeping the different values at each Iterations.
fopen (FileLocation,”w+“)- Here w+ is the mode, which instruct the data to follow action accordingly.
“r” –Opens a file for reading. The file must exist.
“w” –Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file.
“a” –Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist.
“r+” –Opens a file to update both reading and writing. The file must exist.
“w+” –Creates an empty file for both reading and writing.
“a+” –Opens a file for reading and appending.
Qus :- How to read contents of the text file through loadrunner function
Some time we face to read/Write the file contents of notepad in loadrunner. There is already some inbuild functionalities are available in the Loadrunner-
char * filename =”C:\\Desktop\\DegreeDetails.txt”; // add // quote instead of single (/) for defining path.
long file;
char names[100];
Action()
{
fopen(filename, “r”);
if ((file = fopen(filename, “r”)) == NULL) {
lr_error_message (“Cannot open %s”, file);
return -1; }
while (!feof(file))
{
fgets(names, 100, file); // here 100 decide the length of the contents in a line. So if you have bigger length to read, then you need to increase the length.
if (!feof(file)) //if not at the end of file print names
{
lr_output_message(“%s”,lr_eval_string(names));
}
}
fclose(file);
return 0;
}
Ques :- How to insert bulk volume of data in to the application at one shot
Requirement:
There was a requirement, where we had to send a data in bulk amount (like->500 sets of data) by copy/paste mode to the application server.
Copy means– Need to copy from any excel sheet.
Paste means– All more than 500 data should have pasted in one shot. One data had 6 columns details (Like- name; address; emp id..etc).
After recording it is observed that for one string one Json request was being created, in the similar ways- for two request there was 2 Json request were created (Like- {Json1},{Json2}; format)
Approach:
Define a parameter (Like- xxxx) which save the value from parameter file, and keep changing accordingly the number of Iteration proceed.
Define one another parameter, which could work with strcat function. Strcat function will play a huge role in keeping and adding the new parameter value in terms of string. Strcat(string1, string2);. The value of string1 will concatenate the value of string2, and after that it will save again to string1.
For the above requirement-
We save first Json (this was a big request which had nearly 5 variable, which we plan to send it through parameter) request in string2 and with the help of strcat we added in to the string1.
Internally we ran one FOR loop, which was running till the all parameter value get read and get added to one string.
Ans:
char *Fstring=(char *) malloc(20480 * sizeof(char*));
char * fComa=(char*) malloc (20480 * sizeof(char*));
int i;
char * Json_BaseE, * Json_BaseT;
strcpy(Fstring,” “);
strcpy(fComa,”,”);
Json_BaseE=”{\”0\”:null,\”1\”:null,\”2\”:null,\”3\”:null,\”4\”:null,\”description\”:\”{Desc}\”,\”shipEndDate\”:\”{ShipEndDate}\”,\”shipStartDate\”:\”{ShipStartDate}\”,\”endDate\”:\”{EventEndDate}\”,\”startDate\”:\”{EventStartDate}\”,\”division\”:\”{Division}\”,\”type\”:\”{Type}\”,\”metadata\”:{\”isNewRow\”:true,\”originalValues\”:{},\”rowNumber\”:{RowNumber},\”cellStates\”:{\”metadata.rowNumber\”:true}}”;
for (i=0;i<26;i++) // This has defined to ready the parameter value from parameter file, it would be able to read 26 rows
{
Json_BaseT= lr_eval_string(Json_BaseE); // Eval function will enable “Json_BaseT” to save with the latest value of parameter
lr_save_string(Json_BaseT, “JB_1”); // It can be directly written as- lr_ssave_string(lr_eval_string(Json_BaseT),”JB_1″);
lr_next_row(“Desc.dat”); // This function help our function to read next available value from the parameter file. Dat is a extension of all parameter and Desc I have mentioned because I created a table name Disc, which had other dependent parameter also.
strcat(Fstring,Json_BaseT); // Both variable will concatenate, and the concatenated value get stored in 1st defined variable
if(i<25){
strcat(Fstring,fComa); // This fuction is able to strict the concatenation just before fComa value get added. If it will not here then final string will have Fstring (latest value) with fComa.
}
lr_output_message (“Full path of file is %s”, Fstring);
}
lr_save_string(Fstring,”NJson”); // Finally the NJson variable will have the latest value, and we can pass in to the body of the loadrunner request pattern.
===============
No comments:
Post a Comment