Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid value for unchecked checkboxes #165

Open
bochecha opened this issue Sep 5, 2016 · 1 comment
Open

Invalid value for unchecked checkboxes #165

bochecha opened this issue Sep 5, 2016 · 1 comment

Comments

@bochecha
Copy link

bochecha commented Sep 5, 2016

Using webtest.forms to check the forms generated by my views, the value of a checkbox is None when the checkbox is not checked.

Here is a very simple reproducer.

First, create a checkboxes folder, with acheckboxes/__init__.py file in it, containing the following:

from pyramid.config import Configurator


def home_view(request):
    return {'active': 'id1'}


def main(global_config, **settings):
    config = Configurator(settings=settings)
    config.include('pyramid_jinja2')

    config.add_route('home', '/')
    config.add_view(
        '.home_view', route_name='home',
        renderer='home.jinja2')

    return config.make_wsgi_app()


if __name__ == '__main__':
    from webtest import TestApp

    app = TestApp(main({}, **{}))
    response = app.get('/')

    form = response.forms['home-form']
    checkboxes = form.fields['checkboxes']

    assert checkboxes[0].id == 'id1'
    assert checkboxes[0].checked == True
    assert checkboxes[0].value == 'value1'

    assert checkboxes[1].id == 'id2'
    assert checkboxes[1].checked == False
    assert checkboxes[1].value == 'value2'

In addition, create the checkboxes/home.jinja2 template file, containing the following:

<html>
  <body>
    <form id="home-form">
      <ul>
        <li><input type="checkbox" id="id1" name="checkboxes" value="value1" {% if active == 'id1' %}checked="checked"{% endif %}></li>
        <li><input type="checkbox" id="id2" name="checkboxes" value="value2" {% if active == 'id2' %}checked="checked"{% endif %}></li>
      </ul>
  </body>
</html>

Install pyramid, pyramid_jinja2 and webtest, then run python checkboxes/__init__.py:

$ python checkboxes/__init__.py 
Traceback (most recent call last):
  File "checkboxes/__init__.py", line 35, in <module>
    assert checkboxes[1].value == 'value2'
AssertionError

Why is the checkbox value not what is set in the DOM? This makes testing harder.

We found that checkboxes[1]._value == 'value2' which is the correct value. But using private attributes doesn't feel right.

@bochecha
Copy link
Author

bochecha commented Sep 5, 2016

Turns out the webtest code does this explicitly:

    def value__get(self):
        if self._checked:
            if self._value is None:
                return 'on'
            else:
                return self._value
        else:
            return None

This seems to have always been this way in webtest, the very first commit (c173c2d) already has the code doing the same (except it returns 'checked' instead of 'on', but that's not related)

That commit seems to be a rename from an older project called wsgitest, but I couldn't go back further so I stopped digging up the past at that point. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant