Loadrunner测试json接口
1. loadrunner + json说明
使用lr测试json接口,向服务端发送json格式请求,接收处理返回响应数据。
主要用到函数:
1)web_custon_request
2)web_reg_save_param (此函数常用户处理动态参数,该方法在LoadRunner中被称为Correlation(关联))
2. web_custon_request 参数说明
3. 具体实例如下(包含发送json,获取响应数据,设置关联、设置检查点):
/* 定义事务: 1)用户注册 2)用户登录 3)用户签到 接口为:application/json 思路: 1)app注册,获到mobile 2)使用获取到的mobile登录,获取tokenid和userid。 参数化:手机号码、设备号 */
Action() { //定义接口url char *Interface_Url; lr_save_string("http://192.168.0.1:8080/action.a","Interface_Url"); //注册事务开始 lr_start_transaction("用户注册"); lr_rendezvous("register"); //检查用户是否注册成功 web_reg_find("Search=Body", "Text=createTime", "SaveCount=createTime", LAST); //web_set_max_html_param_len("10000000"); //获取用户注册后响应数据,并传给mobile web_reg_save_param("mobile", "LB=\"mobile\":\"", //设置左边界 "RB=\",", //设置右边界 "Ord=1", "Notfound=empty", "Search=Body", LAST); web_custom_request("register", "Url={Interface_Url}", //设置url "Method=POST", //设置方法,post或get "Mode=HTTP", "EncType=application/json", //编码类型 "RecContentType=application/json", "Body={\"c\":\"1001\",\"p\":{\"code\":\"9051\",\"mobile\":\"{phone}\",\"pwd\":\"123456\",\"model\":\"{model}\",}}", //请求主体,json格式,分号前需要加斜杠 LAST); lr_vuser_status_message("注册事务,虚拟用户数 #%s",lr_eval_string("{phone}")); lr_end_transaction("用户注册",LR_AUTO); //登录事务开始 lr_start_transaction("用户登录"); lr_rendezvous("login"); web_reg_find("Search=Body", "Text=lastLoginTime", "SaveCount=lastLoginTime", LAST); //获取用户登录后响应数据tokenId,并传给pdl_dynamic_tokenId web_reg_save_param("pdl_dynamic_tokenId", "LB=\"tokenId\":\"", "RB=\"", "Ord=1", "Notfound=empty", "Search=Body", LAST); //从用户登录响应数据中,获取pdl_user_id web_reg_save_param("pdl_user_id", "NotFound=warning", "LB=\"id\":", "RB=,\"inviteCode", "Ord=1", "Search=Body", LAST); web_custom_request("login", "Url={Interface_Url}", "Method=POST", "Mode=HTTP", "EncType=application/json", "RecContentType=application/json", "Body={\"c\":\"1002\",\"p\":{\"mobile\":\"{pdl_mobile}\",\"pwd\":\"123456\"}}", LAST); lr_end_transaction("用户登录",LR_AUTO); //用户签到 lr_start_transaction("用户签到"); lr_rendezvous("Sign_in"); web_reg_find("Search=Body", "Text=signDate", "SaveCount=signDate", LAST);
web_custom_request("pdl_Sign_in", "Url={Interface_Url}", "Method=POST", "Mode=HTTP", "EncType=application/json", "RecContentType=application/json", "Body={\"c\":\"1014\",\"p\":{\"userId\":\"{pdl_user_id}\",\"tokenId\":\"{pdl_dynamic_tokenId}\"}}", LAST); lr_end_transaction("用户签到",LR_AUTO); return 0; }