Download and randomly generate file names with LR recording files

Source: Internet
Author: User

Download and randomly generate file names with LR recording files


{H.iy:x.tn0

8h&~-d|6fv0 Action ()51Testing Software test network Ge$nv}s,r
{51Testing Software test network}9sus ' Q
int Flen; Define an integer variable save the size of the obtained file 51Testing software test network vf0u.jkm

a[email protected]%xt?    U0 long filedes; Save File handle
"Fy*e gvr$~p$}0 51Testing Software test network O0EB6B;J~+[!X7SW
Char file[256]= "/0"; Save file path and filename 51Testing software test network. E1BA.YHB

51Testing software test network 5J; [Email protected]\m:a VNF

int rnum; Define a random number
#\ ({a.j8a\x0
x@:v@x/w rq0 Char s[8]; The number is converted from int to Char
g[xb i%e6vwc0 51Testing software Test network n L5^,~UCV (r[5d
Srand (Time (NULL)); Initialization

L*w ' yejyQ0

] "O ' f%{zkih! E#zh "x0 rnum= rand ()% 100; Get 0~100 random number 51Testing software test network $j6ofas{! R "RyR

$m 2au f*lr0 sprintf (S, "%d", rnum)//int convert to char51Testing software Test network 9q| M l%x y ufe4u! S

"C$g2zvm0 strcat (file," C://test ");//Assign C:/test path to file
'%ff ' 5gb2z0
j-vc_w5\0 strcat (file,s);//Add random number 51Testing software test network SFM+UM5MW ktu!d*r after file

zkpz$u) s:q) N0 web_set_max_html_param_len ("2000000");//Set the maximum number of bytes that the page receives, which should be larger than the size of the download file

-R} q@~g0

0rmk.js$me! O uOQ0//Use the correlation function to get the contents of the downloaded file, where the left and right boundaries are not defined, and all the contents of the server response are obtained 51Testing software test network w^h:@1cd!k; A{8i9a-o
Web_reg_save_param ("Fcontent",
9ugsi5a) I ' M0 "lb=",51Testing Software test network #f&o\ge&d.b*d:c s:p
"Rb=",
#en ^ R "P0 "Search=body",
Mf @ rozg!w#m ' v\0 last);

X ' q.o4tb0

X6v8hl9w.t#i2s7?0
jl%?9~s st2m0 web_url ("Download",
) ' 6|:P | Qqb-l0 "Url=http://www.51testing.com/ddimg/uploadsoft/20100107/testingguifan.pdf",//download page URL51Testing Soft Piece test mesh w$lu8f R b/^_
last); 51Testing Software test network 4t7i1yr,m5~

oe.v! B,v^:m0
w0p@/lrad9vs5 ' 0 flen = web_get_int_property (http_info_download_size); Get file size 51Testing software test network a9yp%e7x k

51Testing Software Test Network "J U4_/I8DP o#j

if (Flen > 0)51Testing Software test network #s+oc?l-f
{
fuk7ddky:m0 if ((filedes = fopen (file, "WB")) = = NULL)51Testing Software test network 2N]~0s ' 7]5l
{
zb@ kmq8pm0 lr_output_message ("Open File failed!");

9[g*|w5{) ~e.y)]:r051Testing Software test network vl\!^$ ' 9w;^

return-1;
z la-g (e_il:q0 }
^ x.l:| "bx7f0 fwrite (lr_eval_string (" {fcontent} "), flen,1,filedes); 51Testing Software test network R#[m-zih

51Testing Software Test network [: K "P" l$a0~a!y "ZF

Fclose (Filedes); 51Testing Software Test network ' V ' E,DMA
}

0a| F.qwhl7o0

p6x%\\9 ' 0 return 0;
/b{a8ry@0}

LoadRunner download File script when looking at Shi about pezybase test report, found that it is useful to JMeter (HTTP protocol) concurrent test download file, considering that the latter may need to be in the company pezybase concurrent Download,   Re-run and organize the LoadRunner download file scripts you used previously. One, HTTP protocol LoadRunner using the HTTP protocol is unable to record to the download process, will only send a download request to the server, in fact, the server has returned to the client, but LoadRunner is not recorded to save the file to the local process, So it is necessary for us to manually save the content received locally.  (The complete process of uploading files by HTTP protocol can be recorded directly, and has been used in UC). Note: The following are the LR functions used in the script below, and some basic C language file manipulation methods. In the script can also add some judgment to the success of the transaction and the file size is correct, such as the judgment; Web_reg_save_param: Associate function, put in front of the HTTP request, save the content returned by the request, Web_url:http request function, send the request to the specified URL , the download file is the direct download of the link to send the request; Web_get_int_property: Gets the length of the file returned by the downloading request; Web_set_max_html_param_len: Set WEB_SET_MAX_HTML_PARAM_ The maximum length of the parameter in the Len method is greater than the size of the file that needs to be downloaded; Action () {int flen;//defines an integer variable to save the size of the obtained file long filecontent;//Save the file handle, that is, the contents of the file Char filename[ ]= "";//Save the file path and file name char * strnumber;strnumber=lr_eval_string ("Test{newparam}");//Get a random number and convert it to a string, Newparam set the parameter to a random type, Here the parameterization is set strcat (FileName, "c:/test/"), and/or the path is saved to the file variable strcat (filename,strnumber); Stitching file name strcat (filename, ". pdf");//concatenation suffix name, as required, complete the complete path and file name Web_set_max_html_param_len ("20000");//Set the maximum length of the parameter, Note that the value must be greater than the size of the file//useThe correlation function gets the contents of the downloaded file, where the left and right boundaries are not defined, and all the contents of the server response are obtained web_reg_save_param ("fcontent",///contents are all stored in fcontent this parameter "lb=", "rb=", "   Search=body ", last); Lr_start_transaction (" Download file "); #设置的事务开始点 #web_url method You can write directly, you can also start recording, enter the download link URL for simple recording, mainly note the URL and resource these two parameters can be Web_url ("file.php", #函数名, no practical role " url=http://forum.ubuntu.org.cn/download/file.php?id=129973&sid=78fc8d76767ef49b606595824ceb963d ", #下载链接, That is, the link entered into the browser will prompt the download, get a lot of methods; "Resource=1", #1表示是下载资源, 0 means the page resource "Reccontenttype=application/octetstream", "referer=", " Snapshot=t1.inf ", last); Flen = Web_get_int_property (http_info_download_size);//Gets the length of the file in the response if (Flen > 0) {//To Write Open File Filecontent = fopen (filename, "WB"), if (filecontent = = NULL) #看是否正确打开了需要保存下载内容的文件, filename is the file path of the prepared organization above; {LR_ Output_message ("Open file failed!"); return-1;} Fwrite (Lr_eval_string ("{fcontent}"), Flen, 1, filecontent);//write file Contents fclose (filecontent);//Close File}lr_end_transaction (  "Download File", Lr_auto); #事务结束点return 0;}








 Second, the Java User protocol to use the Java User protocol is simpler, is to use Java to write a section from the specified link to download files from the script; Import Java.io.bufferedinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.net.malformedurlexception;import Java.net.URL; Import Java.net.urlconnection;import Lrapi.lr;public class Actions {public int init () throws Throwable{return 0;} End of Initpublic int action () throws Throwable{int downloadsize = 0; String Path = "c:\\temp\\"; Set download File save path String Vuid = string.valueof (lr.get_vuser_id ()); Gets the current virtual user ID and converts it to a string downloadsize = Urltools.gethttpfilebyurl ("http://forum.ubuntu.org.cn/download/file.php?id= 129973&sid=78fc8d76767ef49b606595824ceb963d ", Vuid,path); Call Urltools.gethttpfilebyurl () return 0;} End of Actionpublic int end () throws Throwable{return 0;} End of end}class urltools{public static int gethttpfilebyurl (String address,string userid,string path) {// Define the URL URL of the variable you need to use below; URLConnection conn = Null;int buff_size = 1024;byte[] buf = new Byte[buff_size];int downloadsize = 0; BufferedinputsTream bis; FileOutputStream fos = null;int size = 0;try{url = new URL (address),//address for passing in a link to be downloaded conn = url.openconnection (); Instantiate the link to be downloaded with a URL bis = new Bufferedinputstream (Conn.getinputstream ()); Save the contents of the file to be downloaded in the BIS input stream, Fos = new FileOutputStream (path+ "\" +userid+ "test000001" + ". pdf"));//Make a full path and instantiate it to the output stream, Here can be parameterized, such as parameterized file name,//path need to be manually created in advance, of course you can also create in the script to implement a different path System.out.println ("File size to download:" + conn.getcontentlength ()/ 1024x768 + "K"), while ((size = Bis.read (BUF))! =-1) #按照设置的buf大小写文件并记录下载的大小 {fos.write (buf,0,size);D ownloadsize = Downloadsiz E+size;} Bis.close (); Fos.close (); System.out.println ("user" + userid + "Download" + URL + "Done!");} catch (Malformedurlexception e) {System.out.println ("Download exception:"); E.printstacktrace ();} catch (IOException e) {System.out.println ("Download exception:"); E.printstacktrace (); }return downloadsize/1024;}}

Download and randomly generate file names with LR recording files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.