Form handling ============= .. >>> from webtest.debugapp import make_debug_app >>> from webtest.app import TestApp >>> app = make_debug_app({}, ... form='docs/form.html', ... show_form=True) >>> app = TestApp(app) Getting a form -------------- If you have a single html form in your page, just use the ``.form`` attribute: .. code-block:: python >>> res = app.get('/form.html') >>> form = res.form If you have more then one HTML form in your page, use the ``.forms`` property and access via the form index: .. code-block:: python >>> form = res.forms[0] Or the form id: .. code-block:: python >>> form = res.forms['myform'] You can check form attributes: .. code-block:: python >>> print(form.id) myform >>> print(form.action) /form-submit >>> print(form.method) POST Filling a form -------------- You can fill out and submit forms from your tests. Fields are a dict like object: .. code-block:: python >>> # dict of fields >>> form.fields.items() #doctest: +SKIP [(u'text', []), ..., (u'submit', [])] You can check the current value: .. code-block:: python >>> print(form['text'].value) Foo Then you fill it in fields: .. code-block:: python >>> form['text'] = 'Bar' >>> # When names don't point to a single field: >>> form.set('text', 'Bar', index=0) Field types ------------ Input and textarea fields ************************* .. code-block:: python >>> print(form['textarea'].value) Some text >>> form['textarea'] = 'Some other text' You can force the value of an hidden field:: >>> form['hidden'].force_value('2') Select fields ************* Simple select: .. code-block:: python >>> print(form['select'].value) option2 >>> form['select'] = 'option1' Select multiple: .. code-block:: python >>> print(form['multiple'].value) # doctest: +SKIP ['option2', 'option3'] >>> form['multiple'] = ['option1'] You can select an option by its text with ``.select()``: .. code-block:: python >>> form['select'].select(text="Option 2") >>> print(form['select'].value) option2 For select multiple use ``.select_multiple()``: .. code-block:: python >>> form['multiple'].select_multiple(texts=["Option 1", "Option 2"]) >>> print(form['multiple'].value) # doctest: +SKIP ['option1', 'option2'] Select fields can only be set to valid values (i.e., values in an ``