[Zope3-checkins] SVN: Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/ refactor to get ready to share some code with "real"

Benji York benji at zope.com
Wed Aug 16 21:50:55 EDT 2006


Log message for revision 69577:
  refactor to get ready to share some code with "real"
  

Changed:
  U   Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/README.txt
  U   Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/browser.py
  A   Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/forms.py

-=-
Modified: Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/README.txt
===================================================================
--- Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/README.txt	2006-08-17 01:01:30 UTC (rev 69576)
+++ Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/README.txt	2006-08-17 01:50:54 UTC (rev 69577)
@@ -57,6 +57,8 @@
 The contents of the current page are available:
 
     >>> print browser.contents
+    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+    "DTD/xhtml1-strict.dtd">
     <html>
       <head>
         <title>Simple Page</title>
@@ -196,18 +198,18 @@
 
     >>> browser.open('http://localhost/@@/testbrowser/navigate.html')
     >>> browser.contents
-    '...> Link Text \n    with     Whitespace\tNormalization (and parens) </...'
-    >>> link = browser.getLink('Link Text with Whitespace Normalization '
+    '...> Link \n    with     Whitespace\tNormalization (and parens) </...'
+    >>> link = browser.getLink('Link with Whitespace Normalization '
     ...                        '(and parens)')
     >>> link
-    <Link text='Link Text with Whitespace Normalization (and parens)'...>
+    <Link text='Link with Whitespace Normalization (and parens)'...>
     >>> link.text
-    'Link Text with Whitespace Normalization (and parens)'
+    'Link with Whitespace Normalization (and parens)'
     >>> link.click()
     >>> browser.url
-    'http://localhost/@@/testbrowser/navigate.html?message=By+Link+Text+with+Normalization'
+    'http://localhost/@@/testbrowser/navigate.html?message=By+Link+with+Normalization'
     >>> browser.contents
-    '...Message: <em>By Link Text with Normalization</em>...'
+    '...Message: <em>By Link with Normalization</em>...'
 
 Note that clicking a link object after its browser page has expired will
 generate an error.
@@ -322,7 +324,7 @@
     >>> browser.getControl('Ambiguous Control')
     Traceback (most recent call last):
     ...
-    AmbiguityError: label 'Ambiguous Control'
+    AmbiguityError: "label 'Ambiguous Control'"
 
 This is also true if an option in a control is ambiguous in relation to
 the control itself.
@@ -330,7 +332,7 @@
     >>> browser.getControl('Sub-control Ambiguity')
     Traceback (most recent call last):
     ...
-    AmbiguityError: label 'Sub-control Ambiguity'
+    AmbiguityError: "label 'Sub-control Ambiguity'"
 
 Ambiguous controls may be specified using an index value.  We use the control's
 value attribute to show the two controls; this attribute is properly introduced
@@ -395,7 +397,7 @@
     >>> browser.getControl(name='ambiguous-control-name')
     Traceback (most recent call last):
     ...
-    AmbiguityError: name 'ambiguous-control-name'
+    AmbiguityError: "name 'ambiguous-control-name'"
     >>> browser.getControl(name='does-not-exist')
     Traceback (most recent call last):
     ...
@@ -533,7 +535,7 @@
     >>> browser.getControl('Third') # ambiguous in the browser, so useful
     Traceback (most recent call last):
     ...
-    AmbiguityError: label 'Third'
+    AmbiguityError: "label 'Third'"
 
 Finally, submit controls provide ISubmitControl, and image controls provide
 IImageSubmitControl, which extents ISubmitControl.  These both simply add a
@@ -1045,11 +1047,11 @@
     >>> browser.getControl(name='text-value')
     Traceback (most recent call last):
     ...
-    AmbiguityError: name 'text-value'
+    AmbiguityError: "name 'text-value'"
     >>> browser.getControl('Text Control')
     Traceback (most recent call last):
     ...
-    AmbiguityError: label 'Text Control'
+    AmbiguityError: "label 'Text Control'"
 
 I'll always get an ambiguous form field.  I can use the index argument, or
 with the `getForm` method I can disambiguate by searching only within a given

Modified: Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/browser.py
===================================================================
--- Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/browser.py	2006-08-17 01:01:30 UTC (rev 69576)
+++ Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/browser.py	2006-08-17 01:50:54 UTC (rev 69577)
@@ -19,6 +19,7 @@
 from cStringIO import StringIO
 from test import pystone
 from zope.testbrowser import interfaces
+from zope.testbrowser.forms import getControl, getForm, getAllControls
 from zope.testbrowser.utilities import disambiguate, any, onlyOne, zeroOrOne, \
     SetattrErrorsMixin, PystoneTimer, compressText, RegexType
 import ClientForm
@@ -199,75 +200,6 @@
             args = {'text_regex': text_regex, 'url_regex': url_regex}
         return Link(self.mech_browser.find_link(**args), self)
 
-    def _findByLabel(self, label, forms, include_subcontrols=False):
-        # forms are iterable of mech_forms
-        matches = re.compile(r'(^|\b|\W)%s(\b|\W|$)'
-                             % re.escape(compressText(label))).search
-        found = []
-        for f in forms:
-            for control in f.controls:
-                phantom = control.type in ('radio', 'checkbox')
-                if not phantom:
-                    for l in control.get_labels():
-                        if matches(l.text):
-                            found.append((control, f))
-                            break
-                if include_subcontrols and (
-                    phantom or control.type=='select'):
-
-                    for i in control.items:
-                        for l in i.get_labels():
-                            if matches(l.text):
-                                found.append((i, f))
-                                found_one = True
-                                break
-
-        return found
-
-    def _findByName(self, name, forms):
-        found = []
-        for f in forms:
-            for control in f.controls:
-                if control.name==name:
-                    found.append((control, f))
-        return found
-
-    def getControl(self, label=None, name=None, index=None):
-        """See zope.testbrowser.interfaces.IBrowser"""
-        intermediate, msg = self._get_all_controls(
-            label, name, self.mech_browser.forms(), include_subcontrols=True)
-        control, form = disambiguate(intermediate, msg, index)
-        return controlFactory(control, form, self)
-
-    def _get_all_controls(self, label, name, forms, include_subcontrols=False):
-        onlyOne([label, name], '"label" and "name"')
-
-        if label is not None:
-            res = self._findByLabel(label, forms, include_subcontrols)
-            msg = 'label %r' % label
-        elif name is not None:
-            res = self._findByName(name, forms)
-            msg = 'name %r' % name
-        return res, msg
-
-    def getForm(self, id=None, name=None, action=None, index=None):
-        zeroOrOne([id, name, action], '"id", "name", and "action"')
-        if index is None and not any([id, name, action]):
-            raise ValueError(
-                'if no other arguments are given, index is required.')
-
-        matching_forms = []
-        for form in self.mech_browser.forms():
-            if ((id is not None and form.attrs.get('id') == id)
-            or (name is not None and form.name == name)
-            or (action is not None and re.search(action, str(form.action)))
-            or id == name == action == None):
-                matching_forms.append(form)
-
-        form = disambiguate(matching_forms, '', index)
-        self.mech_browser.form = form
-        return Form(self, form)
-
     def _clickSubmit(self, form, control, coord):
         self._start_timer()
         self.mech_browser.open(form.click(
@@ -278,7 +210,18 @@
         self._counter += 1
         self._contents = None
 
+    def getControl(self, label=None, name=None, index=None):
+        """See zope.testbrowser.interfaces.IBrowser"""
+        forms = self.mech_browser.forms()
+        control, form = getControl(forms, label, name, index)
+        return controlFactory(control, form, self)
 
+    def getForm(self, id=None, name=None, action=None, index=None):
+        """See zope.testbrowser.interfaces.IBrowser"""
+        form = getForm(self.mech_browser.forms(), id, name, action, index)
+        return Form(self, form)
+
+
 class Link(SetattrErrorsMixin):
     interface.implements(interfaces.ILink)
 
@@ -589,8 +532,7 @@
             raise 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, msg = getAllControls([form], label, name)
             intermediate = [
                 (control, form) for (control, form) in intermediate if
                 control.type in ('submit', 'submitbutton', 'image')]
@@ -610,7 +552,8 @@
         """See zope.testbrowser.interfaces.IBrowser"""
         if self._browser_counter != self.browser._counter:
             raise interfaces.ExpiredError
-        intermediate, msg = self.browser._get_all_controls(
-            label, name, (self.mech_form,), include_subcontrols=True)
+        forms = [self.mech_form]
+        intermediate, msg = getAllControls(forms, label, name,
+                                           include_subcontrols=True)
         control, form = disambiguate(intermediate, msg, index)
         return controlFactory(control, form, self.browser)

Added: Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/forms.py
===================================================================
--- Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/forms.py	2006-08-17 01:01:30 UTC (rev 69576)
+++ Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/forms.py	2006-08-17 01:50:54 UTC (rev 69577)
@@ -0,0 +1,68 @@
+from zope.testbrowser.utilities import disambiguate, any, onlyOne, zeroOrOne, \
+    compressText
+import re
+
+def findByLabel(label, forms, include_subcontrols=False):
+    # forms are iterable of mech_forms
+    matches = re.compile(r'(^|\b|\W)%s(\b|\W|$)'
+                         % re.escape(compressText(label))).search
+    found = []
+    for f in forms:
+        for control in f.controls:
+            phantom = control.type in ('radio', 'checkbox')
+            if not phantom:
+                for l in control.get_labels():
+                    if matches(l.text):
+                        found.append((control, f))
+                        break
+            if include_subcontrols and (
+                phantom or control.type=='select'):
+
+                for i in control.items:
+                    for l in i.get_labels():
+                        if matches(l.text):
+                            found.append((i, f))
+                            found_one = True
+                            break
+
+    return found
+
+def findByName(name, forms):
+    found = []
+    for f in forms:
+        for control in f.controls:
+            if control.name==name:
+                found.append((control, f))
+    return found
+
+def getControl(forms, label=None, name=None, index=None):
+    intermediate, msg = getAllControls(
+        forms, label, name, include_subcontrols=True)
+    return disambiguate(intermediate, msg, index)
+
+def getAllControls(forms, label, name, include_subcontrols=False):
+    onlyOne([label, name], '"label" and "name"')
+
+    if label is not None:
+        res = findByLabel(label, forms, include_subcontrols)
+        msg = 'label %r' % label
+    elif name is not None:
+        res = findByName(name, forms)
+        msg = 'name %r' % name
+    return res, msg
+
+def getForm(forms, id=None, name=None, action=None, index=None):
+    zeroOrOne([id, name, action], '"id", "name", and "action"')
+    if index is None and not any([id, name, action]):
+        raise ValueError(
+            'if no other arguments are given, index is required.')
+
+    matching_forms = []
+    for form in forms:
+        if ((id is not None and form.attrs.get('id') == id)
+        or (name is not None and form.name == name)
+        or (action is not None and re.search(action, str(form.action)))
+        or id == name == action == None):
+            matching_forms.append(form)
+
+    return disambiguate(matching_forms, '', index)


Property changes on: Zope3/branches/benji-testbrowser-with-real-browsers-take-2/src/zope/testbrowser/forms.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Zope3-Checkins mailing list