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:[email protected]: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 重定向