[Checkins] SVN: zope.testbrowser/trunk/ Fixed a bug that caused the ``Browser`` to keep it's previous ``contents``

Adam Groszer agroszer at gmail.com
Tue Sep 21 07:33:37 EDT 2010


Log message for revision 116702:
  Fixed a bug that caused the ``Browser`` to keep it's previous ``contents``
  Also adjusted exception messages at places to match pre version 3.4.1 messages.
  

Changed:
  U   zope.testbrowser/trunk/CHANGES.txt
  U   zope.testbrowser/trunk/src/zope/testbrowser/browser.py
  U   zope.testbrowser/trunk/src/zope/testbrowser/fixed-bugs.txt
  U   zope.testbrowser/trunk/src/zope/testbrowser/ftests/__init__.py
  U   zope.testbrowser/trunk/src/zope/testbrowser/ftests/ftesting.zcml
  A   zope.testbrowser/trunk/src/zope/testbrowser/ftests/status_lead.html

-=-
Modified: zope.testbrowser/trunk/CHANGES.txt
===================================================================
--- zope.testbrowser/trunk/CHANGES.txt	2010-09-21 09:57:29 UTC (rev 116701)
+++ zope.testbrowser/trunk/CHANGES.txt	2010-09-21 11:33:37 UTC (rev 116702)
@@ -5,9 +5,17 @@
 3.10.1 (unreleased)
 -------------------
 
-- Nothing changed yet.
+- Fixed a bug that caused the ``Browser`` to keep it's previous ``contents``
+  The places are:
+  - Link.click()
+  - SubmitControl.click()
+  - ImageControl.click()
+  - Form.submit()
 
+- Also adjusted exception messages at the above places to match
+  pre version 3.4.1 messages.
 
+
 3.10.0 (2010-09-14)
 -------------------
 

Modified: zope.testbrowser/trunk/src/zope/testbrowser/browser.py
===================================================================
--- zope.testbrowser/trunk/src/zope/testbrowser/browser.py	2010-09-21 09:57:29 UTC (rev 116701)
+++ zope.testbrowser/trunk/src/zope/testbrowser/browser.py	2010-09-21 11:33:37 UTC (rev 116702)
@@ -405,15 +405,17 @@
             label = labels[0].text
         else:
             label = None
-        self._start_timer()
         try:
-            self.mech_browser.form = form
-            self.mech_browser.submit(id=control.id, name=control.name,
-                                     label=label, coord=coord)
-        except Exception, e:
-            fix_exception_name(e)
-            raise
-        self._stop_timer()
+            self._start_timer()
+            try:
+                self.mech_browser.form = form
+                self.mech_browser.submit(id=control.id, name=control.name,
+                                         label=label, coord=coord)
+            except Exception, e:
+                fix_exception_name(e)
+                raise
+        finally:
+            self._stop_timer()
 
     def _changed(self):
         self._counter += 1
@@ -433,9 +435,15 @@
         if self._browser_counter != self.browser._counter:
             raise zope.testbrowser.interfaces.ExpiredError
         self.browser._start_timer()
-        self.browser.mech_browser.follow_link(self.mech_link)
-        self.browser._stop_timer()
-        self.browser._changed()
+        try:
+            try:
+                self.browser.mech_browser.follow_link(self.mech_link)
+            except Exception, e:
+                fix_exception_name(e)
+                raise
+        finally:
+            self.browser._stop_timer()
+            self.browser._changed()
 
     @property
     def url(self):
@@ -622,8 +630,10 @@
     def click(self):
         if self._browser_counter != self.browser._counter:
             raise zope.testbrowser.interfaces.ExpiredError
-        self.browser._clickSubmit(self.mech_form, self.mech_control, (1,1))
-        self.browser._changed()
+        try:
+            self.browser._clickSubmit(self.mech_form, self.mech_control, (1,1))
+        finally:
+            self.browser._changed()
 
 
 class ImageControl(Control):
@@ -632,8 +642,10 @@
     def click(self, coord=(1,1)):
         if self._browser_counter != self.browser._counter:
             raise zope.testbrowser.interfaces.ExpiredError
-        self.browser._clickSubmit(self.mech_form, self.mech_control, coord)
-        self.browser._changed()
+        try:
+            self.browser._clickSubmit(self.mech_form, self.mech_control, coord)
+        finally:
+            self.browser._changed()
 
 
 class ItemControl(SetattrErrorsMixin):
@@ -729,23 +741,31 @@
         if self._browser_counter != self.browser._counter:
             raise zope.testbrowser.interfaces.ExpiredError
         form = self.mech_form
-        if label is not None or name is not None:
-            intermediate, msg = self.browser._get_all_controls(
-                label, name, (form,))
-            intermediate = [
-                (control, form) for (control, form) in intermediate if
-                control.type in ('submit', 'submitbutton', 'image')]
-            control, form = disambiguate(intermediate, msg, index)
-            self.browser._clickSubmit(form, control, coord)
-        else: # JavaScript sort of submit
-            if index is not None or coord != (1,1):
-                raise ValueError(
-                    'May not use index or coord without a control')
-            request = self.mech_form._switch_click("request", mechanize.Request)
-            self.browser._start_timer()
-            self.browser.mech_browser.open(request)
-            self.browser._stop_timer()
-        self.browser._changed()
+        try:
+            if label is not None or name is not None:
+                intermediate, msg = self.browser._get_all_controls(
+                    label, name, (form,))
+                intermediate = [
+                    (control, form) for (control, form) in intermediate if
+                    control.type in ('submit', 'submitbutton', 'image')]
+                control, form = disambiguate(intermediate, msg, index)
+                self.browser._clickSubmit(form, control, coord)
+            else: # JavaScript sort of submit
+                if index is not None or coord != (1,1):
+                    raise ValueError(
+                        'May not use index or coord without a control')
+                request = self.mech_form._switch_click("request", mechanize.Request)
+                self.browser._start_timer()
+                try:
+                    try:
+                        self.browser.mech_browser.open(request)
+                    except Exception, e:
+                        fix_exception_name(e)
+                        raise
+                finally:
+                    self.browser._stop_timer()
+        finally:
+            self.browser._changed()
 
     def getControl(self, label=None, name=None, index=None):
         """See zope.testbrowser.interfaces.IBrowser"""

Modified: zope.testbrowser/trunk/src/zope/testbrowser/fixed-bugs.txt
===================================================================
--- zope.testbrowser/trunk/src/zope/testbrowser/fixed-bugs.txt	2010-09-21 09:57:29 UTC (rev 116701)
+++ zope.testbrowser/trunk/src/zope/testbrowser/fixed-bugs.txt	2010-09-21 11:33:37 UTC (rev 116702)
@@ -91,8 +91,113 @@
 
 
 Textareas with HTML/XML
-======================
+=======================
 
     >>> browser.open('http://localhost/@@/testbrowser/textarea.html')
     >>> browser.getControl('Text Area').value
     '<block>\r\n  <feed/>\r\n    &\r\n</block>'
+
+
+.click() with non-200 status
+============================
+
+The problem was that with the below controls testbrowser forgot to do
+after-processing after an exception in mechanize.
+That means ``_stop_timer()`` and ``_changed()`` were not executed if an exception
+was raised. Not calling ``_changed()`` resulted in not refreshing ``contents``.
+The ``contents`` property gets cached on any first access and should be reset
+on any navigation.
+The problem is that e.g. a simple 403 status raises an exception.
+
+This is how it works with a simple open():
+
+    >>> browser.handleErrors=False
+
+    >>> browser.open('http://localhost/set_status.html')
+    >>> print browser.contents
+    Everything fine
+
+    >>> browser.open('http://localhost/set_status.html?status=403')
+    Traceback (most recent call last):
+    ...
+    HTTPError: HTTP Error 403: Forbidden
+
+    >>> print browser.contents
+    Just set a status of 403
+
+
+These are the various controls:
+
+A link:
+
+    >>> browser.open('http://localhost/@@/testbrowser/status_lead.html')
+    >>> print browser.contents
+    <html>...
+
+    >>> browser.getLink('403').click()
+    Traceback (most recent call last):
+    ...
+    HTTPError: HTTP Error 403: Forbidden
+
+    >>> print browser.contents
+    Just set a status of 403
+
+A submit button:
+
+    >>> browser.open('http://localhost/@@/testbrowser/status_lead.html')
+    >>> print browser.contents
+    <html>...
+
+    >>> browser.getControl(name='status').value = '404'
+    >>> browser.getControl('Submit This').click()
+    Traceback (most recent call last):
+    ...
+    HTTPError: HTTP Error 404: Not Found
+
+    >>> print browser.contents
+    Just set a status of 404
+
+A submit image control:
+
+    >>> browser.open('http://localhost/@@/testbrowser/status_lead.html')
+    >>> print browser.contents
+    <html>...
+
+    >>> browser.getControl(name='status').value = '403'
+    >>> browser.getControl(name='image-value').click()
+    Traceback (most recent call last):
+    ...
+    HTTPError: HTTP Error 403: Forbidden
+
+    >>> print browser.contents
+    Just set a status of 403
+
+A javascript-ish form submit:
+
+    >>> browser.open('http://localhost/@@/testbrowser/status_lead.html')
+    >>> print browser.contents
+    <html>...
+
+    >>> browser.getControl(name='status').value = '404'
+    >>> browser.getForm(name='theform').submit()
+    Traceback (most recent call last):
+    ...
+    HTTPError: HTTP Error 404: Not Found
+
+    >>> print browser.contents
+    Just set a status of 404
+
+A non-javascript-ish form submit:
+
+    >>> browser.open('http://localhost/@@/testbrowser/status_lead.html')
+    >>> print browser.contents
+    <html>...
+
+    >>> browser.getControl(name='status').value = '403'
+    >>> browser.getForm(name='theform').submit(name='submit-value')
+    Traceback (most recent call last):
+    ...
+    HTTPError: HTTP Error 403: Forbidden
+
+    >>> print browser.contents
+    Just set a status of 403

Modified: zope.testbrowser/trunk/src/zope/testbrowser/ftests/__init__.py
===================================================================
--- zope.testbrowser/trunk/src/zope/testbrowser/ftests/__init__.py	2010-09-21 09:57:29 UTC (rev 116701)
+++ zope.testbrowser/trunk/src/zope/testbrowser/ftests/__init__.py	2010-09-21 11:33:37 UTC (rev 116702)
@@ -34,8 +34,20 @@
                 self.request.cookies.items()))
 
 class SetCookie(View):
-    """Gets cookie value"""
+    """Sets cookie value"""
 
     def __call__(self):
         self.request.response.setCookie(
             **dict((str(k), str(v)) for k, v in self.request.form.items()))
+
+
+class SetStatus(View):
+    """Sets HTTP status"""
+
+    def __call__(self):
+        status = self.request.get('status')
+        if status:
+            self.request.response.setStatus(int(status))
+            return 'Just set a status of %s' % status
+        else:
+            return 'Everything fine'

Modified: zope.testbrowser/trunk/src/zope/testbrowser/ftests/ftesting.zcml
===================================================================
--- zope.testbrowser/trunk/src/zope/testbrowser/ftests/ftesting.zcml	2010-09-21 09:57:29 UTC (rev 116701)
+++ zope.testbrowser/trunk/src/zope/testbrowser/ftests/ftesting.zcml	2010-09-21 11:33:37 UTC (rev 116702)
@@ -49,6 +49,13 @@
      permission="zope.Public"
      />
 
+  <browser:page
+     name="set_status.html"
+     for="*"
+     class=".ftests.SetStatus"
+     permission="zope.Public"
+     />
+
   <browser:resourceDirectory
       name="testbrowser"
       directory="ftests" />

Added: zope.testbrowser/trunk/src/zope/testbrowser/ftests/status_lead.html
===================================================================
--- zope.testbrowser/trunk/src/zope/testbrowser/ftests/status_lead.html	                        (rev 0)
+++ zope.testbrowser/trunk/src/zope/testbrowser/ftests/status_lead.html	2010-09-21 11:33:37 UTC (rev 116702)
@@ -0,0 +1,22 @@
+<html>
+  <body>
+
+    <h1>This page leads to status setting</h1>
+
+    <form action="http://localhost/set_status.html" method="get" name="theform">
+
+      <a href="http://localhost/set_status.html?status=403">403</a>
+
+      <input type="text" name="status" id="status"
+               value="Some Text" />
+
+      <input type="image" name="image-value" id="image-value"
+               src="zope3logo.gif" />
+
+      <input type="submit" name="submit-value" id="submit-value"
+               value="Submit This" />
+
+    </form>
+
+  </body>
+</html>


Property changes on: zope.testbrowser/trunk/src/zope/testbrowser/ftests/status_lead.html
___________________________________________________________________
Added: svn:keywords
   + Date Author Id Revision
Added: svn:eol-style
   + native



More information about the checkins mailing list