相对于C语言,对java可能会更加熟悉点,某些无法录制的或者单接口压测的场景,动手写脚本还是实用的。
下面的脚本中演示了lrapi.jar中相关http协议的相关函数使用方式。
/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: * */ import lrapi.*; public class Actions { public int init() throws Throwable { return 0; }//end of init public int action() throws Throwable { String url = "http://localhost:8080/AutoTest/"; lr.start_transaction("登录"); web.reg_save_param("return_code_login",new String[]{ "LB={\"returnCode\":", "RB=","SaveOffset=0","SaveLen=1", "LAST"}); web.submit_data("loginAction", "Action="+url+"user-toLogin", new String[]{ "Method=POST", "RecContentType=application/json", "Mode=HTML" }, new String[]{ "Name=username","Value=admin",web.ENDITEM, "Name=password","Value=q708162543",web.ENDITEM, web.LAST }); if(lr.eval_string("{return_code_login}").equals("0")){ lr.end_transaction("登录",lr.AUTO); }else{ lr.error_message("登录失败:returnCode="+lr.eval_string("{return_code_login}")); lr.end_transaction("登录",lr.FAIL); } lr.start_transaction("获取登录信息"); web.reg_save_param("return_code",new String[]{ "LB={\"returnCode\":", "RB=","SaveOffset=0","SaveLen=1", "LAST"}); web.url("localhost", "URL="+url+"user-getLoginUserInfo", new String[]{ "TargetFrame=", //"TargetBrowser=Mercury Technologies", "Resource=0", "RecContentType=application/json", "Snapshot=t1.inf", "Mode=HTML", "LAST"}); if(lr.eval_string("{return_code}").equals("0")){ lr.end_transaction("获取登录信息", lr.AUTO); }else{ lr.error_message(lr.eval_string("{return_code}")); lr.end_transaction("获取登录信息", lr.FAIL); } return 0; }//end of action public int end() throws Throwable { return 0; }//end of end }
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于