diff options
| author | Carl Meyer <carl@oddbird.net> | 2013-08-30 20:05:48 -0600 |
|---|---|---|
| committer | Carl Meyer <carl@oddbird.net> | 2013-08-30 20:05:48 -0600 |
| commit | d009e5baabc502abe79730b73279e349a4539135 (patch) | |
| tree | 88aed531d95925f936b65463c7e2695ecd3f9e74 | |
| parent | dcfae9d18833a04f9b7d742fb7d7c6f26eee2923 (diff) | |
| download | webtest-d009e5baabc502abe79730b73279e349a4539135.tar.gz | |
Allow assigning a list to a set of same-named checkboxes.
| -rw-r--r-- | docs/form.html | 3 | ||||
| -rw-r--r-- | docs/forms.txt | 11 | ||||
| -rw-r--r-- | tests/html/form_inputs.html | 5 | ||||
| -rw-r--r-- | tests/test_forms.py | 12 | ||||
| -rw-r--r-- | webtest/forms.py | 18 |
5 files changed, 43 insertions, 6 deletions
diff --git a/docs/form.html b/docs/form.html index 76ce700..95f6d68 100644 --- a/docs/form.html +++ b/docs/form.html @@ -16,6 +16,9 @@ <input type="radio" name="radio" value="Radio 2" checked="checked"/> <input type="checkbox" name="checkbox" value="checkbox 1" /> <input type="checkbox" name="checkbox2" /> + <input type="checkbox" name="checkboxes" value="a"> + <input type="checkbox" name="checkboxes" value="b" checked="checked"> + <input type="checkbox" name="checkboxes" value="c"> <input type="hidden" name="hidden" value="1" /> <input type="file" name="file" value="" /> <input type="submit" name="submit" value="Submit" /> diff --git a/docs/forms.txt b/docs/forms.txt index 6215376..02569f6 100644 --- a/docs/forms.txt +++ b/docs/forms.txt @@ -142,6 +142,17 @@ If the checkbox has no value then it will be 'on' if you checked it:: >>> print(form['checkbox2'].value) on +If there are multiple checkboxes of the same name, you can assign a list to +that name to check all the checkboxes whose value is present in the list:: + + >>> form['checkboxes'] = ['a', 'c'] + >>> print(form.get('checkboxes', index=0).value) + a + >>> print(form.get('checkboxes', index=1).value) + None + >>> print(form.get('checkboxes', index=2).value) + c + Radio ***** diff --git a/tests/html/form_inputs.html b/tests/html/form_inputs.html index 5f046d9..6333430 100644 --- a/tests/html/form_inputs.html +++ b/tests/html/form_inputs.html @@ -36,6 +36,11 @@ <textarea name="textarea"> aaa</textarea> </form> + <form method="POST" id="multiple_checkbox_form"> + <input type="checkbox" name="checkbox" value="10"> + <input type="checkbox" name="checkbox" value="20" checked="checked"> + <input type="checkbox" name="checkbox" value="30"> + </form> </body> </html> diff --git a/tests/test_forms.py b/tests/test_forms.py index 31bea5f..9f75548 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -19,11 +19,11 @@ from tests.compat import u class TestForms(unittest.TestCase): - def callFUT(self, filename='form_inputs.html'): + def callFUT(self, filename='form_inputs.html', formid='simple_form'): dirname = os.path.join(os.path.dirname(__file__), 'html') app = DebugApp(form=os.path.join(dirname, filename), show_form=True) resp = webtest.TestApp(app).get('/form.html') - return resp.forms['simple_form'] + return resp.forms[formid] def test_set_submit_field(self): form = self.callFUT() @@ -95,6 +95,14 @@ class TestForms(unittest.TestCase): form = self.callFUT() self.assertEqual(form.text, str(form.html)) + def test_set_multiple_checkboxes(self): + form = self.callFUT(formid='multiple_checkbox_form') + form['checkbox'] = [10, 30] + + self.assertEqual(form.get('checkbox', index=0).value, '10') + self.assertEqual(form.get('checkbox', index=1).value, None) + self.assertEqual(form.get('checkbox', index=2).value, '30') + class TestResponseFormAttribute(unittest.TestCase): diff --git a/webtest/forms.py b/webtest/forms.py index 98c306f..8875dd3 100644 --- a/webtest/forms.py +++ b/webtest/forms.py @@ -427,6 +427,10 @@ class Form(object): """Set the value of the named field. If there is 0 or multiple fields by that name, it is an error. + Multiple checkboxes of the same name are special-cased; a list may be + assigned to them to check the checkboxes whose value is present in the + list (and uncheck all others). + Setting the value of a ``<select>`` selects the given option (and confirms it is an option). Setting radio fields does the same. Checkboxes get boolean values. You cannot set hidden fields or buttons. @@ -437,10 +441,16 @@ class Form(object): assert fields is not None, ( "No field by the name %r found (fields: %s)" % (name, ', '.join(map(repr, self.fields.keys())))) - assert len(fields) == 1, ( - "Multiple fields match %r: %s" - % (name, ', '.join(map(repr, fields)))) - fields[0].value = value + all_checkboxes = all(isinstance(f, Checkbox) for f in fields) + if all_checkboxes and isinstance(value, list): + values = set(utils.stringify(v) for v in value) + for f in fields: + f.checked = f._value in values + else: + assert len(fields) == 1, ( + "Multiple fields match %r: %s" + % (name, ', '.join(map(repr, fields)))) + fields[0].value = value def __getitem__(self, name): """Get the named field object (ambiguity is an error).""" |
