LR二:post接口_form表单上传
上传文件的本质是浏览器读取本地文件的内容,以二进制数据方式传输到服务端,服务端新建一个文件,将获取到的数据复制到文件中
LR中上传操作可以通过web_submit_data函数实现,支持录制
要点:
web_add_header(“Content-type”,”multipart/form-data”)或者Enctype=“multipart/form-data”
一般情况需要加 web_add_header("Content-Type","multipart/form-data");
1 Action() 2 { 3 web_reg_save_param("fin_msg", 4 "LB=", 5 "RB=", 6 "Ord=1", 7 "Search=Body", 8 LAST); 9 10 web_add_header("Content-Type","multipart/form-data"); 11 12 lr_start_transaction("form-data"); 13 14 //上传文件---函数 web_submit_data 15 web_submit_data("Attachments", 16 17 "Action=http://localhost:8080/pinter/file/api/upload", 18 19 "Method=POST", 20 21 "EncType=multipart/form-data", 22 23 "TargetFrame=", 24 25 "RecContentType=text/html", 26 27 "Snapshot=t5.inf", 28 29 "Mode=HTML", 30 31 ITEMDATA, 32 33 "Name=file", 34 35 "Value=C:\\a.txt", 36 37 "File=yes", 38 ENDITEM, 39 40 LAST ); 41 42 lr_convert_string_encoding(lr_eval_string("{fin_msg}"), LR_ENC_UTF8, LR_ENC_SYSTEM_LOCALE, "Account"); 43 44 45 if(strcmp(lr_eval_string("{Account}"), "上传成功") == 0){ 46 lr_end_transaction("form-data", LR_PASS); 47 48 49 }else{ 50 lr_end_transaction("form-data", LR_FAIL); 51 52 53 } 54 55 56 return 0; 57 }
运行后日志如下:
1 Virtual User Script started at : 2019-03-16 17:24:13 2 Starting action vuser_init. 3 Web Turbo Replay of LoadRunner 11.0.0 for WINXP; build 8859 (Aug 18 2010 20:14:31) [MsgId: MMSG-27143] 4 Run Mode: HTML [MsgId: MMSG-26000] 5 Run-Time Settings file: "C:\test_huzhenyu\Script\biao_dan_upload\\default.cfg" [MsgId: MMSG-27141] 6 Ending action vuser_init. 7 Running Vuser... 8 Starting iteration 1. 9 Starting action Action. 10 Action.c(3): Registering web_reg_save_param was successful [MsgId: MMSG-26390] 11 Action.c(10): Warning -26593: The header being added may cause unpredictable results when applied to all ensuing URLs. It is added anyway [MsgId: MWAR-26593] 12 Action.c(10): web_add_header("Content-Type") highest severity level was "warning" [MsgId: MMSG-26391] 13 Action.c(12): Notify: Transaction "form-data" started. 14 Action.c(15): t=618ms: 114-byte response headers for "http://localhost:8080/pinter/file/api/upload" (RelFrameId=1, Internal ID=1) 15 Action.c(15): HTTP/1.1 200 \r\n 16 Action.c(15): Content-Type: text/plain;charset=UTF-8\r\n 17 Action.c(15): Content-Length: 12\r\n 18 Action.c(15): Date: Sat, 16 Mar 2019 09:24:14 GMT\r\n 19 Action.c(15): \r\n 20 Action.c(15): t=643ms: 12-byte response body for "http://localhost:8080/pinter/file/api/upload" (RelFrameId=1, Internal ID=1) 21 Action.c(15): 涓婁紶鎴愬姛 22 Action.c(15): Notify: Saving Parameter "fin_msg = 涓婁紶鎴愬姛". 23 Action.c(15): web_submit_data("Attachments") was successful, 12 body bytes, 114 header bytes [MsgId: MMSG-26386] 24 Action.c(42): Notify: Parameter Substitution: parameter "fin_msg" = "涓婁紶鎴愬姛" 25 Action.c(42): Notify: Saving Parameter "Account = 上传成功\x00". 26 Action.c(45): Notify: Parameter Substitution: parameter "Account" = "上传成功\x00" 27 Action.c(46): Notify: Transaction "form-data" ended with "Pass" status (Duration: 0.6063 Wasted Time: 0.3921). 28 Ending action Action. 29 Ending iteration 1. 30 Ending Vuser... 31 Starting action vuser_end. 32 Ending action vuser_end. 33 Vuser Terminated.