Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Recorder-Generated Test Script Structure


The structure of a recorded test-script looks like this:

(
...
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)
     ...
     )

Import statements

you will import classes that make the tests work.By default,the following are indispensable.

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, Cookie, CookieModule, CookiePolicyHandler

The first two import statements import the Test and grinder objects, which give you access to grinder framework and its environment. Next two import statements import utility classes which perform HTTP Requests. The NVPair class lets you organize parameters and values for POST requests nto name-value pairs.

Header definitions

These headers are used to create the request objects .The generated code looks like this:

connectionDefaults = HTTPPluginControl.getConnectionDefaults()
httpUtilities = HTTPPluginControl.getHTTPUtilities()

log = grinder.logger.info
err = grinder.logger.error

# Set up a cookie handler to log all cookies that are sent and received.
class MyCookiePolicyHandler(CookiePolicyHandler):
	def acceptCookie(self, cookie, request, response):
		# log("accept cookie: %s" % cookie)
		return 1

	def sendCookie(self, cookie, request):
		# log("send cookie: %s" % cookie)
		return 1
 
def createRequest(url, headers=None):
	"""Create an instrumented HTTPRequest."""
	request = HTTPRequest(url=url)
	if headers: request.headers=headers
	return request

CookieModule.setCookiePolicyHandler(MyCookiePolicyHandler())
 
# These definitions at the top level of the file are evaluated once,
# when the worker process is started.
connectionDefaults.defaultHeaders = NVPair('Accept-Encoding', 'gzip, deflate'),
	NVPair('Referer', 'https://accounts.google.com/ServiceLogin?service=mail&passive=true&continue=http://mail.google.com/mail/?ui%3Dmobile%26zyp%3Dl&scc=1<mpl=ecobh&nui=5&btmpl=mobile'),
	NVPair('Accept-Language', 'ko,Korean;q=0.7,en-US;q=0.3'),
	NVPair('User-Agent', 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 1_2_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5')


headers0 = NVPair('Accept', '*/*')

The first statement prepares an HTTPPluginConnection object, which lets you control the behavior of the connection. The next statement prepares an instance of an HTTPUtilities object which provides access to various utility methods.

In the next few lines, the script defines the headers. First, it sets up the default headers that are used by each request.

Request and test definitions

This are the actual requests that we are going to use for our tests:

request_https_ssl_google_analytics_com = createRequest("https://ssl.google-analytics.com");
request_https_accounts_youtube_com = createRequest("https://accounts.youtube.com");
request_https_mail_google_com = createRequest("https://mail.google.com");

Page definitions

After the request objects have been created, it goes to the actual TestRunner class.

# A method for each recorded page.
	def page1(self) :
		##########################################################################################
		# https://accounts.youtube.com/accounts/CheckConnection
		##########################################################################################
		grinder.sleep(196)
		self.token_pmpo = 'https://accounts.google.com'  
		self.token_v = '180753535'  
		self.token_timestamp = '1366165396372'  
		result = request_https_accounts_youtube_com.GET(
				"/accounts/CheckConnection?pmpo=%s&v=%s×tamp=%s"
					% (self.token_pmpo, self.token_v, self.token_timestamp),
				None,	
				NVPair('Accept', 'application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*')
			) 
		self.checkResponse(result, 200,  "https://accounts.youtube.com/accounts/CheckConnection")

		##########################################################################################
		# https://ssl.google-analytics.com/__utm.gif
		##########################################################################################
		self.token_utmwv = '5.4.1'  
		self.token_utms = '1'  
		self.token_utmn = '1757647728'  
		self.token_utmhn = 'accounts.google.com'  
		self.token_utmcs = 'utf-8'  
		self.token_utmsr = '1920x1080'  
		self.token_utmvp = '839x617'  
		self.token_utmsc = '24-bit'  
		self.token_utmul = 'en-us'  
		self.token_utmje = '1'  
		self.token_utmfl = '11.6 r602'  
		self.token_utmdt = 'Gmail: Google 이메일'  
		self.token_utmhid = '1841284240'  
		self.token_utmr = '-'  
		self.token_utmp = '/mail/homepage'  
		self.token_utmht = '1366165396400'  
		self.token_utmac = 'UA-992684-1'  
		self.token_utmcc = '__utma=72592003.2014635375.1366162754.1366162754.1366165396.2;+__utmz=72592003.1366162754.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);'  
		self.token_utmu = 'qjCAAC~'  
		result = request_https_ssl_google_analytics_com.GET(
				"/__utm.gif?utmwv=%s&utms=%s&utmn=%s&utmhn=%s&utmcs=%s&utmsr=%s&utmvp=%s&utmsc=%s&utmul=%s&utmje=%s&utmfl=%s&utmdt=%s&utmhid=%s&utmr=%s&utmp=%s&utmht=%s&utmac=%s&utmcc=%s&utmu=%s"
					% (self.token_utmwv, self.token_utms, self.token_utmn, self.token_utmhn, self.token_utmcs, self.token_utmsr, self.token_utmvp, self.token_utmsc, self.token_utmul, self.token_utmje, self.token_utmfl, self.token_utmdt, self.token_utmhid, self.token_utmr, self.token_utmp, self.token_utmht, self.token_utmac, self.token_utmcc, self.token_utmu),
				None,	
				headers0
			) 
		self.checkResponse(result, 200,  "https://ssl.google-analytics.com/__utm.gif")

		##########################################################################################
		# https://mail.google.com/mail/images/c.gif
		##########################################################################################
		grinder.sleep(94)
		self.token_t = '1366165396404'  
		result = request_https_mail_google_com.GET(
				"/mail/images/c.gif?t=%s"
					% (self.token_t),
				None,	
				headers0
			) 
		self.checkResponse(result, 200,  "https://mail.google.com/mail/images/c.gif")

In this script, it contains one method for each recorded page like page1, page2.

Within each page, there are multiple GET and/or POST requests made via the request objects, which simulate the transactions made by a single page.

__call__ method

In Jython any object implementing __call__ is callable, which basically means that this function is called when you call the class instance as a function. In the context of the this script, it just means that this is where all the testing starts off. In the __call__ method you can see that there are calls to all the recorded page methods,and this essentially runs the recorded test.

	def __call__(self) :
		try :
			self.page1()
			grinder.statistics.forLastTest.success = 1
		except Exception, e:
			err(e.message)
			grinder.statistics.forLastTest.success = 0

comments powered by Disqus
Page info
viewed 1995 times
translations en
Author
posted last year by
Matt
Contributors
updated last year by
View revisions
Share this article