下面我们来看一下手动关联后的脚本情况:
最好不要用自动关联,虽然LR11.5已经准确了不少,但还是会出现截取不准确的情况。
- <SPAN>Action()
- {
- char DistrictName[15]={NULL},
- CityCode[20]={NULL},
- ProvinceCode[20]={NULL},
- DistrictCode[20]={NULL};
- int DistrictSequence=0;
- web_add_cookie("JSESSIONID=6B75C5017659AA3BFB2C89746A16D20F; DOMAIN=m.weathercn.com");
- </SPAN>
Action()
{
char DistrictName[15]={NULL},
CityCode[20]={NULL},
ProvinceCode[20]={NULL},
DistrictCode[20]={NULL};
int DistrictSequence=0;
web_add_cookie("JSESSIONID=6B75C5017659AA3BFB2C89746A16D20F; DOMAIN=m.weathercn.com");
/* type url,the default city code is shanghai, you can parameterize it */ /*这里默认定位是上海,可通过后面的关联将省代码获取到,然后进行参数化避免每次打开都是上海'*/
- <SPAN style="FONT-FAMILY: 'Microsoft YaHei',微软雅黑,SimHei,tahoma,arial,helvetica,sans-serif">web_url("m.weathercn.com",
- "URL=http://m.weathercn.com/?cid=01012601",
- "Resource=0",
- "RecContentType=text/html",
- "Referer=",
- "Snapshot=t1.inf",
- "Mode=HTTP",
- LAST);
- </SPAN>
web_url("m.weathercn.com", "URL=http://m.weathercn.com/?cid=01012601", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t1.inf", "Mode=HTTP", LAST);
/*这里有一点疑问,之前看自动关联LR生成的关联中写的为RegExp=pid=(.*?)\ \\" class,一直没有弄明白,这里为什么会多出一个\\而仍然可以关联成功*/
/*关联以获取到所有的省代码,LR在C语言中并不推荐使用web_reg_save_param_ex函数,这里用其实也是可以通过的*/
- <SPAN>
- lr_start_transaction("Defaultpage");
- web_url("Province",
- "URL=http://m.weathercn.com/province.jsp",
- "Resource=0",
- "RecContentType=text/html",
- "Referer=http://m.weathercn.com/?cid=01012601",
- "Snapshot=t2.inf",
- "Mode=HTTP",
- LAST);
- lr_end_transaction("Defaultpage",LR_AUTO);
-
-
- lr_paramarr_len("ProvincesCode");
- sprintf(ProvinceCode,"%s",lr_paramarr_random("ProvincesCode"));
- lr_save_string(lr_eval_string(ProvinceCode),"ProvinceCode");
- lr_output_message("%s",lr_eval_string("{ProvinceCode}"));
-
-
-
- web_reg_save_param(
- "CitiesCode",
- "LB=did=",
- "RB=&pid=",
- "ORD=all",
- LAST);
-
- lr_start_transaction("SelectCity");
- web_url("City",
- "URL=http://m.weathercn.com/dis.do?pid={ProvinceCode}",
- "Resource=0",
- "RecContentType=text/html",
- "Referer=http://m.weathercn.com/province.jsp",
- "Snapshot=t3.inf",
- "Mode=HTTP",
- LAST);
- lr_end_transaction("SelectCity",LR_AUTO);
-
-
- sprintf(CityCode,"%s",lr_paramarr_random("CitiesCode"));
- lr_save_string(lr_eval_string(CityCode),"CityCode");
- lr_output_message("%s",lr_eval_string("{CityCode}"));
-
- web_reg_save_param(
- "DistrictsCode",
- "LB=cid=",
- "RB=&pid",
- "ORD=all",
- LAST);
-
-
- web_reg_save_param_regexp(
- "paramName=DistrictsName",
- "RegExp=class=\"c\">(.{4,8})</a>\r\n",
- "Ordinal=all",
- LAST);
-
-
- .
- .
- .
- .
- .
- .
- .
- '中间太长了我就不贴了,操作都是类似的。下面通过是将具体的省、市、区保存为参数来进行参数化'
-
-
- srand(time(NULL));
- DistrictSequence=(rand()%(lr_paramarr_len("DistrictsCode")))+1;
- lr_output_message("%d",DistrictSequence);
- sprintf(DistrictName,"{DistrictsName_%d}",DistrictSequence);
- lr_save_string(lr_eval_string(DistrictName),"DistrictName");
- sprintf(DistrictCode,"{DistrictsCode_%d}",DistrictSequence);
- lr_save_string(lr_eval_string(DistrictCode),"DistrictCode");
- lr_output_message("%s",lr_eval_string("{DistrictName}"));
- lr_output_message("%s",lr_eval_string("{DistrictCode}"));
-
-
- web_reg_find("Search=Body",
- "Text={DistrictName}",
- LAST);
- lr_start_transaction("ReportPage");
- web_url("WeatherReport",
- "URL=http://m.weathercn.com/index.do?cid={DistrictCode}&pid={ProvinceCode}",
- "Resource=0",
- "RecContentType=text/html",
- "Referer=http://m.weathercn.com/cout.do?did={CityCode}&pid={ProvinceCode}",
- "Snapshot=t5.inf",
- "Mode=HTTP",
- LAST);
- lr_end_transaction("ReportPage",LR_AUTO);
-
-
- web_concurrent_start(NULL);
-
- web_url("Day02.png",
- "URL=http://m.weathercn.com/fh30/Day02.png",
- "Resource=1",
- "RecContentType=image/png",
- "Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114",
- LAST);
-
- web_url("Day00.png",
- "URL=http://m.weathercn.com/fh100/Day00.png",
- "Resource=1",
- "RecContentType=image/png",
- "Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114",
- LAST);
-
- web_url("Night02.png",
- "URL=http://m.weathercn.com/fh30/Night02.png",
- "Resource=1",
- "RecContentType=image/png",
- "Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114",
- LAST);
-
- web_concurrent_end(NULL);
-
- return 0;
- }
- 下面我们就来简单的看一个服务器返回的内容来如何进行参数化。</SPAN>
/*select province*/
lr_start_transaction("Defaultpage");
web_url("Province",
"URL=http://m.weathercn.com/province.jsp",
"Resource=0",
"RecContentType=text/html",
"Referer=http://m.weathercn.com/?cid=01012601",
"Snapshot=t2.inf",
"Mode=HTTP",
LAST);
lr_end_transaction("Defaultpage",LR_AUTO);
lr_paramarr_len("ProvincesCode"); /*'输出数组长度'*/
/*save random procince code as a parameter*/
sprintf(ProvinceCode,"%s",lr_paramarr_random("ProvincesCode"));
/*根据数组内容来随机获取并保存为变量*/
lr_save_string(lr_eval_string(ProvinceCode),"ProvinceCode");
/*将变量保存为参数,这一步是必需的,否则将无法参数化*/
lr_output_message("%s",lr_eval_string("{ProvinceCode}"));
/*obtain city code,web_reg_save_param can function well here, but not suggested */
web_reg_save_param(
"CitiesCode",
"LB=did=",
"RB=&pid=",
"ORD=all",
LAST);
/*select city */
lr_start_transaction("SelectCity");
web_url("City",
"URL=http://m.weathercn.com/dis.do?pid={ProvinceCode}", /* the default value is 0101114*/
"Resource=0",
"RecContentType=text/html",
"Referer=http://m.weathercn.com/province.jsp",
"Snapshot=t3.inf",
"Mode=HTTP",
LAST);
lr_end_transaction("SelectCity",LR_AUTO);
/* save city code as a parameter*/
sprintf(CityCode,"%s",lr_paramarr_random("CitiesCode"));
lr_save_string(lr_eval_string(CityCode),"CityCode");
lr_output_message("%s",lr_eval_string("{CityCode}"));
/*obtain district code*/
web_reg_save_param(
"DistrictsCode",
"LB=cid=",
"RB=&pid",
"ORD=all",
LAST);
/*obtain district name for text check.*/
web_reg_save_param_regexp(
"paramName=DistrictsName",
"RegExp=class=\"c\">(.{4,8})</a>\r\n", /*match Chinese character use ([\u4e00-\u9fa5]{4,6}).. because chinese character display inproper here */
"Ordinal=all",
LAST);
.
.
.
.
.
.
.
'中间太长了我就不贴了,操作都是类似的。下面通过是将具体的省、市、区保存为参数来进行参数化'
/* generate random number to acquire a district name corresponding with district code*/
srand(time(NULL));
DistrictSequence=(rand()%(lr_paramarr_len("DistrictsCode")))+1;
/*save district name and district code as a parameter*/
lr_output_message("%d",DistrictSequence);
sprintf(DistrictName,"{DistrictsName_%d}",DistrictSequence);
lr_save_string(lr_eval_string(DistrictName),"DistrictName");
sprintf(DistrictCode,"{DistrictsCode_%d}",DistrictSequence);
lr_save_string(lr_eval_string(DistrictCode),"DistrictCode");
lr_output_message("%s",lr_eval_string("{DistrictName}"));
lr_output_message("%s",lr_eval_string("{DistrictCode}"));
/*text check*/
/*设置文本检查点,下好从上面保存为参数的DistrictName中获取*/
web_reg_find("Search=Body",
"Text={DistrictName}",
LAST);
/*weather report*/
lr_start_transaction("ReportPage");
web_url("WeatherReport",
"URL=http://m.weathercn.com/index.do?cid={DistrictCode}&pid={ProvinceCode}", /* default cid=0101141012 pid=0101114*/
"Resource=0",
"RecContentType=text/html",
"Referer=http://m.weathercn.com/cout.do?did={CityCode}&pid={ProvinceCode}",
"Snapshot=t5.inf",
"Mode=HTTP",
LAST);
lr_end_transaction("ReportPage",LR_AUTO);
/*以下内容没有进行参数化,以下内容主要是一些像显示天气状态的图片内容,之前看到过一篇文件说关于web_concurrent_start只是提交的数据有区别,所以没有必要进行参数化' */
/* you can parameterize the following value */
web_concurrent_start(NULL);
web_url("Day02.png",
"URL=http://m.weathercn.com/fh30/Day02.png",
"Resource=1",
"RecContentType=image/png",
"Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114",
LAST);
web_url("Day00.png",
"URL=http://m.weathercn.com/fh100/Day00.png",
"Resource=1",
"RecContentType=image/png",
"Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114",
LAST);
web_url("Night02.png",
"URL=http://m.weathercn.com/fh30/Night02.png",
"Resource=1",
"RecContentType=image/png",
"Referer=http://m.weathercn.com/index.do?cid=0101141012&pid=010114",
LAST);
web_concurrent_end(NULL);
return 0;
}
下面我们就来简单的看一个服务器返回的内容来如何进行参数化。
- <SPAN style="FONT-FAMILY: 'Microsoft YaHei',微软雅黑,SimHei,tahoma,arial,helvetica,sans-serif"><ul id="more">
- <li><a href="dis.do?pid=010101" class="c">??</a>|<a href="dis.do?pid=010126" class="c">??</a>|<a href="dis.do?pid=010129" class="c">??</a></li>
- <li><a href="dis.do?pid=010104" class="c">??</a>|<a href="dis.do?pid=010116" class="c">??</a>|<a href="dis.do?pid=010113" class="c">???</a></li>
- <li><a href="dis.do?pid=010119" class="c">??</a>|<a href="dis.do?pid=010120" class="c">???</a>|<a href="dis.do?pid=010132" class="c">??</a></li>
- <li><a href="dis.do?pid=010121" class="c">??</a>|<a href="dis.do?pid=010122" class="c">??</a>|<a href="dis.do?pid=010106" class="c">??</a></li>
- <li><a href="dis.do?pid=010125" class="c">??</a>|<a href="dis.do?pid=010130" class="c">??</a>|<a href="dis.do?pid=010127" class="c">??</a></li>
- <li><a href="dis.do?pid=010109" class="c">??</a>|<a href="dis.do?pid=010133" class="c">??</a>|<a href="dis.do?pid=010124" class="c">??</a></li>
- <li><a href="dis.do?pid=010111" class="c">??</a>|<a href="dis.do?pid=010123" class="c">??</a>|<a href="dis.do?pid=010112" class="c">??</a></li>
- <li><a href="dis.do?pid=010102" class="c">??</a>|<a href="dis.do?pid=010117" class="c">??</a>|<a href="dis.do?pid=010114" class="c">??</a></li>
- <li><a href="dis.do?pid=010115" class="c">??</a>|<a href="dis.do?pid=010118" class="c">??</a>|<a href="dis.do?pid=010134" class="c">??</a></li>
- <li><a href="dis.do?pid=010105" class="c">??</a>|<a href="dis.do?pid=010128" class="c">??</a>|<a href="dis.do?pid=010108" class="c">??</a></li>
- <li><a href="dis.do?pid=010110" class="c">??</a>|<a href="dis.do?pid=010107" class="c">??</a>|<a href="dis.do?pid=010131" class="c">??</a></li>
- <li><a href="dis.do?pid=010103" class="c">??</a></li></SPAN>
<ul id="more"> <li><a href="dis.do?pid=010101" class="c">??</a>|<a href="dis.do?pid=010126" class="c">??</a>|<a href="dis.do?pid=010129" class="c">??</a></li> <li><a href="dis.do?pid=010104" class="c">??</a>|<a href="dis.do?pid=010116" class="c">??</a>|<a href="dis.do?pid=010113" class="c">???</a></li> <li><a href="dis.do?pid=010119" class="c">??</a>|<a href="dis.do?pid=010120" class="c">???</a>|<a href="dis.do?pid=010132" class="c">??</a></li> <li><a href="dis.do?pid=010121" class="c">??</a>|<a href="dis.do?pid=010122" class="c">??</a>|<a href="dis.do?pid=010106" class="c">??</a></li> <li><a href="dis.do?pid=010125" class="c">??</a>|<a href="dis.do?pid=010130" class="c">??</a>|<a href="dis.do?pid=010127" class="c">??</a></li> <li><a href="dis.do?pid=010109" class="c">??</a>|<a href="dis.do?pid=010133" class="c">??</a>|<a href="dis.do?pid=010124" class="c">??</a></li> <li><a href="dis.do?pid=010111" class="c">??</a>|<a href="dis.do?pid=010123" class="c">??</a>|<a href="dis.do?pid=010112" class="c">??</a></li> <li><a href="dis.do?pid=010102" class="c">??</a>|<a href="dis.do?pid=010117" class="c">??</a>|<a href="dis.do?pid=010114" class="c">??</a></li> <li><a href="dis.do?pid=010115" class="c">??</a>|<a href="dis.do?pid=010118" class="c">??</a>|<a href="dis.do?pid=010134" class="c">??</a></li> <li><a href="dis.do?pid=010105" class="c">??</a>|<a href="dis.do?pid=010128" class="c">??</a>|<a href="dis.do?pid=010108" class="c">??</a></li> <li><a href="dis.do?pid=010110" class="c">??</a>|<a href="dis.do?pid=010107" class="c">??</a>|<a href="dis.do?pid=010131" class="c">??</a></li> <li><a href="dis.do?pid=010103" class="c">??</a></li>
因为当时区域没有修改,导致中间有些问号,其实中间部分主要是一些地方区的中文名称。
可以看出都还是比较有规律的。可用的几个函数都在以上列出,其中LR11.5中最新加的一个函数regexp最大的特点是可以用正则表达式,但我还是习惯用web_reg_save_param_ex ,因为LB/IC 和RB/IC可以来根据边界匹配,二者在实际中运用可能还是会有差别,可能会出现有些情况只能用其中一种才能解决的问题,之前在做上面的时候就曾遇到过用正则表达式匹配出的值会多一些。像上面返回中有还有一行
- <SPAN style="FONT-FAMILY: 'Microsoft YaHei',微软雅黑,SimHei,tahoma,arial,helvetica,sans-serif"><div align="center"><a href="#" class="c">?????????</a></div></SPAN>
<div align="center"><a href="#" class="c">?????????</a></div>
如果用正则表达式就很难区分以上标签中的内容与实际要参数化的内容了。当然这里还是可以的,因为中文长度不一致,可能通过正则表达式限制长度解决,但如果长度也与要获取的参数相同又该怎处理呢?
因为获取上面的参数可以用如下关联函数解决
- <SPAN style="FONT-FAMILY: 'Microsoft YaHei',微软雅黑,SimHei,tahoma,arial,helvetica,sans-serif"> web_reg_save_param_regexp(
- "paramName=DistrictsName",
- "RegExp=class=\"c\">(.{4,8})</a>\r\n",
- "Ordinal=all",
- LAST);
- '这里通过限制长度还是能够正确获取的'</SPAN>