原创

LoadRunner12中的JavaUser脚本编写

相对于C语言,对java可能会更加熟悉点,某些无法录制的或者单接口压测的场景,动手写脚本还是实用的。

下面的脚本中演示了lrapi.jar中相关http协议的相关函数使用方式。

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
}
正文到此结束
本文目录