loadrunner12.55:登录功能--手动关联【示例】


示例1:

该平台登录过程及脚本实现过程如下:

1、通过“http://10.248.57.226:9010/user-center/user-info/publicKey?machineFlag=1&randomStr=1395644086717349888”请求获取响应报文中的publicKey及randomStr字段;(脚本保存publicKey字段到文件publicKey.txt中,同时保存randomStr字段为参数{ResprandomStr})

2、根据获取的publicKey生成用户名和密码的密文;(脚本通过批处理文件getEncrypt.bat调用Java应用生成密文并分别保存至文件userName.txt和passwd.txt中,通过lr_read_file()函数读取密文到参数{name}和{passwd}中)

3、根据生成的密文发起请求http://10.248.65.91:8010/user-center/user/login?machineFlag=1&pwd={passwd}&userName={name}&randomStr={ResprandomStr}完成用户名和密码验证,并返回重定向URL及授权token;(脚本使用参数{name}、{passwd}、{ResprandomStr}发起请求获取响应头中的location字段,即重定向URL及token)

4、凭借token和重定向URL开展后续会话;

脚本调用JAVA应用的批处理文件 getEncrypt.bat :

@echo off
set filepath=%~dp0

call cd %filepath%
call javac -encoding UTF-8 Encrypt2.java
call java Encrypt2 


::pause

 登录脚本:

/* -------------------------------------------------------------------------------
	Script Title       : 
	Script Description : 
                        
                        
	Recorder Version   : 911
   ------------------------------------------------------------------------------- */

vuser_init()
{
	FILE *fp;
	char mytoken[1024]="Bearer ";
	char filepath[]="D:\\03.LR_TraningCourse\\DaQianDuan";
	char command[1024];
	
	web_set_user("user_1", "123456", "10.248.57.226:9010");
	
	web_url("login", 
		"URL=http://10.248.57.226:9010/login", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=", 
		"Snapshot=t13.inf", 
		"Mode=HTML", 
		EXTRARES, 
		"Url=/static/img/login_logo.142e525b.png", ENDITEM, 
		LAST);

	//获取响应中的publicKey字段值
	web_reg_save_param("ResppublicKey",
	"LB=publicKey\":\"",
	"RB=\",\"machineFlag",
	"Search=Body",
	LAST);
	
	//获取响应中的randomStr字段值
	web_reg_save_param("ResprandomStr",
	"LB=randomStr\":\"",
	"RB=\"},\"code",
	"Search=Body",
	LAST);

	web_url("publicKey", 
		"URL=http://10.248.57.226:9010/user-center/user-info/publicKey?machineFlag=1&randomStr=1395661090648064000", 
		"Resource=0", 
		"RecContentType=application/json", 
		"Referer=http://10.248.57.226:9010/login", 
		"Snapshot=t14.inf", 
		"Mode=HTML", 
		LAST);
	
	sprintf (command, "d: & cd %s & javac -encoding UTF-8 Encrypt2.java & java Encrypt2 publicKey%s.txt",filepath,lr_eval_string("{UserID}"));
	system(command);
	lr_output_message(command);
	
	//分别读取Java应用加密用户名和密码生成的密文,并分别保存到参数name和passwd中
	lr_read_file("D:\\03.LR_TraningCourse\\DaQianDuan\\userName.txt","name",0);
	lr_read_file("D:\\03.LR_TraningCourse\\DaQianDuan\\passwd.txt","passwd",0);	
	
	web_set_max_html_param_len("1024");
	//web_save_header(RESPONSE, "ResponseHeader");// RESPONSE保存响应的头信息 
	
	web_reg_save_param("location",//获取响应头中Location字段,即重定向URL
		"LB=Location:",
		"RB=\r\nContent-Length",
		"Search=Headers",
		LAST);
	web_reg_save_param("Token",//获取响应头中token字段内容,用于后续授权认证
		"LB=token=",
		"RB=\r\nContent-Length",
		"Search=Headers",
		LAST);
	
	web_reg_save_param("responseBody",
	"LB=",
	"RB=",
	"Search=Body",
	LAST);

	web_url("login_2", 
		"URL=http://10.248.65.91:8010/user-center/user/login?machineFlag=1&pwd={passwd}&userName={name}&randomStr={ResprandomStr}", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=http://10.248.57.226:9010/login", 
		"Snapshot=t15.inf", 
		"Mode=HTML", 
		LAST);
	
	strcat(mytoken,lr_eval_string("{Token}"));
	lr_output_message("%s",mytoken);
	
	web_add_auto_header("Authorization",mytoken);//将token值添加到请求头的Authorization字段中
	
	web_url("getSystemDetailList",
		"URL=http://10.248.57.226:9010/user-center/user/getSystemDetailList?userName=user_1&loginUrl=http:%2F%2F10.248.57.226:9010",
		"Resource=0",
		"RecContentType=application/json",
		"Referer=${location}",
		"Snapshot=t16.inf",
		"Mode=HTML",
		EXTRARES,
		"URL=/static/img/ch.1bfbffa1.png", "Referer=http://10.248.57.226:9010/", ENDITEM,
		"URL=/static/img/user_p.607a8ef8.png", "Referer=http://10.248.57.226:9010/", ENDITEM,
		"URL=/static/img/down.cfed1b07.png", "Referer=http://10.248.57.226:9010/", ENDITEM,
		LAST);

	web_url("10.248.57.226:9020", 
		"URL=http://10.248.57.226:9020/", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=http://10.248.57.226:9010/U-C/", 
		"Snapshot=t17.inf", 
		"Mode=HTML", 
		EXTRARES, 
		"Url=http://10.248.57.226:9010/static/fonts/element-icons.535877f5.woff", "Referer=http://10.248.57.226:9010/U-C/", ENDITEM, 
		LAST);

	web_url("10.248.57.226:9030", 
		"URL=http://10.248.57.226:9030/", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=http://10.248.57.226:9010/U-C/", 
		"Snapshot=t18.inf", 
		"Mode=HTML", 
		LAST);

	web_custom_request("search", 
		"URL=http://10.248.57.226:9010/user-center/user-info/search", 
		"Method=POST", 
		"Resource=0", 
		"RecContentType=application/json", 
		"Referer=http://10.248.57.226:9010/U-C/", 
		"Snapshot=t19.inf", 
		"Mode=HTML", 
		"EncType=application/json;charset=utf-8", 
		"Body={\"current\":1,\"size\":5,\"userInfoDTO\":{\"realName\":\"\",\"userName\":\"\",\"id\":\"\"}}", 
		LAST);

	return 0;
}