在运行脚本或做负载测试时,经常需要更改ip地址及端口号,可以写成公共的函数
在做接口的性能测试时,有很多接口是差不多的,只要更改接口名及参数即可,这种也可以写成公共的函数。下面简单介绍一下
写一个公共的api_url.h
//char url[100] = "192.168.0.80:9000";
char url[100] = "223.5.14.59:9020";
//char url[100] = "192.168.7.5:9000";
//char url[100] = "210.22.155.234:9000";
void save_url(char *url){
lr_save_string(url,"url");
}
void api_get(char *url,char *name,char *parameter){
lr_save_string(url,"url");
lr_save_string(name,"name");
lr_save_string(parameter,"parameter");
web_custom_request(name,
"URL=http://{url}/{name}?{parameter}",
"Method=GET",
"RecContentType=text/xml",
LAST);
}
void api_post(char *url,char *name,char *parameter){
lr_save_string(url,"url");
lr_save_string(name,"name");
lr_save_string(parameter,"parameter");
web_custom_request(name,
"URL=http://{url}/{name}",
"Method=POST",
"RecContentType=text/xml",
"body={parameter}",
LAST);
}
这个函数需要放置在同脚本的同一级目录,在写脚本时,需要把该公共文件包含进来,如下:
#include "..\api_url.h"----这样声明了才能调用api_url.h中的函数
Action()
{
web_reg_save_param("getLocation","LB=","RB=","search=body",LAST);
web_reg_find("FAIL=NOTFOUND","Search=BODY","SaveCount=check","Text=</description>",LAST);
api_get(url,"getLocation","userName=test1336&password=111111&locatorId=1336&mode=1");
lr_output_message("-----%s",lr_eval_string("{getLocation}"));
return 0;
}
也可以如下,直接调用url地址:
#include "..\api_url.h"
Action()
{
//char url[100] = "192.168.0.80";
//char url[100] = "223.5.14.59:9020";
web_reg_save_param("getLocation","LB=","RB=","search=body",LAST);
web_reg_find("FAIL=NOTFOUND","Search=BODY","SaveCount=check","Text=</description>",LAST);
//api_get(url,"getLocation","userName=test1336&password=111111&locatorId=1336&mode=1");
save_url(url);
web_custom_request("GetLocation",
"URL=http://{url}/getLocation?userName=test1336&password=111111&locatorId=1336&mode=1",
"Method=GET",
"RecContentType=text/xml","ContentEncoding=deflate",
LAST);
lr_output_message("-----%s",lr_eval_string("{getLocation}"));
return 0;
}