Loadrunner开发测试脚本
开发测试脚本可以通过录制,也可以手动开发,建议能录制的尽量录制,省时省力,不能录制的只能费力自己开发了,具体看项目情况来决定。
使用Loadrunner开发脚本过程中和Jmeter一样,需要了解HTTP传输原理,使用Fiddler等工具进程抓包,根据请求信息编写请求表单,根据响应信息判断返回值做关联处理,在Action中插入->新建步骤中添加需要的函数,设置请求参数来逐步完成每个请求。
在开发流程测试脚本过程中,往往需要将上一个请求的返回值作为下个请求的参数,这时候需要用到关联,很多时候Loadrunner自动关联不是万能的,需要我们自己定义关联提取出需要的信息,以下为关联部分脚本:
Action() { lr_start_transaction("打开登陆页"); web_url("web_url", "URL={URL}/user/login.html", "TargetFrame=", "Resource=0", "Referer=", LAST); lr_end_transaction("打开登陆页", LR_AUTO); lr_start_transaction("提交登陆表单"); web_submit_data("web_submit_data", "Action={URL}/user/login.do", "Method=POST", "TargetFrame=", "Referer=", ITEMDATA, "Name=acct", "Value={username}", ENDITEM, "Name=beforeURL", "Value=", ENDITEM, "Name=password", "Value={password}", ENDITEM, "Name=verifyCode", "Value=1234", ENDITEM, LAST); lr_end_transaction("提交登陆表单", LR_AUTO); lr_start_transaction("出借列表"); web_url("web_url", "URL={URL}/front/investmentList.html", "TargetFrame=", "Resource=0", "Referer=", LAST); lr_end_transaction("出借列表", LR_AUTO); //提取“去出借”请求的“loanId”参数,在“出借”请求中调用; web_reg_save_param("loanId", "LB=type=\"hidden\" id=\"loanId\" value=\"", "RB=\"/>", LAST); lr_start_transaction("去出借"); web_url("web_url", "URL={URL}/loanObject/2220003.html", "TargetFrame=", "Resource=0", "Referer=", LAST); lr_end_transaction("去出借", LR_AUTO); //以下为提取“出借”请求的关联,在“出借跳转”请求参数中调用; web_reg_save_param("functionNum", "LB=functionNum\\\":\\\"", "RB=\\\",\\\"orderId", LAST); web_reg_save_param("orderId", "LB=orderId\\\":\\\"", "RB=\\\"", LAST); web_reg_save_param("ordDate", "LB=ordDate\\\":\\\"", "RB=\\\"", LAST); web_reg_save_param("investorId", "LB=investorId\\\":\\\"", "RB=\\\"", LAST); web_reg_save_param("borrowerId", "LB=investorId\\\":\\\"", "RB=\\\"", LAST); web_reg_save_param("fullAmount", "LB=fullAmount\\\":", "RB=,\\", LAST); web_reg_save_param("amount", "LB=amount\\\":", "RB=,\\", LAST); web_reg_save_param("signStr", "LB=signStr\\\":\\\"", "RB=\\\"", LAST); web_reg_save_param("freezeOrdId", "LB=freezeOrdId\\\":\\\"", "RB=\\\"", LAST); lr_start_transaction("出借"); web_submit_data("web_submit_data", "Action={URL}/loanObject/invest.do", "Method=POST", "RecContentType=application/json", "TargetFrame=", "Referer=", ITEMDATA, "Name=loanId", "Value={loanId}", ENDITEM, "Name=amount", "Value=100", ENDITEM, LAST); lr_end_transaction("出借", LR_AUTO); lr_start_transaction("出借跳转"); web_submit_data("web_submit_data", "Action={URL}/intf/bid.do", "Method=POST", "EncodeAtSign=YES", "TargetFrame=", "Referer=", "Mode=HTML", ITEMDATA, "Name=jsonDataString", "Value={\"functionNum\":\"{functionNum}\",\"orderId\":\"{orderId}\",\"ordDate\":\"{ordDate}\",\"investorId\":\"{investorId}\",\"borrowerId\":\"{borrowerId}\",\"loanId\":\"{loanId}\",\"amount\":{amount},\"fullAmount\":{fullAmount},\"fee\":0.00,\"feeRate\":1.00,\"terminal\":\"pc\",\"maxTenderRate\":0.5,\"isFreeze\":\"Y\",\"freezeOrdId\":\"{freezeOrdId}\",\"signStr\":\"{signStr}\"}", ENDITEM, LAST); lr_end_transaction("出借跳转", LR_AUTO); return 0; } |
提取数据根据请求响应信息的格式编写关联脚本,如抓包响应信息如下:
提取orderId参数的关联脚本为:
web_reg_save_param("orderId", "LB=orderId\\\":\\\"", "RB=\\\"", LAST); |
脚本调试过程中,根据脚本调试的目的来选取回放日志的类型,如果要查看提交表单请求的参数信息,选择扩展日志的高级跟踪:
如果要查看关联提取值是否正确,选择扩展日志的参数替换;