测试环境:Win7 x64+IE8+loadRunner11
在loadrunner中可以添加检查点,以检查从服务器返回的内容是否正确。
添加检查点方法:将脚本切换到Tree View,右键单击要检查的内容,选择Insert After或Insert Before,在弹出窗口中选择Web Checks-Image Check或Text Check.
用这种方式添加的检查点,对应的函数是:
Image Check:web_image_check
Text Check:web_find
首先需要注意的是,使用这两个函数来进行检查,必须要在Run-time Settings-Preferences中,勾选"Enable Image and text check",否则检查函数不会被执行,系统默认是不勾选该项的,原因是使用检查函数会消耗较多的内存,在系统给出的注释中可以看到。
下图就是没有勾选"Enable Image and text check"时,执行脚本时得到的日志信息
从"Enable Image and text check"的description中还可以看到,这两个检查函数只能用于以HTML-based方式录制的脚本,录制脚本的方式是在Recording Options中选择的
如下两图即是在以URL-based方式录制的脚本中使用了这两个检查函数得到的运行日志
查看VuGen的帮助文档中web_find函数的General Information,
General Information
The web_find function searches an HTML page for a specified text string. web_find is deprecated. It has been replaced with web_reg_find.
This function is limited to HTML–based recorded scripts (see Recording Options > Recording tab). It searches the context only after the HTML request is complete, resulting in slower execution time than web_reg_find.
The web_find function has been superseded in C Language scripts by web_reg_find, which runs faster and can be used in both HTML–based and URL–based recording. web_find is supported in C for backward compatibility. In Java and Visual Basic, it has not been superseded.
The web_find function is not supported for WAP scripts running in HTTP or Wireless Session Protocol (WSP) replay mode.
它是在服务器返回的页面上查找文本,所以必须将其放到被检查内容的后面,如下是成功搜索到文本的日志信息:
如下是没有找到搜索的文本的日志信息:
出错会导致脚本停止运行,如果希望出错后仍可以继续运行脚本,需要在Run-time Settings中勾选"Continue on error"
如果在设置web_find函数时使用了RightOf和LeftOf参数
需要注意的是待搜索字符串右侧的空格不要包括进来,否则运行时将找不到指定条件的字符串
如果把web_find函数放在了检查内容的前面,将会得到如下的报错信息:
vuser_init.c(15): Error -27985: There is no context for HTML-based functions. A previous function may not have used "Mode=HTML" or downloaded only non-HTML page(s), or the context has been reset (e.g., due to a GUI-based function) [MsgId: MERR-27985]
vuser_init.c(15): web_find highest severity level was "ERROR" [MsgId: MMSG-26391]
查看帮助文档中web_image_check函数的General Information,
General Information
The web_image_check function verifies the presence of a specified image inside an HTML page.
Either Alt or Src, or both, must be in the argument list. If both are passed, the check passes if both match a single image element.
This function is only supported for HTML–based scripts.
web_image_check函数的alt和src参数,值取的是网页源代码中,被检查图片的img标签中的alt和src属性的值
以下是成功找到检查图片的日志信息:
vuser_init.c(36): "web_image_check" succeeded (1 occurrence(s) found. Alt="", Src="images/hp_logo.png") [MsgId: MMSG-27192]
vuser_init.c(36): web_image_check was successful [MsgId: MMSG-26392]
以下是没有找到检查的图片的日志信息:
vuser_init.c(36): Error -27191: "web_image_check" failed (0 occurrence(s) found. Alt="", Src="images/hp_logo1.png") [MsgId: MERR-27191]
vuser_init.c(36): web_image_check highest severity level was "ERROR" [MsgId: MMSG-26391]
web_image_check的位置同样需要放在被检查内容的后面,如果放在前面,运行时也会得到如下报错信息:
vuser_init.c(11): Error -27985: There is no context for HTML-based functions. A previous function may not have used "Mode=HTML" or downloaded only non-HTML page(s), or the context has been reset (e.g., due to a GUI-based function) [MsgId: MERR-27985]
vuser_init.c(11): web_image_check highest severity level was "ERROR" [MsgId: MMSG-26391]
对于文本的检查,除了可以使用函数web_find之外,还可以使用web_reg_find函数。
查看帮助文档中web_reg_find的General Information,
General Information
The web_reg_find function registers a request to search for a text string on a Web page retrieved by the next action function, such as web_url.
This function helps you verify whether or not the page you received is the desired page by searching for an expected text string. For example, you can search for the text "Welcome" to check if your home page opened properly. You can check for the word "Error" to check if the browser encountered an error. You can also use this function to register a request to count the number of times the text appeared.
If the check fails, the error is reported after the next action function executes. This function only registers requests, but does not perform them. Thus, the return value of web_reg_find indicates only if the registration succeeded, and not if the check succeeded.
You can search for either the text or the strings surrounding the text. Do not specify both text, and a prefix–suffix pair.
This function can be used for both HTML–based and URL–based scripts (see Recording Options > Recording tab). It registers the search request before the buffers arrive, so the buffers are scanned as they come. This results in a more efficient script with better performance.
To globally search all subsequent Action functions (not only the following one), use web_global_verification. The web_global_verification function is useful in detecting application level errors that are not represented by HTTP status codes. To locate errors that are represented by HTTP status codes, use the web_get_int_property.
从这里不难看出,web_reg_find的位置必须放在被检查内容的前面.
以下一个段落来自http://www.ithao123.cn/content-2849412.html
Web_reg_find是注册类型函数,它本身并不执行,不能通过它的返回值来作为事务的判断条件(因为web_reg_find()的返回值0和1表示web_reg_find()是否注册成功,并不代表查找的内容是否存在,也就是说无论查找的文本内容是否存在,都返回0。它是从返回的缓冲区扫描而不是在接收的页面中查找。这是比web_find更高效的一个函数。
在Tree View视图模式中,插入web_reg_find的方法是选中要检查的项,右键单击,选择Insert Before,选择Services-web_reg_find
web_reg_find常用的参数有Text(定义搜索的字符串)、TextPfx和TextSfx(定义搜索字符串的左右边界)、Serch(定义搜索范围)、SaveCount(记录搜索到的次数)、Fail(失败条件)
下图是web_reg_find成功找到搜索的字符串的日志信息
下图是web_reg_find没有找到搜索的字符串的日志信息
可以利用web_reg_find的SaveCount参数来控制输出日志信息。例如,对于LoadRunner自带的WebTours程序,我们在登录成功后的第一个显示页面上搜索字符串"to the Web Tours reservation pages."来判断登录是否成功。
我们在登录操作前插入web_reg_find,在登录操作之后使用if语句,通过判断SaveCount参数的值,来输出登录是否成功的信息,代码如下:
web_reg_find("Fail=NotFound",
"Search=Body",
"SaveCount=reservation_Count",
"Text=to the Web Tours reservation pages.",
LAST);
//log in
web_submit_form("login.pl",
"Snapshot=t2.inf",
ITEMDATA,
"Name=username", "Value=lchydp", ENDITEM,
"Name=password", "Value=111111", ENDITEM,
"Name=login.x", "Value=55", ENDITEM,
"Name=login.y", "Value=10", ENDITEM,
LAST);
if(atoi(lr_eval_string("{reservation_Count}"))>0)
lr_output_message("Log in successfully!");
else
lr_error_message("Log in Failly!");
运行后,从日志中可以看到,web_reg_find成功找到了搜索的字符串,并改变了reservation_Count的值,执行了if语句,正确输出了登录成功的信息。
需要注意的是,if语句必须要放在登录操作的后面,如果放到了前面,也就是如下的代码:
web_reg_find("Fail=NotFound",
"Search=Body",
"SaveCount=reservation_Count",
"Text=to the Web Tours reservation pages.",
LAST);
if(atoi(lr_eval_string("{reservation_Count}"))>0)
lr_output_message("Log in successfully!");
else
lr_error_message("Log in Failly!");
//log in
web_submit_form("login.pl",
"Snapshot=t2.inf",
ITEMDATA,
"Name=username", "Value=lchydp", ENDITEM,
"Name=password", "Value=111111", ENDITEM,
"Name=login.x", "Value=55", ENDITEM,
"Name=login.y", "Value=10", ENDITEM,
LAST);
我们运行一下,看一下日志:
可以看到先执行了if语句,然后才找到搜索的字符串并改变reservation_Count的值,由于if语句执行时,reservation_Count的值并未发生变化,就导致误报了登录失败。
如果将web_reg_find放到了被检查内容的后面,如下面的脚本:
vuser_init()
{
web_url("WebTours",
"URL=http://127.0.0.1:1080/WebTours/",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTML",
LAST);
web_reg_find("Text=Web Tours",
LAST);
return 0;
}
由于web_reg_find后面没有action function,将报错:
除了用在Tree View中右键插入检查点这种方式之外,还可以在Script View中直接书写函数语句;也可以在Script View或Tree View中选择Insert菜单-New Step,选择Services或Web Checks来插入检查点。
如果是在Tree View中,使用Insert菜单来插入检查点,是插入到选中项的后边。
以上描述的插入检查点的方法均是在录制结束后插入,如果在录制时插入,需要先选中想搜索的文本,然后点击录制工具条的Insert text check按钮来插入。
这种方式插入的是web_reg_find函数,并且参数只有Text.
从帮助文档中还可以看到,在LoadRunner的C语言脚本中,web_find已不被赞成使用,它可以被更高效的web_reg_find所取代。
参考资料:
1.Web_find、Web_reg_find和Web_image_check了解多少?http://www.ithao123.cn/content-2849412.html
2.Loadrunner中web_find和web_reg_find函数的使用与区别,http://blog.sina.com.cn/s/blog_5857124a0100oy9d.html
3.LoadRunner11-设置集合点及检查点,http://wenku.baidu.com/link?url=PR_zM-jc_w0zAk27j5ltqZ83JuiqckAuzUIF134EbBy-0_A0utZkeNNbxXXlSYsf-Yn0Aa9l2NurawC9aneUXjIR5NOWR1FM-1CJHEJMmve
4.LoadRunner检查点使用小结,http://wenku.baidu.com/link?url=M_cfc6-lVaGRvdtY3eilWko0hoOVvZLWFlyxoyxpYgMjgeV2rKHJbFjgqHfkdIH3gC2d-n3MHdvHFO8oRguXFy3ll6XQOu1sBqj_qMUl3Gm
5.HP LoadRunner Online Function Reference