Commits

Gael Pasgrimaud committed 72ab56d

Improve selenium api
Add Form.__repr__
Few doc improvements

Comments (0)

Files changed (8)

 +++++++++++++++++++++++++++++++++
 
 :author: Ian Bicking <ianb@colorstudy.com>
+:maintainer: Gael Pasgrimaud <gael@gawel.org>
 
 .. toctree::
    :maxdepth: 1
 TestApp
 =======
 
-The most important object in WebTest is ``webtest.TestApp``, the
-wrapper for WSGI applications.  To use it, you simply instantiate it
-with your WSGI application.  (Note: if your WSGI application requires
-any configuration, you must set that up manually in your tests.)
+The most important object in WebTest is :class:`~webtest.TestApp`, the wrapper
+for WSGI applications.  To use it, you simply instantiate it with your WSGI
+application. (Note: if your WSGI application requires any configuration, you
+must set that up manually in your tests.)
 
 .. code-block:: python
 
     >>> res.status
     '200 OK'
     >>> res.form
-    <webtest.app.Form object at ...>
+    <Form />
 
 Making Requests
 ---------------
 ``response.forms``:
     Return a dictionary of forms; you can use both indexes (refer to
     the forms in order) or the string ids of forms (if you've given
-    them ids) to identify the form.  See `Form Submissions <#form-submissions>`_ for
+    them ids) to identify the form.  See :ref:`form-submissions` for
     more on the form objects.
 
 ``response.form``:
     If there is just a single form, this returns that.  It is an error
     if you use this and there are multiple forms.
 
+.. _form-submissions:
+
 Form Submissions
 ================
 

docs/modules/sel.txt

 
 .. autoclass:: TestResponse
    :members:
+
 .. autoclass:: Document
    :members:
+
 .. autoclass:: Element
    :members:
+
 .. autoclass:: Form
    :members:
 

docs/modules/webtest.txt

 
 .. autoclass:: TestApp
    :members:
+
 .. autoexception:: AppError
 
 Return Values
 
 .. autoclass:: TestResponse
    :members:
+
 .. autoclass:: TestRequest
    :members:
+
 .. autoclass:: Form
    :members:
+
 .. autoclass:: Field
+
 .. autoclass:: Select
+
 .. autoclass:: Radio
+
 .. autoclass:: Checkbox
+
 .. autoclass:: Text
+
 .. autoclass:: Textarea
+
 .. autoclass:: Hidden
+
 .. autoclass:: Submit
 
 
 
 try:
     # py < 2.7
-    import unnitest2 as unittest
+    import unittest2 as unittest
 except ImportError:
     import unittest
 

tests/test_selenium.py

         draggable = resp.doc.draggable
         droppable = resp.doc.droppable
         self.assertFalse(droppable.hasClass('ui-state-highlight'))
-        draggable.dragAndDropToObject(droppable)
+        draggable.drag_and_drop(droppable)
         self.assertTrue(droppable.hasClass('ui-state-highlight'))
 
         resp.doc.link('Shopping Cart').click()
 
     def forms__get(self):
         """
-        Returns a dictionary of ``Form`` objects.  Indexes are both in
-        order (from zero) and by form id (if the form is given an id).
+        Returns a dictionary of :class:`~webtest.Form` objects.  Indexes are
+        both in order (from zero) and by form id (if the form is given an id).
         """
         if self._forms_indexed is None:
             self._parse_forms()
 
     forms = property(forms__get,
                      doc="""
-                     A list of <form>s found on the page (instances of
-                     ``Form``)
+                     A list of :class:`~webtest.Form`s found on the page
                      """)
 
     def form__get(self):
 
     form = property(form__get,
                     doc="""
-                    Returns a single ``Form`` instance; it
-                    is an error if there are multiple forms on the
-                    page.
+                    Returns a single :class:`~webtest.Form` instance; it is an
+                    error if there are multiple forms on the page.
                     """)
 
     @property
                     submit.append((name, value))
         return submit
 
+    def __repr__(self):
+        value = '<Form'
+        if self.id:
+            value += ' id=%r' % str(self.id)
+        return value + ' />'
+
 ########################################
 ## Utility functions
 ########################################

webtest/compat.py

     def to_bytes(s):
         if isinstance(s, bytes):
             return s
-        return s.encode('ISO-8859-1')
+        return s.encode('latin1')
 
     def to_string(s):
         if isinstance(s, str):
             return s
-        return str(s, 'ISO-8859-1')
+        return str(s, 'latin1')
 
     def join_bytes(sep, l):
         l = [to_bytes(e) for e in l]
 """
 Routines for testing WSGI applications with selenium.
 
-Most interesting is :class:`~webtest.browser.browsereniumApp` and the
-:func:`~webtest.browser.browserenium` decorator
+Most interesting is :class:`~webtest.sel.SeleniumApp` and the
+:func:`~webtest.sel.selenium` decorator
 """
 import os
 import cgi
         json = False
 
 
+try:
+    unicode()
+except NameError:
+    unicode = str
+
+
 log = logging.getLogger(__name__)
 
-sys_stdout = sys.stdout
 
 if 'SELENIUM_VERBOSE':
     log.addHandler(logging.StreamHandler(sys.stderr))
     def _body__get(self):
         body = self.browser.getHtmlSource()
         if isinstance(body, binary_type):
-            return body.encode(self.charset or 'utf-8')
+            return unicode(body, self.charset or 'utf-8')
         else:
             return body
 
 
         return wrapped
 
+    def exist(self):
+        """return true is the element is present"""
+        return self.isElementPresent()
+
     def wait(self, timeout=3000):
         """Wait for an element and return this element"""
         script = "selenium.isElementPresent(%r) || null" % str(self)
 
     def hasClass(self, name):
         """True iif the class is present"""
-        classes = self.eval('e.getAttribute("class")').split()
+        classes = self.attr('class').split()
         return name in classes
 
     def html(self):
 
     def attr(self, attr):
         """Return the attribute value of the element"""
-        return self.getAttribute(attr)
+        return self.eval('e.getAttribute(%r)' % str(attr))
+
+    def drag_and_drop(self, element):
+        return self.dragAndDropToObject(element)
 
     def value__get(self):
         return self.getValue()
         return s in self.html()
 
     def __nonzero__(self):
-        return self.isElementPresent()
+        return self.exist()
     __bool__ = __nonzero__
 
     def __repr__(self):
 
     def __contains__(self, s):
         if isinstance(s, Element):
-            return s.isElementPresent()
+            return s.exist()
         return self.browser.isTextPresent(_get_value(s))
 
     def __call__(self, locator):
         return Form(self.resp, key)
 
 
-class Form(testapp.Form):
+class Form(testapp.Form, Element):
     """See :class:`~webtest.Form`"""
 
     FieldClass = Field
             self.locator = _eval_xpath('form', index=id)
         else:
             self.locator = _eval_xpath('form', id=id)
-        if not self.browser.isElementPresent(self.locator):
+        if not self:
             raise LookupError('No form found at %s' % self.locator)
-        form = self.browser.getEval(
-            "this.browserbot.findElement('%s').innerHTML;" % self.locator)
+        form = self.eval('e.innerHTML')
         super(Form, self).__init__(resp, '<form>%s</form>' % form)
 
     def _parse_fields(self):
         """Submits the form.  If ``name`` is given, then also select that
         button (using ``index`` to disambiguate)``.
 
-        Any extra keyword arguments are passed to the ``.get()`` or
-        ``.post()`` method.
-
         Returns a :class:`webtest.browser.TestResponse` object.
         """
         if timeout != 0:
             return False
         else:
             jar = os.environ['SELENIUM_JAR']
-            p = subprocess.Popen(['java', '-jar', jar], stdout=sys_stdout)
+            p = subprocess.Popen(['java', '-jar', jar])
             os.environ['SELENIUM_PID'] = str(p.pid)
             for i in range(30):
                 time.sleep(.3)