loadrunner_遇到cookie接口_3种应对方法

时间:2022-10-25 19:32:14

方法一:是调用登录接口,在调用登录后的接口

方法二:手动储存cookie,写死cookie

方法一:提前登录收集cookie,写成参数化文件

方法一,案例(就是先登录,再写登录后的接口);

注:userName也可以做成文件参数化的方式

 Action()
{ //先登录login接口,由于loadrunner也是基于浏览器形式的,所以也会储存cookie
web_custom_request("post_cokies", "Method=POST", "URL=http://localhost:8080/pinter/bank/api/login", "Body=userName=admin&password=1234", "TargetFrame=", LAST ); //这是登录后的接口,查询余额的
web_url("query", "URL=http://localhost:8080/pinter/bank/api/query?userName=admin", LAST ); return ;
}

运行日志如下:

 Virtual User Script started at : -- ::
Starting action vuser_init.
Web Turbo Replay of LoadRunner 11.0. for WINXP; build (Aug ::) [MsgId: MMSG-]
Run Mode: HTML [MsgId: MMSG-]
Run-Time Settings file: "C:\script\0224\cookie\\default.cfg" [MsgId: MMSG-]
Ending action vuser_init.
Running Vuser...
Starting iteration .
Starting action Action.
Action.c(): t=1224ms: -byte response headers for "http://localhost:8080/pinter/bank/api/login" (RelFrameId=, Internal ID=)
Action.c(): HTTP/1.1 \r\n
Action.c(): Set-Cookie: testfan-id=76e4a2de-8dbd--bbd7-728b39bd58d8; Max-Age=; Expires=Tue,
Action.c(): -Mar- :: GMT; Path=/\r\n
Action.c(): Content-Type: application/json;charset=UTF-\r\n
Action.c(): Transfer-Encoding: chunked\r\n
Action.c(): Date: Sun, Mar :: GMT\r\n
Action.c(): \r\n
Action.c(): t=1398ms: -byte chunked response overhead for "http://localhost:8080/pinter/bank/api/login" (RelFrameId=, Internal ID=)
Action.c(): 2c\r\n
Action.c(): t=1402ms: -byte chunked response overhead for "http://localhost:8080/pinter/bank/api/login" (RelFrameId=, Internal ID=)
Action.c(): \r\n
Action.c(): \r\n
Action.c(): \r\n
Action.c(): t=1409ms: -byte chunked response body for "http://localhost:8080/pinter/bank/api/login" (RelFrameId=, Internal ID=)
Action.c(): {"code":"","message":"success","data":null}
Action.c(): 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-]
Action.c(): web_custom_request("post") was successful, body bytes, header bytes, chunking overhead bytes [MsgId: MMSG-]
Action.c(): t=1930ms: -byte response headers for "http://localhost:8080/pinter/bank/api/query?userName=beihe" (RelFrameId=, Internal ID=)
Action.c(): HTTP/1.1 \r\n
Action.c(): Content-Type: application/json;charset=UTF-\r\n
Action.c(): Transfer-Encoding: chunked\r\n
Action.c(): Date: Sun, Mar :: GMT\r\n
Action.c(): \r\n
Action.c(): t=2108ms: -byte chunked response overhead for "http://localhost:8080/pinter/bank/api/query?userName=beihe" (RelFrameId=, Internal ID=)
Action.c(): \r\n
Action.c(): t=2173ms: -byte chunked response overhead for "http://localhost:8080/pinter/bank/api/query?userName=beihe" (RelFrameId=, Internal ID=)
Action.c(): \r\n
Action.c(): \r\n
Action.c(): \r\n
Action.c(): t=2310ms: -byte chunked response body for "http://localhost:8080/pinter/bank/api/query?userName=beihe" (RelFrameId=, Internal ID=)
Action.c(): {"code":"","message":"success","data":"$ 10,163,749"}
Action.c(): 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-]
Action.c(): web_url("query") was successful, body bytes, header bytes, chunking overhead bytes [MsgId: MMSG-]
Ending action Action.
Ending iteration .
Ending Vuser...
Starting action vuser_end.
Ending action vuser_end.
Vuser Terminated.

log

方法二,案例(通过浏览器抓包,手动设置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也可以做成文件参数化的形式
 Action()
{ // //先登录login接口,由于loadrunner也是基于浏览器形式的,所以也会储存cookie
// web_custom_request("post_cokies", "Method=POST",
//
// "URL=http://localhost:8080/pinter/bank/api/login",
//
// "Body=userName=admin&password=1234",
//
// "TargetFrame=",
//
// LAST );
// //这是手动抓包抓取的cookie 信息,添加在前面就可以了。
web_add_cookie ("testfan-id=f9325d71-f9a1-46a4-bc35-1c760a94b464; domain=localhost"); //这是登录后的接口,查询余额的
web_url("query", "URL=http://localhost:8080/pinter/bank/api/query?userName=admin", LAST ); return ;
}

运行日志如下:

log
Virtual User Script started at : -- ::
Starting action vuser_init.
Web Turbo Replay of LoadRunner 11.0. for WINXP; build (Aug ::) [MsgId: MMSG-]
Run Mode: HTML [MsgId: MMSG-]
Run-Time Settings file: "C:\test_huzhenyu\Script\cokies2\\default.cfg" [MsgId: MMSG-]
Ending action vuser_init.
Running Vuser...
Starting iteration .
Starting action Action.
Action.c(): web_add_cookie was successful [MsgId: MMSG-]
Action.c(): t=861ms: -byte response headers for "http://localhost:8080/pinter/bank/api/query?userName=hu" (RelFrameId=, Internal ID=)
Action.c(): HTTP/1.1 \r\n
Action.c(): Content-Type: application/json;charset=UTF-\r\n
Action.c(): Transfer-Encoding: chunked\r\n
Action.c(): Date: Sat, Mar :: GMT\r\n
Action.c(): \r\n
Action.c(): t=897ms: -byte chunked response overhead for "http://localhost:8080/pinter/bank/api/query?userName=hu" (RelFrameId=, Internal ID=)
Action.c(): \r\n
Action.c(): t=903ms: -byte chunked response overhead for "http://localhost:8080/pinter/bank/api/query?userName=hu" (RelFrameId=, Internal ID=)
Action.c(): \r\n
Action.c(): \r\n
Action.c(): \r\n
Action.c(): t=918ms: -byte chunked response body for "http://localhost:8080/pinter/bank/api/query?userName=hu" (RelFrameId=, Internal ID=)
Action.c(): {"code":"","message":"success","data":"$ 81,715,804"}
Action.c(): 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-]
Action.c(): web_url("query") was successful, body bytes, header bytes, chunking overhead bytes [MsgId: MMSG-]
Ending action Action.
Ending iteration .
Ending Vuser...
Starting action vuser_end.
Ending action vuser_end.
Vuser Terminated.

方法三,案例(提前登录收集cookie,写成参数化文件)

 Action()
{ // loadrunner和浏览器一样,会自动处理cookie // web_custom_request("post", "Method=POST",
//
// "URL=http://localhost:8080/pinter/bank/api/login",
//
// "Body=userName={user}&password=1234",
//
// "TargetFrame=",
//
// LAST );
//{cookie} 取的是文件参数化的值
web_add_cookie ("testfan-id={cookie}; domain=localhost"); web_url("query",
//{uesr}取得也是文件中的参数化的值 "URL=http://localhost:8080/pinter/bank/api/query?userName={user}", LAST ); return ;
} 文件参数化-->操作步骤 --> 如下 user,cookie admin,cookie参数 dev,cookie参数值 test,cookie参数值 设置成 uesr,cookie 抓包收集的