How to use the webtest.forms function in WebTest

To help you get started, we’ve selected a few WebTest examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Pylons / webtest / webtest / sel.py View on Github external
Field.classes['file'] = File


class Textarea(Text):
    """Field representing ``<textarea>``"""

Field.classes['textarea'] = Textarea


class Hidden(Text, forms.Hidden):
    """Field representing ``&lt;input type="hidden"&gt;``"""

Field.classes['hidden'] = Hidden


class Submit(Field, forms.Submit):
    """Field representing ``&lt;input type="submit"&gt;`` and ``&lt;button&gt;``"""

    settable = False

    def value__get(self):
        return None

    value = property(value__get)

    def value_if_submitted(self):
        return self._value

Field.classes['submit'] = Submit

Field.classes['button'] = Submit
</textarea>
github zopefoundation / zope.testbrowser / src / zope / testbrowser / browser.py View on Github external
def simpleControlFactory(wtcontrol, form, elemindex, browser):
    if isinstance(wtcontrol, webtest.forms.Radio):
        elems = [e for e in elemindex
                 if e.attrs.get('name') == wtcontrol.name]
        return RadioListControl(wtcontrol, form, elems, browser)

    elem = elemindex[wtcontrol.pos]
    if isinstance(wtcontrol, (webtest.forms.Select,
                              webtest.forms.MultipleSelect)):
        return ListControl(wtcontrol, form, elem, browser)

    elif isinstance(wtcontrol, webtest.forms.Submit):
        if wtcontrol.attrs.get('type', 'submit') == 'image':
            return ImageControl(wtcontrol, form, elem, browser)
        else:
            return SubmitControl(wtcontrol, form, elem, browser)
    else:
        return Control(wtcontrol, form, elem, browser)
github zopefoundation / zope.testbrowser / src / zope / testbrowser / browser.py View on Github external
def simpleControlFactory(wtcontrol, form, elemindex, browser):
    if isinstance(wtcontrol, webtest.forms.Radio):
        elems = [e for e in elemindex
                 if e.attrs.get('name') == wtcontrol.name]
        return RadioListControl(wtcontrol, form, elems, browser)

    elem = elemindex[wtcontrol.pos]
    if isinstance(wtcontrol, (webtest.forms.Select,
                              webtest.forms.MultipleSelect)):
        return ListControl(wtcontrol, form, elem, browser)

    elif isinstance(wtcontrol, webtest.forms.Submit):
        if wtcontrol.attrs.get('type', 'submit') == 'image':
            return ImageControl(wtcontrol, form, elem, browser)
        else:
            return SubmitControl(wtcontrol, form, elem, browser)
    else:
        return Control(wtcontrol, form, elem, browser)
github Pylons / webtest / webtest / app.py View on Github external
content_type=None):
        """
        Do a generic request.
        """

        environ = self._make_environ(extra_environ)

        inline_uploads = []

        # this supports OrderedDict
        if isinstance(params, dict) or hasattr(params, 'items'):
            params = list(params.items())

        if isinstance(params, (list, tuple)):
            inline_uploads = [v for (k, v) in params
                              if isinstance(v, (forms.File, forms.Upload))]

        if len(inline_uploads) > 0:
            content_type, params = self.encode_multipart(
                params, upload_files or ())
            environ['CONTENT_TYPE'] = content_type
        else:
            params = utils.encode_params(params, content_type)
            if upload_files or \
                (content_type and
                 to_bytes(content_type).startswith(b'multipart')):
                params = urlparse.parse_qsl(params, keep_blank_values=True)
                content_type, params = self.encode_multipart(
                    params, upload_files or ())
                environ['CONTENT_TYPE'] = content_type
            elif params:
                environ.setdefault('CONTENT_TYPE',
github zopefoundation / zope.testbrowser / src / zope / testbrowser / browser.py View on Github external
infos = []
            if 'readonly' in ctrl.attrs or tp == 'hidden':
                infos.append('readonly')
            if 'disabled' in ctrl.attrs:
                infos.append('disabled')

            classnames = {'password': "PasswordControl",
                          'hidden': "HiddenControl",
                          'email': "EMailControl",
                          }
            clname = classnames.get(tp, "TextControl")
            return "&lt;%s(%s=%s)%s&gt;" % (
                clname, toStr(ctrl.name), toStr(ctrl.value),
                ' (%s)' % (', '.join(infos)) if infos else '')

        if isinstance(ctrl, webtest.forms.File):
            return repr(ctrl) + "&lt;-- unknown"
        raise NotImplementedError(str((self, ctrl)))
github zopefoundation / zope.testbrowser / src / zope / testbrowser / browser.py View on Github external
def mechRepr(self):
        # emulate mechanize control representation
        toStr = self.browser.toStr
        ctrl = self._control
        if isinstance(ctrl, (webtest.forms.Text, webtest.forms.Email)):
            tp = ctrl.attrs.get('type')
            infos = []
            if 'readonly' in ctrl.attrs or tp == 'hidden':
                infos.append('readonly')
            if 'disabled' in ctrl.attrs:
                infos.append('disabled')

            classnames = {'password': "PasswordControl",
                          'hidden': "HiddenControl",
                          'email': "EMailControl",
                          }
            clname = classnames.get(tp, "TextControl")
            return "&lt;%s(%s=%s)%s&gt;" % (
                clname, toStr(ctrl.name), toStr(ctrl.value),
                ' (%s)' % (', '.join(infos)) if infos else '')
github zopefoundation / zope.testbrowser / src / zope / testbrowser / browser.py View on Github external
def _selectedIndex(self, index):
        self._control.force_value(webtest.forms.NoValue)
        self._control.selectedIndex = index
github apache / allura / AlluraTest / alluratest / validation.py View on Github external
def _validate_params(self, params, method):
        if not params:
            return
        # params can be a list or a dict
        if hasattr(params, 'items'):
            params = params.items()
        for k, v in params:
            if not isinstance(k, basestring):
                raise TypeError('%s key %s is %s, not str' %
                                (method, k, type(k)))
            if not isinstance(v, (basestring, webtest.forms.File)):
                raise TypeError(
                    '%s key %s has value %s of type %s, not str. ' %
                    (method, k, v, type(v)))
github Pylons / webtest / webtest / app.py View on Github external
fcontent = mimetypes.guess_type(filename.decode('utf8'))[0]
            fcontent = to_bytes(fcontent)
            fcontent = fcontent or b'application/octet-stream'
            lines.extend([
                b'--' + boundary,
                b'Content-Disposition: form-data; ' +
                b'name="' + key + b'"; filename="' + filename + b'"',
                b'Content-Type: ' + fcontent, b'', value])

        for key, value in params:
            if isinstance(key, text_type):
                try:
                    key = key.encode('ascii')
                except:  # pragma: no cover
                    raise  # field name are always ascii
            if isinstance(value, forms.File):
                if value.value:
                    _append_file([key] + list(value.value))
                else:
                    # If no file was uploaded simulate an empty file with no
                    # name like real browsers do:
                    _append_file([key, b'', b''])
            elif isinstance(value, forms.Upload):
                file_info = [key, value.filename]
                if value.content is not None:
                    file_info.append(value.content)
                    if value.content_type is not None:
                        file_info.append(value.content_type)
                _append_file(file_info)
            else:
                if isinstance(value, int):
                    value = str(value).encode('utf8')