loadrunner-脚本设计
Posted yhms
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了loadrunner-脚本设计相关的知识,希望对你有一定的参考价值。
Action()
{
// web_set_max_html_param_len("262144");
// web_reg_save_param("ResponseBody", "LB=", "RB=", "Search=Body", LAST); //关联出所有的响应内容
// web_save_header(REQUEST, "RequestHeader");
// web_save_header(RESPONSE, "ResponseHeader");
int y;
web_reg_save_param("sessionid",
"LB=sessionId":"",
"RB="",
LAST);
web_reg_save_param("userid",
"LB=userId":"",
"RB=",",
LAST);
lr_start_transaction("登录");
// 登录
web_custom_request("login",
"Url=http://10.9.17.250:8080/danglaoshi-web-app/user/login", //测试服地址
// "Url=https://dls-api-v3.danglaoshi.info/danglaoshi-web-app/user/login", //正式服地址
"Method=POST",
"RecContentType=application/json", //指定响应头的Content-Type
"EncType=application/json", //指定请求头的Content-Type
"Mode=HTTP",
"Body={"versionName":"3.3.0","password":"e10adc3949ba59abbe56e057f20f883e","userName":"{user}","versionCode":71,"deviceType":1}", //请求体,里面的双引号需要转义
LAST);
lr_end_transaction("登录", LR_AUTO);
lr_output_message(lr_eval_string("{user}"));//格式:lr_eval_string("{参数名}");从参数中取值并转换为字符串
lr_output_message(lr_eval_string("{userid}")); //lr_output_message()输出内容,打印内容
lr_output_message(lr_eval_string("{sessionid}"));
web_add_auto_header("Content-Type",
"application/json;charset=utf-8");
web_add_auto_header("userId",
"{userid}");
web_add_auto_header("sessionId",
"{sessionid}");
//如果有多个接口请求,使用web_add_header时就需要在每个请求前面加;但是使用web_add_auto_header时,就只需要添加一次就行,不需要每个接口前面去加
// web_add_header("Content-Type",
// "application/json;charset=utf-8");
//
// web_add_header("userId",
// "{userid}");
//
// web_add_header("sessionId",
// "{sessionid}");
// web_set_max_html_param_len("262144");
// web_reg_save_param("ResponseBody", "LB=", "RB=", "Search=Body", LAST); //关联出所有的响应内容
// web_save_header(REQUEST, "RequestHeader");
// web_save_header(RESPONSE, "ResponseHeader");
web_reg_save_param("courseid",
"LB="courseId":",
"RB=,",
"Ord=All", //不写Ord这一行代码,默认Ord=1,取出关联的第一个值;Ord=2,取出关联到的第2个值;Ord=All,取出所有的值存在一个数组中
LAST);
lr_start_transaction("课程列表");
//打开我的课程列表
web_url("userCourselist",
"URL=http://{ip}/api/celery/userCourse/list",
"TargetFrame=",
"Resource=0",
"Referer=",
LAST);
lr_end_transaction("课程列表", LR_AUTO);
y=atoi(lr_eval_string("{courseid_count}")); //统计关联到的所有的值的个数
lr_output_message("符合条件的课程id有%d条",y); //输出关联到的所有值的个数
// for (i=1,) {
// } 关联值循环保存输出
//
lr_save_string(lr_paramarr_random("courseid"), "couid"); //当"Ord=All"时,从数组中随机取出一个值给到couid
lr_output_message(lr_eval_string("{couid}")); //输出随机取出的值
lr_output_message(lr_eval_string("{courseid_3}")); //当"Ord=All"时,通过courseid_3取出数组中的第3个值
//随机进入我的课程中的任意一个课程详情
// web_set_max_html_param_len("262144");
// web_reg_save_param("ResponseBody", "LB=", "RB=", "Search=Body", LAST); //关联出所有的响应内容
// web_save_header(REQUEST, "RequestHeader");
// web_save_header(RESPONSE, "ResponseHeader");
// web_add_header("Content-Type",
// "application/json;charset=utf-8");
//
// web_add_header("userId",
// "{userid}");
//
// web_add_header("sessionId",
// "{sessionid}");
web_reg_save_param("scheduleid",
"LB="courseScheduleId":",
"RB=,",
LAST);
lr_start_transaction("课程详情");
web_url("CourseDetail",
"URL=http://{ip}/api/celery/userCourse/detail?courseId={couid}",
"TargetFrame=",
"Resource=0",
"Referer=",
LAST);
lr_end_transaction("课程详情", LR_AUTO);
lr_output_message(lr_eval_string("{scheduleid}"));
//随机打开一个课时
// web_set_max_html_param_len("262144");
// web_reg_save_param("ResponseBody", "LB=", "RB=", "Search=Body", LAST); //关联出所有的响应内容
// web_save_header(REQUEST, "RequestHeader");
// web_save_header(RESPONSE, "ResponseHeader");
web_add_auto_header("source", //1 是app 2是pc
"1");
lr_start_transaction("打开课时");
web_url("web_url",
"URL=http://{ip}/api/celery/listeningCourse/getConfig?courseScheduleId={scheduleid}&courseId={couid}",
"TargetFrame=",
"Resource=0",
"Referer=",
LAST);
lr_end_transaction("打开课时", LR_AUTO);
web_set_max_html_param_len("262144");
web_reg_save_param("ResponseBody", "LB=", "RB=", "Search=Body", LAST); //关联出所有的响应内容
web_save_header(REQUEST, "RequestHeader");
web_save_header(RESPONSE, "ResponseHeader");
//播放课时
web_add_auto_header("source","1");
lr_start_transaction("播放课时");
web_custom_request("web_custom_request",
"URL=http://{ip}/api/celery/userAttend/add",
"Method=post",
"RecContentType=application/json",
"EncType=application/json",
"TargetFrame=",
"Resource=0",
"Referer=",
"Mode=HTTP",
"Body={"rate":"1.0","actionKey":"","scheduleId":{scheduleid},"courseId":{couid},"itemType":2,"action":100}",
LAST);
lr_end_transaction("播放课时", LR_AUTO);
lr_convert_string_encoding(lr_eval_string("{RequestHeader}"),LR_ENC_UTF8 ,LR_ENC_SYSTEM_LOCALE,"RequestHeaderUTF8");
lr_convert_string_encoding(lr_eval_string("{ResponseHeader}"),LR_ENC_UTF8 ,LR_ENC_SYSTEM_LOCALE,"ResponseHeaderUTF8");
lr_convert_string_encoding(lr_eval_string("{ResponseBody}"),LR_ENC_UTF8 ,LR_ENC_SYSTEM_LOCALE,"ResponseBodyUTF8");
lr_output_message("# 请求头信息:
%s", lr_eval_string("{RequestHeaderUTF8}"));
lr_output_message("# 响应头信息:
%s", lr_eval_string("{ResponseHeaderUTF8}"));
lr_output_message("# 响应内容体:
%s", lr_eval_string("{ResponseBodyUTF8}"));
return 0;
}
以上是关于loadrunner-脚本设计的主要内容,如果未能解决你的问题,请参考以下文章