LR一:loadrunner_遇到cookie接口_3种应对方法
方法一:是调用登录接口,在调用登录后的接口
方法二:手动储存cookie,写死cookie
方法一:提前登录收集cookie,写成参数化文件
方法一,案例(就是先登录,再写登录后的接口);
注:userName也可以做成文件参数化的方式
1 Action() 2 { 3 4 //先登录login接口,由于loadrunner也是基于浏览器形式的,所以也会储存cookie 5 web_custom_request("post_cokies", "Method=POST", 6 7 "URL=http://localhost:8080/pinter/bank/api/login", 8 9 "Body=userName=admin&password=1234", 10 11 "TargetFrame=", 12 13 LAST ); 14 15 //这是登录后的接口,查询余额的 16 web_url("query", 17 18 "URL=http://localhost:8080/pinter/bank/api/query?userName=admin", 19 20 LAST ); 21 22 return 0; 23 }
运行日志如下:
1 Virtual User Script started at : 2019-03-10 14:06:02 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:\script\0224\cookie\\default.cfg" [MsgId: MMSG-27141] 6 Ending action vuser_init. 7 Running Vuser... 8 Starting iteration 1. 9 Starting action Action. 10 Action.c(5): t=1224ms: 252-byte response headers for "http://localhost:8080/pinter/bank/api/login" (RelFrameId=1, Internal ID=1) 11 Action.c(5): HTTP/1.1 200 \r\n 12 Action.c(5): Set-Cookie: testfan-id=76e4a2de-8dbd-4730-bbd7-728b39bd58d8; Max-Age=216000; Expires=Tue, 13 Action.c(5): 12-Mar-2019 18:06:03 GMT; Path=/\r\n 14 Action.c(5): Content-Type: application/json;charset=UTF-8\r\n 15 Action.c(5): Transfer-Encoding: chunked\r\n 16 Action.c(5): Date: Sun, 10 Mar 2019 06:06:03 GMT\r\n 17 Action.c(5): \r\n 18 Action.c(5): t=1398ms: 4-byte chunked response overhead for "http://localhost:8080/pinter/bank/api/login" (RelFrameId=1, Internal ID=1) 19 Action.c(5): 2c\r\n 20 Action.c(5): t=1402ms: 7-byte chunked response overhead for "http://localhost:8080/pinter/bank/api/login" (RelFrameId=1, Internal ID=1) 21 Action.c(5): \r\n 22 Action.c(5): 0\r\n 23 Action.c(5): \r\n 24 Action.c(5): t=1409ms: 44-byte chunked response body for "http://localhost:8080/pinter/bank/api/login" (RelFrameId=1, Internal ID=1) 25 Action.c(5): {"code":"0","message":"success","data":null} 26 Action.c(5): HTML parsing not performed for Content-Type "application/json" ("ParseHtmlContentType" Run-Time Setting is "TEXT"). URL="http://localhost:8080/pinter/bank/api/login" [MsgId: MMSG-26548] 27 Action.c(5): web_custom_request("post") was successful, 44 body bytes, 252 header bytes, 11 chunking overhead bytes [MsgId: MMSG-26385] 28 Action.c(18): t=1930ms: 128-byte response headers for "http://localhost:8080/pinter/bank/api/query?userName=beihe" (RelFrameId=1, Internal ID=2) 29 Action.c(18): HTTP/1.1 200 \r\n 30 Action.c(18): Content-Type: application/json;charset=UTF-8\r\n 31 Action.c(18): Transfer-Encoding: chunked\r\n 32 Action.c(18): Date: Sun, 10 Mar 2019 06:06:03 GMT\r\n 33 Action.c(18): \r\n 34 Action.c(18): t=2108ms: 4-byte chunked response overhead for "http://localhost:8080/pinter/bank/api/query?userName=beihe" (RelFrameId=1, Internal ID=2) 35 Action.c(18): 36\r\n 36 Action.c(18): t=2173ms: 7-byte chunked response overhead for "http://localhost:8080/pinter/bank/api/query?userName=beihe" (RelFrameId=1, Internal ID=2) 37 Action.c(18): \r\n 38 Action.c(18): 0\r\n 39 Action.c(18): \r\n 40 Action.c(18): t=2310ms: 54-byte chunked response body for "http://localhost:8080/pinter/bank/api/query?userName=beihe" (RelFrameId=1, Internal ID=2) 41 Action.c(18): {"code":"0","message":"success","data":"$ 10,163,749"} 42 Action.c(18): HTML parsing not performed for Content-Type "application/json" ("ParseHtmlContentType" Run-Time Setting is "TEXT"). URL="http://localhost:8080/pinter/bank/api/query?userName=beihe" [MsgId: MMSG-26548] 43 Action.c(18): web_url("query") was successful, 54 body bytes, 128 header bytes, 11 chunking overhead bytes [MsgId: MMSG-26385] 44 Ending action Action. 45 Ending iteration 1. 46 Ending Vuser... 47 Starting action vuser_end. 48 Ending action vuser_end. 49 Vuser Terminated.
方法二,案例(通过浏览器抓包,手动设置cookie的方法)
1.”火狐“ 浏览器清空以前的cookie
2.进行登录操作F12抓包
3.查看响应cookie
4.在loadrunner中添加 这个函数
web_add_cookie ("key=value; domain=ip不加端口");
web_add_cookie ("testfan-id=f9325d71-f9a1-46a4-bc35-1c760a94b464; domain=localhost");
注: cookie也可以做成文件参数化的形式
1 Action() 2 { 3 4 // //先登录login接口,由于loadrunner也是基于浏览器形式的,所以也会储存cookie 5 // web_custom_request("post_cokies", "Method=POST", 6 // 7 // "URL=http://localhost:8080/pinter/bank/api/login", 8 // 9 // "Body=userName=admin&password=1234", 10 // 11 // "TargetFrame=", 12 // 13 // LAST ); 14 // 15 16 //这是手动抓包抓取的cookie 信息,添加在前面就可以了。 17 web_add_cookie ("testfan-id=f9325d71-f9a1-46a4-bc35-1c760a94b464; domain=localhost"); 18 19 //这是登录后的接口,查询余额的 20 web_url("query", 21 22 "URL=http://localhost:8080/pinter/bank/api/query?userName=admin", 23 24 LAST ); 25 26 return 0; 27 }
运行日志如下:
log
Virtual User Script started at : 2019-03-16 16:44:25 Starting action vuser_init. Web Turbo Replay of LoadRunner 11.0.0 for WINXP; build 8859 (Aug 18 2010 20:14:31) [MsgId: MMSG-27143] Run Mode: HTML [MsgId: MMSG-26000] Run-Time Settings file: "C:\test_huzhenyu\Script\cokies2\\default.cfg" [MsgId: MMSG-27141] Ending action vuser_init. Running Vuser... Starting iteration 1. Starting action Action. Action.c(15): web_add_cookie was successful [MsgId: MMSG-26392] Action.c(18): t=861ms: 128-byte response headers for "http://localhost:8080/pinter/bank/api/query?userName=hu" (RelFrameId=1, Internal ID=1) Action.c(18): HTTP/1.1 200 \r\n Action.c(18): Content-Type: application/json;charset=UTF-8\r\n Action.c(18): Transfer-Encoding: chunked\r\n Action.c(18): Date: Sat, 16 Mar 2019 08:44:25 GMT\r\n Action.c(18): \r\n Action.c(18): t=897ms: 4-byte chunked response overhead for "http://localhost:8080/pinter/bank/api/query?userName=hu" (RelFrameId=1, Internal ID=1) Action.c(18): 36\r\n Action.c(18): t=903ms: 7-byte chunked response overhead for "http://localhost:8080/pinter/bank/api/query?userName=hu" (RelFrameId=1, Internal ID=1) Action.c(18): \r\n Action.c(18): 0\r\n Action.c(18): \r\n Action.c(18): t=918ms: 54-byte chunked response body for "http://localhost:8080/pinter/bank/api/query?userName=hu" (RelFrameId=1, Internal ID=1) Action.c(18): {"code":"0","message":"success","data":"$ 81,715,804"} Action.c(18): HTML parsing not performed for Content-Type "application/json" ("ParseHtmlContentType" Run-Time Setting is "TEXT"). URL="http://localhost:8080/pinter/bank/api/query?userName=hu" [MsgId: MMSG-26548] Action.c(18): web_url("query") was successful, 54 body bytes, 128 header bytes, 11 chunking overhead bytes [MsgId: MMSG-26385] Ending action Action. Ending iteration 1. Ending Vuser... Starting action vuser_end. Ending action vuser_end. Vuser Terminated.
方法三,案例(提前登录收集cookie,写成参数化文件)
1 Action() 2 { 3 4 // loadrunner和浏览器一样,会自动处理cookie 5 6 // web_custom_request("post", "Method=POST", 7 // 8 // "URL=http://localhost:8080/pinter/bank/api/login", 9 // 10 // "Body=userName={user}&password=1234", 11 // 12 // "TargetFrame=", 13 // 14 // LAST ); 15 //{cookie} 取的是文件参数化的值 16 web_add_cookie ("testfan-id={cookie}; domain=localhost"); 17 18 19 web_url("query", 20 //{uesr}取得也是文件中的参数化的值 21 22 "URL=http://localhost:8080/pinter/bank/api/query?userName={user}", 23 24 LAST ); 25 26 27 return 0; 28 }
文件参数化-->操作步骤 --> 如下
1 user,cookie
2 admin,cookie参数
3 dev,cookie参数值
4 test,cookie参数值
5 设置成 uesr,cookie 抓包收集的