[ptt-users] Re: patch for agentbase.py
Lars Huttar
lars_huttar at sil.org
Mon Oct 1 09:27:54 PDT 2007
Oops, forgot to attach the patch. Here it is.
Lars
On 10/1/2007 11:24 AM, Lars Huttar wrote:
> Hello,
> I made a few small modifications to agentbase.py to make it more
> amenable to unit- and functional testing of web applications.
> As far as I can tell, currently the only kinds of verification
> supported are:
> - going to a URL (via click or goto) succeeded, that is, returned an
> HTTP success code
> - the title of a page is the expected string
>
> Often, you want to check that data is correct, e.g. that the
> application returned the correct data value for a query. To do this,
> you want to check the content returned.
> I modified the get() and post() functions to return the content as a
> string, so that these checks could be performed more easily by jython
> scripts.
>
> The proposed patch is attached.
>
> It would also be useful to add utility functions for checking that the
> returned content contains (or doesn't contain) a specified string:
> def mustContain(self, content, requiredString, diagnostic):
> '''Assert that content contains requiredString; else raise
> exception with diagnostic message.'''
> def mustNotContain(self, content, requiredString, diagnostic):
> '''Assert that content does not contain requiredString; else raise
> exception with diagnostic message.'''
>
> And it would be nice to have built-in support for XPath expressions,
> so that this doesn't have to be done by hand each time:
> def evalXPath(self, content, xpathExpr):
> '''Evaluate XPath expression with regard to content, and return
> result.'''
> This could be used either for boolean tests:
> not(/*/body//p[contains(., 'error')])
> or retrieving values
> /*/body//table[@class = 'accountDetails']/tr[td[1] = 'State']]/td[2]
>
> Is there already support for these kinds of things, and I missed it?
> If not, I would be happy to work on this, although I'm sure there are
> others who are more familiar with the relevant Python libraries.
>
> Regards,
> Lars
>
>
>
>
-------------- next part --------------
*** agentbase.py.orig Mon Aug 13 13:59:18 2007
--- agentbase.py Fri Sep 28 14:08:59 2007
***************
*** 140,145 ****
--- 140,146 ----
self.steps = 0 # Counter of steps in transaction
def get( self, url, paramsin=None ):
+ '''Make GET request to URL with params, and return response'''
self.params = paramsin
self.curl = url
***************
*** 167,175 ****
self.paramval = "?" + self.paramval
# Form request and connect
! self.connect( self.curl + self.paramval )
def post( self, url, params=None ):
self.params = params
self.curl = url
--- 168,177 ----
self.paramval = "?" + self.paramval
# Form request and connect
! return self.connect( self.curl + self.paramval )
def post( self, url, params=None ):
+ '''Make POST request to URL with params, and return response'''
self.params = params
self.curl = url
***************
*** 186,195 ****
for self.param in self.params:
self.body.addParameter( self.param[0], self.param[1] )
! self.connect( self.curl )
def connect( self, url ):
! ''' Checks the response for an error and logs the results '''
self.url = url
--- 188,197 ----
for self.param in self.params:
self.body.addParameter( self.param[0], self.param[1] )
! return self.connect( self.curl )
def connect( self, url ):
! '''Connect to URL, check the response for an error, log the results, and return the response'''
self.url = url
***************
*** 228,234 ****
return self.response
def errMsg( self ):
! ''' Returns the error message showing the cause of a problem '''
return "Step " + str( self.steps ) + \
" got response " + str( self.response.getResponseCode() ) + \
" when requesting: '" + self.http.getURL("http")
--- 230,236 ----
return self.response
def errMsg( self ):
! ''' Return the error message showing the cause of a problem '''
return "Step " + str( self.steps ) + \
" got response " + str( self.response.getResponseCode() ) + \
" when requesting: '" + self.http.getURL("http")
More information about the Users
mailing list