webInject 手册
一 系统框架
WebInject为perl语言编写,其系统框架如下:
webInject包含webInject Engine,可以用命令行调用,或者使用GUI调用webInject Engine. webInject Engine对应代码文件为webinject.pl.
二 webInject使用
webInject可以使用GUI运行以及命令行运行.下面步骤为gui运行,command运行具体参看: http://www.webinject.org/manual.html
步骤一:下载webInject: http://www.webinject.org/download.html
步骤二:解压,例如C:/ webinject
步骤三:双击webinjectgui.exe运行webinject自带的gui.
步骤四:点击run,运行默认配置config.xml
三 配置
3.1 config.xml
Config.xml用于配置测试项目,config.xml指定webInject运行哪几个test case并配置一些运行中使用的常量.config.xml是GUI运行时默认的配置文件名称.在命令行运行webInject时候,用户可以指定自己的配置文件.
下面讲述config,xml中使用的各类参数.
3.1.1 Proxy(代理)
在config.xml中,配置http request代理.代码如下:
<proxy>http://127.0.0.1:8080</proxy>
如果配置需要验证的代理服务器,代码中加入用户名+密码.代码如下:
<proxy>http://username:password@127.0.0.1:8080</proxy>
3.1.2 useragent(User-agent翻译为用户代理)
User-agent 字段指的是每个request的自身的标志符,相当于request的身份证号码.默认的user-agent为webInject
<useragent>Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)</useragent>
3.1.3 httpauth(http验证)
对于需要http验证,配置如下:
<httpauth>www.fakedomain.com:80:my_realm:foo:welcome</httpauth>
3.1.4 baseurl(常量baseurl)
<baseurl>http://myserver</baseurl>
3.1.5 globalhttplog(httplog配置)
参数yes,记录所有的test case运行过程中的http log
参数onfail,只记录失败的test case的http log.
3.1.6 comment(注释)
<comment>this will be ignored</comment>
3.1.7 timeout(超时时间)
定义http响应的timeout时间(单位:second),如果test case中的response相应超过设定的timeout时间.这个test case表明为失败.默认配置为180秒.
<timeout>10</timeout>
3.1.8 reporttype(报告形式)
这个配置项,用来指定输出的报告以何种形式显示.这里可以指定外部程序作为显示插件.例如:
<reporttype>mrtg</reporttype>
<reporttype>nagios</reporttype>
3.1.8 globatimeout
这个配置项用于nagios 插件,当所有的case通过,但是时间超过globaltimeout时间,警告消息将发送至nagios.
<globaltimeout>10</globaltimeout>
3.1.9 Gnuplot
使用此参数,配置webinject使用的gnuplot产生相应时间图表.
<gnuplot>/usr/bin/gnuplot</gnuplot>
3.1.10 standaloneplot
使用webinject.pl生成png图片.此参数只对gui模式下有效.
<standaloneplot>on</standaloneplot>
3.2
测试用例配置 testcase.xml.
配置文件中,配置测试用例文件.代码如下:
<testcasefile>tests_1.xml</testcasefile>
<testcasefile>tests_2.xml</testcasefile>
<testcasefile>tests_3.xml</testcasefile>
Note:可以使用相对路径.
3.3 命令模式(指定自定义的config 文件)
webInject engine(webinject.pl)可以在命令模式下直接调用.
webinject.pl [-c|--config config_file] [-o|--output output_location]
[-n|--no-output] [testcase_file [XPath]]
命令模式下,可以直接指定测试用例.
perl webinject.pl mytests.xml
如果没有指定测试用例,将会查找config.xml中配置的测试用例,如果没有测试用例配置,将会查找testases.xml文件.如果还是没有,将会报错.
下面的命令直接执行mytests.xml中的第二个case.
perl webinject.pl mytests.xml testcases/case[2]
4 测试用例
测试用例为xml 文档,文档中只有id 以及url为必选项,其他为可选项,如果没有验证参数,pass的标准为http response在范围100-399以内.
最简单的测试用例配置如下:
<case
id="1"
url="http://myserver/test/test.html"
/>
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | "application/x-www-form-urlencoded"下为text data, “multipart/form-data”下为perl code "text/xml" or "application/soap+xml"下为指定外部文件地址: |
| verifyresponsecode
| |
| verifypositive
| |
| verifypositive2 | |
| verifypositive3 | |
| verifynegative | |
| verifynegative1
| |
| verifynextpositive
| |
| verifynextnegative | |
| logrequest |
|
| logresponse |
|
| parseresponse | |
| sleep |
|
| errormessage | |
| addheader |
|
4.1 重复执行
重复执行测试用例代码如下:
<testcases repeat="5">
4.2 xml文档中的一些特殊字符
符号名称 | 正确代码 | 错误代码 |
<
| |
|
|
|
|
4.3常量设置
常量设置以{} 在config.xml中设置.例子如下:
如果test case中用到: url=http://myserver/test/login.jsp
在config.xml中如下设置: <baseurl>http://myserver</baseurl>
Test case中可以如下重写: url="{BASEURL}/test/login.jsp"
常量设置非常有用,特别是你的测试环境需要改变.比如上面中myserver的名字在不同的测试环境下不同.
5 通过以及失败的标准
定义测试用例通过还是失败.
5.1 Verifications(从内容中定义)
“verifypositive” 如果response中不包含此内容,则测试失败
“verifynegative” 如果response中包含此内容,测试失败.
Verifypositive以及verifynegative可以使用string或者正则表达式.
5.2 http response code
如果返回的http response code与指定的code相同则通过,反之不通过.
默认下通过范围为100-399
5 输出结果
webInject的输出结果有三个地方:
5.1 显示结果于gui中
5.2 显示结果于result.html中
Result.html中为可以得到最详细的细节.
5.3 显示结果于result.xml中
6 session的处理以及状态管理
6.1 summary
http 为无状态协议,因此web应用使用不同的方法来管理状态.一种方法为使用cookies,另一种方法为使用session id.
下面介绍如何使用session id :
1 在test case中设置如下代码:
parseresponse="JSESSIONID=|;"
这段代码将捕捉上一个上一个response的session id,凡是http header中左面为JSESSIONID=右面为;里面的内容将被赋值到变量{PARSEDRESULT}中.
当url中使用{PARSEDRESULT},webinject将自动替代捕获到的sessionid.
url="http://myserver/search.jsp?value=123&;JSESSIONID={PARSEDRESULT}"
上述代码下:如果上一个response的header为
Set-Cookie: JSESSIONID=16CD67F723A6D2218CE73AEAEA899FD9; Path=/
在发送过去的url将被解析为: http://myserver/search.jsp?value=123&;JSESSIONID=16CD67F723A6D2218CE73AEAEA899FD9
7 webInject的不足
1 不支持中文2 异常处理不好(config.xml中配置错误路径的testcase.xml,run gui后界面无反应)
3 不执行response中的脚本.(javascript,vbscript等)
4 不支持http 重定向