本文介紹nGrinder測試腳本實現HTTP Post 請求。 html
nGrinder腳本結構 java
[ ... import statements ... ] [ ... header definitions ... ] [ ... url definitions ... ] [ ... request and test definitions ... ] class TestRunner: [ ... method definitions - a method is defined for each recorded page ... ] def __call__(self): [ ... calls to defined methods, which actually runs the requests ... ] [ utility function (I'll go over this later) ] [ ... calls to utility function to wrap/instrument tests (I'll go over this later) ... ]引用語句
from net.grinder.script import Test from net.grinder.script.Grinder import grinder from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest from HTTPClient import NVPair
以上是引用所須要的類。Test 和 grinder均可以訪問內部的測試架。HTTPPluginControl, HTTPRequest是操做HTTP請求。NVPair 則是爲POST請求提供數據。 spring
Header定義 app
connectionDefaults = HTTPPluginControl.getConnectionDefaults() httpUtilities = HTTPPluginControl.getHTTPUtilities() connectionDefaults.defaultHeaders = \ ( NVPair('Accept-Language', 'en-us,en;q=0.5'), NVPair('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'), NVPair('Accept-Encoding', 'gzip,deflate'), NVPair('User-Agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.11) Gecko/2009060308 Ubuntu/9.04 (jaunty) Firefox/3.0.11'), )第一條語句是建立 HTTPPluginConnection 對象來控制鏈接,然後就是定義了它默認的header。
URL定義 jsp
url0 = 'http://SpringMvc.com'Request and test 定義
在這裏須要定義實際請求中用到的Test和Request 測試
request101 = HTTPRequest(url=url0, headers=headers0) request101 = Test(101, 'POST processLogin.jsp').wrap(request101) request102 = HTTPRequest(url=url0, headers=headers0) request102 = Test(102, 'GET home.jsp').wrap(request102) request201 = HTTPRequest(url=url0, headers=headers1) request201 = Test(201, 'GET popUpTask.jsp').wrap(request201)
方法定義 this
# A method for each recorded page. def page1(self): """GET / (requests 101-104).""" result = request101.GET('/') grinder.sleep(307) # Expecting 302 'Moved Temporarily' request102.GET('/slices/infusion-crm.gif') grinder.sleep(29) # Expecting 302 'Moved Temporarily' request103.GET('/login/defaultLogin.jsp') self.token_msg = \ httpUtilities.valueFromLocationURI('msg') # 'Whoa,+easy+there+tiger.+You\'re+gonna+nee...' grinder.sleep(24) request104.GET('/index.jsp' + '?msg=' + self.token_msg) return result def page2(self): """POST processLogin.jsp (requests 201-202).""" # Expecting 302 'Moved Temporarily' result = request201.POST('/login/processLogin.jsp', ( NVPair('username', 'vivin'), NVPair('password', 'abAB12!@'), NVPair('Login', 'Login'), ), ( NVPair('Content-Type', 'application/x-www-form-urlencoded'), )) grinder.sleep(72) request202.GET('/Admin/home.jsp') return result在這裏能夠看到全部的請求頁面,page1和page2
__call__ 方法 url
在Python語言中,調用__call__就意味着全部的test都會別調用。在這個方法裏能夠看到page1和page2會被調用。
spa
下面就來介紹一個完整的實現POST請求的腳本 code
from net.grinder.script import Test from net.grinder.script.Grinder import grinder from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest from HTTPClient import NVPair connectionDefaults = HTTPPluginControl.getConnectionDefaults() httpUtilities = HTTPPluginControl.getHTTPUtilities() connectionDefaults.defaultHeaders = \ [ NVPair('Accept-Encoding', 'gzip, deflate'), NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'), NVPair('User-Agent', 'Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/15.0 /15.0a1'), NVPair('Accept-Language', 'en-us,en;q=0.5'), ] headers1= \ [ NVPair('Referer', 'http://SpringMvc.com/login'), ] headers2= \ [ NVPair('Referer', 'http://SpringMvc.com/welcome'), ] url0 = 'http://SpringMvc.com' # Create an HTTPRequest for each request, then replace the # reference to the HTTPRequest with an instrumented version. # You can access the unadorned instance using request101.__target__. request101 = HTTPRequest(url=url0) request101 = Test(101, 'GET welcome').wrap(request101) request102 = HTTPRequest(url=url0) request102 = Test(102, 'GET login').wrap(request102) request201 = HTTPRequest(url=url0, headers=headers1) request201 = Test(201, 'POST j_spring_security_check').wrap(request201) request202 = HTTPRequest(url=url0, headers=headers1) request202 = Test(202, 'GET welcome').wrap(request202) request301 = HTTPRequest(url=url0, headers=headers2) request301 = Test(301, 'GET j_spring_security_logout').wrap(request301) request302 = HTTPRequest(url=url0, headers=headers2) request302 = Test(302, 'GET logout').wrap(request302) class TestRunner: # A method for each recorded page. def page1(self): """GET welcome (requests 101-102).""" # Expecting 302 'Moved Temporarily' result = request101.GET('/welcome') request102.GET('/login') return result def page2(self): """POST j_spring_security_check (requests 201-202).""" # Expecting 302 'Moved Temporarily' result = request201.POST('/j_spring_security_check', ( NVPair('j_username', 'test_user'), NVPair('j_password', '123456'), NVPair('submit', 'submit'), ), ( NVPair('Content-Type', 'application/x-www-form-urlencoded'), )) request202.GET('/welcome') return result def page3(self): """GET j_spring_security_logout (requests 301-302).""" # Expecting 302 'Moved Temporarily' result = request301.GET('/j_spring_security_logout') request302.GET('/logout') return result def __call__(self): """Called for every run performed by the worker thread.""" self.page1() # GET welcome (requests 101-102) grinder.sleep(xxxx) self.page2() # POST j_spring_security_check (requests 201-202) grinder.sleep(xxxx) self.page3() # GET j_spring_security_logout (requests 301-302)此場景很簡單,page1 爲登陸頁面,page2爲輸入名稱密碼登陸,page3 爲退出系統