views:

60

answers:

1

Suppose I have a form like this:

<form id='myform'>
    Favorite colors?
    <input type='checkbox' name='color' value='Green'>Green
    <input type='checkbox' name='color' value='Blue'>Blue
    <input type='checkbox' name='color' value='Red'>Red
    <input type='submit' value='Submit'>
</form>

How do I use webtest's form library to test submitting multiple values?

A: 

Not sure about the form library, but you could use a MultiDict (you might have to use UnicodeMultiDict in some cases, I'm not sure).

from webob.multidict import MultiDict

class TestSomeController(TestController):

    def test_something(self):
        params = MultiDict()
        params.add('some_param', '1')
        params.add('color', 'Green')
        params.add('color', 'Blue')
        response = self.app.post(url('something'), params=params)
        assert 'something' in response

I never used WebTest to submit actual forms, but, looking at the source of the Form class, you can set the index of the field you want to set to disambiguate. I've not tested it, but something like that would probably work:

form = response.form
form.set('color', True, 0)
form.set('color', True, 2)
Antoine Leclair