loadrunner传k-v,用web_custom_request函数。
init里面执行登录,根据返回获取到tokenId,action中,执行登录后的操作。
详细脚本如下:
vuser_init
vuser_init() { //获取response响应信息 //web_reg_save_param("response","LB=","RB=",LAST); //获取tokenId值 web_reg_save_param("tokenId","Search=Body","LB="tokenId":"","RB="",LAST); //添加头文件 //web_add_header("Content-Type","application/json;charset=utf-8"); web_add_header("imei","{userAccount}"); //web_add_header("Pragma","no-cache"); //web_add_header("appversion","2.1.1.2"); //web_add_header("systemversion","android:5.0.2"); web_custom_request("login", "URL=http://127.0.0.1:8081/login", "Method=POST", "Resource=0", "RecContentType=application/json", "Referer=", "Mode=HTTP", // O5WDjUwauLx3cACPSkczsElnGiD8n0RDGunFQDMecI96lmBzR5xc2VQhPV0NZbFdaik8tkZsq+kedFdomJ3A9fRklLkJT11IV45mZa9/R45UslH/hJqzPVShm/9vVptgpwYbJZkhkcPx1LSeXJLPwg==|MTIzNDU2Nzg5MDk4NzY1NA==|9eU7qirx0+uJKqcedCzyBQ== "EncType=application/json", "Body={"userAccount":"{userAccount}","userPassword":"O5WDjUwauLx3cACPSkczsElnGiD8n0RDGunFQDMecI96lmBzR5xc2VQhPV0NZbFdaik8tkZsq+kedFdomJ3A9fRklLkJT11IV45mZa9\/R45UslH\/hJqzPVShm\/9vVptgpwYbJZkhkcPx1LSeXJLPwg==|MTIzNDU2Nzg5MDk4NzY1NA==|9eU7qirx0+uJKqcedCzyBQ=="}", LAST); //输出code值 //lr_output_message("输出code值:%s",lr_eval_string("{code}")); //输出tokenId值 //lr_output_message("输出tokenId值:%s",lr_eval_string("{tokenId}")); //对获取response信息进行转码 //lr_convert_string_encoding(lr_eval_string("{response}"),LR_ENC_UTF8,"GBK","result"); //输出response响应信息 //lr_output_message( "返回response响应信息:%s",lr_eval_string("{result}")); return 0; }
Action
Action() { int res = 0; //int int_res = 0; //char * string_res = (char*)calloc(100,sizeof(char)); // 请求存储空间 //设置检查点 web_reg_find("Text="code":"00"","SaveCount=c00",LAST); web_reg_find("Text="code":"8016"","SaveCount=c8016",LAST); web_reg_find("Text="code":"8017"","SaveCount=c8017",LAST); //web_reg_find("Text="code":"8001"",LAST); //获取response响应信息 //web_reg_save_param("response","LB=","RB=",LAST); lr_start_transaction("payPause"); web_custom_request("activityHandler", "URL=http://127.0.0.1:8081/payPause", "Method=POST", "Resource=0", "RecContentType=application/json", "Referer=", "Mode=HTTP", "EncType=application/json", "Body={"tokenId":"{tokenId}","cardNo":"62010101{userAccount}"}", LAST); res = atoi(lr_eval_string("{c00}"))+atoi(lr_eval_string("{c8016}"))+atoi(lr_eval_string("{c8017}")); //下面四行是调试信息,int和string需要互转 //sprintf(string_res,"%d",res); //int转string //lr_output_message("string_res = %s",string_res); //int_res = atoi(string_res); //string转int,其实这里不需要转了,因为res已经是int,这里只是再次体现string如何转int //lr_output_message("int_res = %d",int_res); if (res > 0){ //lr_output_message("事务成功"); lr_end_transaction("payPause",LR_PASS); } else{ //lr_error_message("事务失败"); lr_end_transaction("payPause",LR_FAIL); } //lr_convert_string_encoding(lr_eval_string("{response}"),LR_ENC_UTF8,"GBK","result"); //输出response响应信息 //lr_output_message( "返回response响应信息:%s",lr_eval_string("{result}")); //lr_end_transaction("payPause", LR_AUTO); return 0; }
vuser_end
vuser_end() { return 0; }