Turn http://blog.163.com/yings_9371/blog/static/66196922010711115545137/
(1) LoadRunner upload file
Web_submit_data ("Importstudent.do",
"Action=https://testserver/console/importstudent.do",
"Method=post",
"Enctype=multipart/form-data",
"Reccontenttype=text/html",
"Referer=https://testserver/console/displaystudentlist.do",
"Snapshot=t12.inf",
"Mode=http",
ItemData,
"Name=uploadfile", "Value=d:\\excel\\data161955.zip", "File=yes", Enditem,
last);
Note: script recording uses the Http/html protocol or the Click and script protocol, which is a script recorded with http/html. It is important to note that regardless of which protocol recording is used, the file path and file name to be uploaded are only English characters and cannot be Chinese characters. Otherwise, the binary stream of the file upload may be recorded in the LR script at the same time.
The problem also shows that LR's support for Chinese is far from enough.
The following is a more detailed example of the application.
Action ()
{
Defines the variable trans_time of type double, which is used to hold the execution time of the transaction.
Double trans_time;
Defines the variable status of type int, which is used to hold the returned value of the transaction execution.
int status;
Lr_start_transaction ("test");
Use the built-in function lr_get_transaction_duration to get the execution time of a transaction.
Trans_time=lr_get_transaction_duration ("test");
status= web_submit_data ("Importstudent.do",
"Action=https://testserver/console/importstudent.do",
"Method=post",
"Enctype=multipart/form-data",
"Reccontenttype=text/html",
"Referer=https://testserver/console/displaystudentlist.do",
"Snapshot=t12.inf",
"Mode=http",
ItemData,
"Name=uploadfile", "Value=e:\\datafile\\data01.zip", "File=yes", Enditem,
last);
Increases the manual transaction, and if the test transaction executes a return value of 0, the transaction executes successfully or the execution fails.
if (status = = 0)
Lr_end_transaction ("Test", Lr_pass);
Else
Lr_end_transaction ("Test", lr_fail);
Print transaction execution time.
if (trans_time)
Lr_output_message ("Transaction Execution Time:%f sec", trans_time);
Else
Lr_output_message ("Transaction Execution time unknown!");
return 0;
}
The following is an example of an application of the Web_submit_data function in the Help documentation for LR, which can be seen in this example.
Web_submit_data ("Attachments",
"Action=http://[email protected]/attachments?" yy=45434 ",
"Method=post",
"Enctype=multipart/form–data",
"Targetframe=",
"Reccontenttype=text/html",
"Referer=http:///[email protected]/compose?" yy=20435 ",
"Snapshot=t5.inf",
"Mode=html",
ItemData,
"Name=userfile0",
"Value=e:\\sense_sensibility\\elinor.txt",
"File=yes",
"Contenttype=text/html",//Override default "Text/plain" for. txt files
Enditem,
"Name=userfile1",
"Value=e:\\sense_sensibility\\marianne.jpg",
"File=yes",
Enditem,
last);
(2) LoadRunner download file
Action ()
{
/* Download the Help document */
Define an integer variable save the size of the obtained file
int Flen;
Save File handle
Long filedes;
Save file path and filename
Char file[256]= "n";
Define a random number
char * strnumber;
Returns the string argument after evaluating embedded parameters.
Strnumber=lr_eval_string ("{randomnum}");
Save the path to the file variable
strcat (file, "d:\\excel\\");
Stitching the obtained random number after the file variable
strcat (File,strnumber);
The file suffix name that will be downloaded. rar concatenation after file variable
strcat (file, ". rar");
That's it. A loadrunner has obtained a full file name
To set the maximum length of a parameter, note that the value must be greater than the size of the file
Web_set_max_html_param_len ("20000");
Use the correlation function to get the contents of the downloaded file, where the left and right boundaries are not defined, to get all the contents of the server response
Web_reg_save_param ("Fcontent",
"Lb=",
"Rb=",
"Search=body",
last);
/* Download Help file */
Lr_start_transaction ("Download Help file");
Send a request to download a Help file
Web_url ("Downloadservlet",
"Url=https://testserver3/servlet/downloadservlet?filename=help",
"Resource=1",
"Reccontenttype=application/x-msdownload",
"Referer=",
"Snapshot=t41.inf",
last);
Gets the length of the file in the response
Flen = Web_get_int_property (http_info_download_size);
if (Flen > 0)
{
Open a file in write mode
if (filedes = fopen (file, "wb") = = = NULL)
{
Lr_output_message ("Open file failed!");
return-1;
}
Write file contents
Fwrite (Lr_eval_string ("{fcontent}"), Flen, 1, filedes);
Close File
Fclose (Filedes);
}
Lr_end_transaction ("Download Help file", Lr_auto);
return 0;
}
LoadRunner Upload and download