利用LoadRunner进行http接口功能自…
2015-05-30 11:34
671 查看
需要取得的输入应预先***了CSV文件,关在脚本参数配置中定义变量。
自动化测试程序关键代码
1、生成结果文件(html格式),文件名称为 test[/u] _系统时间(%Y%m%d%H%M%S)_虚拟用户编号,并写入测试结果文件的html开始标识
CODE:
//定义结果文件变量
long file;
//定义文件名种子(虚拟用户编号)变量
char *vusernum;
//定义测试结果变量
char V_Result[1024];
vuser_init()
{
//取得文件名种子(虚拟用户编号)
vusernum=lr_eval_string ("_{vuserid}");
//取得文件种子(系统时间)
lr_save_datetime("%Y%m%d%H%M%S", DATE_NOW,
"now_date");
//拼结测试结果文件名称
strcpy(V_Result,"d://test/Result/test");
strcat(V_Result,lr_eval_string("_{now_date}"));
strcat(V_Result,vusernum);
strcat(V_Result,".html");
//生成并打开测试结果文件
file=fopen(V_Result,"at+");
//写入测试文件头部html信息
strcpy(V_Result,"<
td>IMSI号码");
fputs(V_Result,file);
return 0;
}2、从参数化文件读取测试参数和预期结果、发送请求并获得服务器返回实际结果,比较测试结果后写入测试结果文件。
CODE:
Action()
{
//测试结果文本
char V_testres[1024];
//定义返回结果是否正确变量
int result;
//取得IMSI号码
char *V_imsi=lr_eval_string ("{IMSI}");
//设置页面接收最大的字节数,该设置应大于服务器返回内容的大小
web_set_max_html_param_len("20000");
//取得服务器返回内容
web_reg_save_param("filecontent",
"LB=",
"RB=",
"Search=Body",
LAST);
//发送请求
web_submit_data("login",
"Action=http://host:port/autonavit/search?cmd=clientlogin&termver=5&termcode=30001&termdbver=3
",
"Method=POST",
"RecContentType=text/html",
"Referer=",
"Snapshot=t9.inf",
"Mode=HTTP",
ITEMDATA,
"Name=imsi", "Value={IMSI}", ENDITEM,
LAST);
//比较预期值和实际值是否相等
result=strcmp(lr_eval_string("{YQJG}"),lr_eval_string("{filecontent}"));
if ( result == 0 )
{
strcpy(V_testres,"通过");
}
else
{
strcpy(V_testres,"失败");
}
strcpy(V_Result,"");
strcat(V_Result,"");
strcat(V_Result,"");
strcat(V_Result,"");
fputs(V_Result,file);
return 0;
}3、写入测试结果文件尾部html信息,关闭文件并结束测试。
CODE:
vuser_end()
{
//结束并关闭文件
strcpy(V_Result,"
");
fputs(V_Result,file);
fclose(file);
return 0;
}
自动化测试程序关键代码
1、生成结果文件(html格式),文件名称为 test[/u] _系统时间(%Y%m%d%H%M%S)_虚拟用户编号,并写入测试结果文件的html开始标识
CODE:
//定义结果文件变量
long file;
//定义文件名种子(虚拟用户编号)变量
char *vusernum;
//定义测试结果变量
char V_Result[1024];
vuser_init()
{
//取得文件名种子(虚拟用户编号)
vusernum=lr_eval_string ("_{vuserid}");
//取得文件种子(系统时间)
lr_save_datetime("%Y%m%d%H%M%S", DATE_NOW,
"now_date");
//拼结测试结果文件名称
strcpy(V_Result,"d://test/Result/test");
strcat(V_Result,lr_eval_string("_{now_date}"));
strcat(V_Result,vusernum);
strcat(V_Result,".html");
//生成并打开测试结果文件
file=fopen(V_Result,"at+");
//写入测试文件头部html信息
strcpy(V_Result,"<
td>IMSI号码");
fputs(V_Result,file);
return 0;
}2、从参数化文件读取测试参数和预期结果、发送请求并获得服务器返回实际结果,比较测试结果后写入测试结果文件。
CODE:
Action()
{
//测试结果文本
char V_testres[1024];
//定义返回结果是否正确变量
int result;
//取得IMSI号码
char *V_imsi=lr_eval_string ("{IMSI}");
//设置页面接收最大的字节数,该设置应大于服务器返回内容的大小
web_set_max_html_param_len("20000");
//取得服务器返回内容
web_reg_save_param("filecontent",
"LB=",
"RB=",
"Search=Body",
LAST);
//发送请求
web_submit_data("login",
"Action=http://host:port/autonavit/search?cmd=clientlogin&termver=5&termcode=30001&termdbver=3
",
"Method=POST",
"RecContentType=text/html",
"Referer=",
"Snapshot=t9.inf",
"Mode=HTTP",
ITEMDATA,
"Name=imsi", "Value={IMSI}", ENDITEM,
LAST);
//比较预期值和实际值是否相等
result=strcmp(lr_eval_string("{YQJG}"),lr_eval_string("{filecontent}"));
if ( result == 0 )
{
strcpy(V_testres,"通过");
}
else
{
strcpy(V_testres,"失败");
}
strcpy(V_Result,"");
strcat(V_Result,"");
strcat(V_Result,"");
strcat(V_Result,"");
fputs(V_Result,file);
return 0;
}3、写入测试结果文件尾部html信息,关闭文件并结束测试。
CODE:
vuser_end()
{
//结束并关闭文件
strcpy(V_Result,"
预期值 | 返回值< /td> | 结果 | |
"); //写入测试参数 strcat(V_Result,V_imsi); strcat(V_Result," | "); //写入预期结果 strcat(V_Result,lr_eval_string("{YQJG}")); strcat(V_Result," | "); //写入实际结果 strcat(V_Result,lr_eval_string("{filecontent}")); strcat(V_Result," | "); //写入测试是否通过 strcat(V_Result, V_testres); strcat(V_Result," |
fputs(V_Result,file);
fclose(file);
return 0;
}
相关文章推荐
- 网络七层协议的形象说明
- 使用loadrunner调用HTTPWatch实现…
- Watin与Httpwatch工具结合进行测试
- TCP/IP、Http、Socket的区别
- Spark MLlib Deep Learning Neural Net(深度学习-神经网络)1.2
- Mina学习笔记 TcpClient
- [原]Spark MLlib Deep Learning Neural Net(深度学习-神经网络)1.3
- Spark MLlib Deep Learning Neural Net(深度学习-神经网络)1.1
- Java多线程与网络编程综合使用
- http协议
- 卷积神经网络算法的一个实现
- Java Web之网络通讯
- python使用re, os, httplib, urllib批量下载网页上的图片
- http://guoyifan163.blog.163.com/
- HTTP 400/401/403/404/500网页错误代码是什么意思
- Spring MVC使用HttpClient返回json报406错误解决办法
- 新手如何做好网络营销
- 菜鸟好文推荐(五)——理解 HTTPS 协议
- 利用HttpWebRequest实现实体对象的上传
- 颓废的战士