Commit 788a154b authored by Arnaud Fontaine's avatar Arnaud Fontaine

Define __getattr__ on non-Browser classes rather than modifying the class dictionary

parent 3db880ef
...@@ -54,6 +54,13 @@ def measurementMetaClass(prefix): ...@@ -54,6 +54,13 @@ def measurementMetaClass(prefix):
besides of Browser. besides of Browser.
""" """
def __new__(metacls, name, bases, dictionary): def __new__(metacls, name, bases, dictionary):
def nonBrowserClass_getattr(self, attribute):
"""
Some attributes, such as lastRequestSeconds, are only defined in the
Browser class
"""
return getattr(self.browser, attribute)
def timeInSecondDecorator(method): def timeInSecondDecorator(method):
def wrapper(self, *args, **kwargs): def wrapper(self, *args, **kwargs):
""" """
...@@ -103,14 +110,8 @@ def measurementMetaClass(prefix): ...@@ -103,14 +110,8 @@ def measurementMetaClass(prefix):
if callable(attribute) and not getattr(attribute, '__is_wrapper__', False): if callable(attribute) and not getattr(attribute, '__is_wrapper__', False):
applyMeasure(attribute) applyMeasure(attribute)
# lastRequestSeconds properties are only defined on classes inheriting
# from zope.testbrowser.browser.Browser, so create these properties for
# all other classes too
if 'Browser' not in bases[0].__name__: if 'Browser' not in bases[0].__name__:
time_method = lambda self: self.browser.lastRequestSeconds dictionary['__getattr__'] = nonBrowserClass_getattr
time_method.func_name = 'lastRequestSeconds'
time_method.__doc__ = Browser.lastRequestSeconds.__doc__
dictionary['lastRequestSeconds'] = property(time_method)
return super(MeasurementMetaClass, return super(MeasurementMetaClass,
metacls).__new__(metacls, name, bases, dictionary) metacls).__new__(metacls, name, bases, dictionary)
...@@ -566,7 +567,7 @@ class MainForm(Form): ...@@ -566,7 +567,7 @@ class MainForm(Form):
class attribute name, if class_attribute class attribute name, if class_attribute
parameter is given. parameter is given.
""" """
self.browser._logger.debug( self._logger.debug(
"Submitting (name='%s', label='%s', class='%s')" % (name, label, "Submitting (name='%s', label='%s', class='%s')" % (name, label,
class_attribute)) class_attribute))
...@@ -645,7 +646,7 @@ class MainForm(Form): ...@@ -645,7 +646,7 @@ class MainForm(Form):
value = item value = item
break break
self.browser._logger.debug("select_id='%s', label='%s', value='%s'" % \ self._logger.debug("select_id='%s', label='%s', value='%s'" % \
(select_name, label, value)) (select_name, label, value))
select_control.getControl(label=label, value=value, select_control.getControl(label=label, value=value,
...@@ -668,12 +669,12 @@ class MainForm(Form): ...@@ -668,12 +669,12 @@ class MainForm(Form):
into the page content? into the page content?
""" """
if 'Logged In as' in self.browser.contents: if 'Logged In as' in self.browser.contents:
self.browser._logger.debug("Already logged in") self._logger.debug("Already logged in")
# TODO: Perhaps zope.testbrowser should be patched instead? # TODO: Perhaps zope.testbrowser should be patched instead?
self.browser.timer.start_time = self.browser.timer.end_time = 0 self.browser.timer.start_time = self.browser.timer.end_time = 0
return return
self.browser._logger.debug("Logging in: username='%s', password='%s'" % \ self._logger.debug("Logging in: username='%s', password='%s'" % \
(self.browser._username, self.browser._password)) (self.browser._username, self.browser._password))
def login(form): def login(form):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment